文章目录
  1. 1. 中介者模式
    1. 1.1. 泡泡堂游戏-实例
  2. 2. 参考文档

中介者模式

中介者模式的作用是解除对象和对象之间的紧耦合关系.

泡泡堂游戏-实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

function Player(name, teamColor) {
this.name = name; // 角色名称
this.teamColor = teamColor; // 队伍颜色
this.state = 'alive'; // 玩家生存状态
}

Player.prototype.win = function() {
console.log(this.name + ' won ');
};

Player.prototype.lose = function() {
console.log(this.name + ' lost');
};

Player.prototype.die = function() {
this.state = 'dead';
PlayerDirector.ReceiveMessage('playerDead', this); // 给中介者发送消息,玩家死亡
};

Player.prototype.remove = function() {
PlayerDirector.ReceiveMessage('removePlayer', this);
};


Player.prototype.changeTeam = function(color) {
PlayerDirector.ReceiveMessage('changeTeam', this);
};

var PlayerFactory = function(name, teamColor) {
var newPlayer = new Player(name, teamColor);
PlayerDirector.ReceiveMessage('addPlayer', newPlayer);

return newPlayer;
};

var PlayerDirector = (function() {
var players = {};
var operations = {}; //中介者可以执行的操作;

operations.addPlayer = function(player) {
var teamColor = player.teamColor;
// 如果给颜色的玩家没有成立队伍,则新建一个队伍
players[teamColor] = players[teamColor] || [];
players[teamColor].push(player);
};

operations.removePlayer = function(player) {
var teamColor = player.teamColor;
var teamPlayers = players[teamColor];

for (var i = teamPlayers.length - 1; i >= 0; i--) {
if (teamPlayers[i] === player) {
teamPlayers.splice(i, 1);
}
}
};

operations.changeTeam = function(player, newTeamColor) {
operations.removePlayer(player);
player.teamColor = new newTeamColor;
operations.addPlayer(player);
};

operations.playerDead = function(player) {
var teamColor = player.teamColor;
var teamPlayers = players[teamColor];

var all_dead = true;

for (var i = 0, p; p = teamPlayers[i++]; ) {
if (p.state !== 'dead') {
all_dead = false;
break;
}
}

if (all_dead) {
for (var i = 0, p; p = teamPlayers[i++]; ) {
p.lose();
}

for (var color in players) {
if (color !== teamColor) {
var teamPlayers = players[color];

for (var i = 0, p; p = teamPlayers[i++];) {
p.win();
}
}
}
}
};


var ReceiveMessage = function() {
var message = [].shift.call(arguments);
operations[message].apply(this, arguments);
};

return {
ReceiveMessage: ReceiveMessage
};
}());

var player1 = PlayerFactory('red1', 'red');
var player2 = PlayerFactory('red2', 'red');
var player3 = PlayerFactory('red3', 'red');
var player4 = PlayerFactory('red4', 'red');

var player5 = PlayerFactory('blue1', 'blue');
var player6 = PlayerFactory('blue2', 'blue');
var player7 = PlayerFactory('blue3', 'blue');
var player8 = PlayerFactory('blue4', 'blue');

player1.die();
player2.die();
player3.die();
player4.die();
/*
red1 lost
red2 lost
red3 lost
red4 lost
blue1 won
blue2 won
blue3 won
blue4 won
*/

如果对象之间的复杂耦合确实导致调用和维护出现了困难,而且这些耦合度随项目的变化呈指数增长曲线,那我们就可以考虑用中介者模式重构代码

参考文档

  • 实体书: JavaScript设计模式与开发实践(曾探)

文章若有纰漏请大家补充指正,谢谢~~
http://blog.xinshangshangxin.com SHANG殇

文章目录
  1. 1. 中介者模式
    1. 1.1. 泡泡堂游戏-实例
  2. 2. 参考文档