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
* 购物车
javascript 中介者模式 mediator的更多相关文章
- 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...
- 二十四种设计模式:中介者模式(Mediator Pattern)
中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...
- 中介者模式(Mediator Pattern)
用于减少多个对象或类之间的通信复杂性. 此模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来维护代码.中介者模式属于行为模式类别. 实现实例 在这里通过一个聊天室的示例来演示中 ...
- 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- JavaScript 中介者模式与观察者模式有何不同?
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#detailmvp 感觉二者非常像,都是pub/sub机制,如何进行区分 ...
- 18.中介者模式(Mediator Pattern)
using System; namespace Test { class Program { /// <summary> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...
- 中介者模式(Mediator)
GOF:用一个中介对象来封装一系列的对象交互.中介者使对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 类图:
随机推荐
- SQL 练习36
查询不同课程成绩相同的学生的学生编号.课程编号.学生成绩 select a.cid, a.sid, a.score from sc as a,sc as b WHERE a.sid = b.sid a ...
- NOIP 模拟 $12\; \text{简单的区间}$
题解 签到题 求区间和为 \(k\) 的倍数的区间,我们可以转化为求左右两个端点,其前缀和相等 对于区间最大值,我们可以把其转化为一个值,它能向左,向右扩展的最远边界,一个单调栈即可 我们设一个值 \ ...
- 【硬件模块】RFIDSetting
The Octane SDK includes the core library by acting as a wrapper for extraction, modifying, and the a ...
- 【spring】spring 核心注解
注解具体分类如下: 1.模式注解 @Repository 数据仓储模式注解 @Component 通用组件模式注解 @Service ...
- SpringCloud bootstrap.yml 和application.yml 加载原理
Spring Cloud 官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud.html 一个Spring Cloud的操作是通过 ...
- JS对象创建的几种方法
最近一直在看JS高级程序设计这本书,有空来梳理一下几种创建对象的方式.话不多说,直接步入正题. 第一种:Object构造函数创建 var Person = new Object(); Person.n ...
- 哈希表(HashMap)分析及实现(JAVA)
转自:http://www.java3z.com/cwbwebhome/article/article8/83560.html?id=4649 探讨Hash表中的一些原理/概念,及根据这些原理/概念, ...
- redis百万级数据存取
Jedis jedis0 = new Jedis("localhost", 6379); jedis0.auth("123456"); Pipeline pip ...
- JavaScript 特殊字符
代码输出\'单引号\"双引号\&和号\\反斜杠\n换行符\r回车符\t制表符\b退格符\f换页符
- Centos7部署Open-falcon 0.2.0
官方和github上都有教程,但是对于我来说有的部署内容较为陌生,有点错误官方也未在教程中说明,故在此记录方便以后快速部署,本文部署的时间是2018/10/10. 虽然open-falcon是采用了前 ...