中介者模式

优点:解除对象与对象之间的紧耦合关系。增加一个中介者对象,让所有对象都通过中介者对象来通信。

规则:结构: 最少知识原则,指一个对象应该尽可能少地了解另外的对象。如果对象耦合性太高,一个对象发生变化后,难免会影响到其他对象,“城门失火,殃及池鱼”。

而中介者里,对象几乎不知道彼此的存在。

创建中介者
1.通过中介者开放一些接收消息的接口,各个对象可以通过该接口来给中介者发送消息,中介者处理后通知到其他对象。
2.也可以通过发布-订阅方式。

中介者:

var playerDirector = (function(){
	var players = {},   //所有玩家
		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;
		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 = newTeamColor;
		operations.addPlayer( player );
	};
	//玩家死亡
	operations.playerDead = function( player ){
		var teamColor = player.teamColor,
			teamPlayers = players[ teamColor ];
		var all_dead = true;
		for( var i= 0, player; player = teamPlayers[ i++ ];){
			if( player.state != 'dead'){
				all_dead = false;
				break;
			}
		}
		if( all_dead === true ){
			for( var i= 0, player; player = teamPlayers[ i++ ];){
				player.lose();
			}
		}
		for( var color in players ){
			if( color !== teamColor ){
				var teamPlayers = players[ color ];
				for( var i= 0, player; player = teamPlayers[ i++ ];){
					player.win();
				}
			}
		}
	};
	var ReceiveMessage = function(){
		var message = Array.prototype.shift.call( arguments );
		operations[ message ].apply( this, arguments );
	}
	return {
		ReceiveMessage: ReceiveMessage
	}
})()

  

各玩家:

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 + 'lose');
}

Player.prototype.dead = 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, color );

}

  

通过中介者创建玩家,通知玩家

var playFactory = function( name, teamColor ){
	var newPlayer = new Player( name, teamColor );
	playerDirector.ReceiveMessage('addPlayer', newPlayer);   //通知到中介者

	return newPlayer;
}

  

 

posted on 2017-06-28 16:06  rainbow661314  阅读(109)  评论(0编辑  收藏  举报