javascript 中介者模式 mediator

* player.js

/**
 * 中介者模式
 * @param {*} name  角色名称
 * @param {*} teamColor  队伍颜色
 */
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 playerFactory = function(name, teamColor) {
    // 创造一个新的玩家对象
    var newPlayer = new Player(name, teamColor);
    // 给中介者发送消息, 玩家换队
    playerDirector.ReceiveMessage('addPlayer', newPlayer);

    return newPlayer;
}

// 中介者对象
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);
                break;
            }
        }
    }

    /************* 玩家换队 ******************/
    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]; i++) {
            if (player.state !== 'dead') {
                all_dead = false;
                break;
            }
        }
        if (all_dead) {
            teamPlayers.forEach(function(player) {
                player.lose();
            });
            for (var color in players) {
                if (color !== teamColor) {
                    players[color].forEach(function(player) {
                        player.win();
                    });
                }
            }
        }
    }

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

    return {
        ReceiveMessage: ReceiveMessage
    }

})();

var player1 = playerFactory('皮蛋', 'red'),
    player2 = playerFactory('小怪', 'red'),
    player3 = playerFactory('宝宝', 'red'),
    player4 = playerFactory('小强', 'red');

var player5 = playerFactory('黑妞', 'blue'),
    player6 = playerFactory('葱头', 'blue'),
    player7 = playerFactory('胖墩', 'blue'),
    player8 = playerFactory('海盗', 'blue');


// player1.die(); player2.die(); player3.die(); player4.die();
// player1.remove();  player2.remove(); player3.die(); player4.die();
player1.changeTeam('blue');  player2.die(); player3.die(); player4.die();

  Run:

VM394:17 小怪 lost
VM394:17 宝宝 lost
VM394:17 小强 lost
VM394:13 黑妞 won
VM394:13 葱头 won
VM394:13 胖墩 won
VM394:13 海盗 won
VM394:13 皮蛋 won

  

* 购物车

 

posted @ 2018-08-07 10:13  zhanghui_ming  阅读(143)  评论(0编辑  收藏  举报