中介者模式

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

中介者模式

例子一 泡泡堂游戏

  function Player(name){
            this.name = name;
            this.enemy = null;
        }

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

        var play1 = new Player('皮蛋');
        var play2 = new Player('盐蛋');

        play1.enemy = play2;

        play1.die();// 玩家一被炸死的时候,调用这一句就ok了

下面为游戏增加队伍

  function Player(name,teamColor){
            this.parteners = [];
            this.enemies = [];
            this.state = 'live';
            this.name = name;
            this.teamColor = teamColor;
        }

        Player.prototype.win = function(){
            console.log(this.name + ' won ');
        }
        Player.prototype.lose = function(){
            console.log(this.name + ' lose ');
        }
        Player.prototype.die = function(){
            var all_dead = true;
            this.state = 'dead';
            for(var i=0,partener;partener = this.parteners[i++];){
                if(partener.state !== 'dead'){
                    all_dead = false;
                    break;
                }
            }

            if(all_dead === true){
                this.lose();
                for(var i = 0,partener;partener = this.parteners[i++];){
                    partener.lose();
                }
                for(var i = 0,enemy; enemy = this.enemies[i++];){
                    enemy.win();
                }
            }

        }

        var playerFactory = function(name.teamColor){
            var newPlayer = new Player(name,teamColor);

            for(var i = 0,player; player = players[i++];){
                if(player.teamColor === newPlayer.teamColor){
                    player.parteners.push(newPlayer);
                    newPlayer.parteners.push(player);
                }else{
                    player.enemies.push(newPlayer);
                    newPlayer.enemies.push(player);
                }
            }

        }

在上面的代码中,每个玩家和其他玩家都是紧耦合在一起的,每个玩家的状态发生变化,都要通知其他的对象。

下面使用中介者模式改造上面的代码

 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,color);// 给中介者发送消息,玩家换队
       };

       var placeFactory = function(name,teamColor){
           var newPlayer = new Player(name,teamColor); // 创建一个新的玩家对象
           playerDirector.ReceiveMessage('addPlayer'newPlayer);
           return newPlayer;
       }

你可能感兴趣的:(中介者模式)