js实现中介者模式
来源:互联网 发布:socket网络编程视频 编辑:程序博客网 时间:2024/06/05 05:26
中介者模式主要用于一个系统中存在大量的对象,而且这些大量的对象需要互相通信,因为两个对象需要通信,一个对象必须要持有另一个对象,这样就会导致,系统里,每个对象都互相引用,会引起混乱,中介者把所有的对象都统一管理起来,其他的对象通过中介者去和别的对象通信。
function A(mediator) { this.mediator = mediator;}A.prototype = { send: function(msg,receiver) { this.mediator.send(msg,'A',receiver); }, receiveMsg: function(msg,sender) { console.log(sender+" say:"+msg) }}function B(mediator) { this.mediator = mediator;}B.prototype = { send: function(msg,receiver) { this.mediator.send(msg,'B',receiver); }, receiveMsg: function(msg,sender) { console.log(sender+" say:"+msg) }}function Mediator() { this.A = new A(this); this.B = new B(this);}Mediator.prototype = { send: function(msg,sender,receiver) { try { this[receiver].receiveMsg(msg,sender); } catch(err) { console.log('receiver '+receiver+' is not exsit'); this[sender].receiveMsg('receiver '+ receiver +' is not exsit','mediator'); } }}var _mediator = new Mediator();var _a = new A(_mediator);var _b = new B(_mediator);_a.send('hello i am A','B');_b.send('hello i am B','A');
其中A,B可以抽象出一个公共的父类,这要看A,B有没有共同的地方,Mediator还可以根据对象间的通信的,类似于路由器或交换机一样,中介者需要知道通信者的发起者和接收者,就像路由器需要源ip和目的ip一样,不需要事先把方法写死,因为对象太多的时候,这种方法会导致中介者过于庞大,中介者应该抽象出对象间通信的各种功能,然后把接口对对象开放。
0 0
- js实现中介者模式
- js-中介者模式-demo
- C++实现中介者模式
- 中介者模式 C++ 实现
- 中介者模式lua实现
- 中介者模式----C++实现
- 中介者模式-C++实现
- 中介者模式(java实现)
- JS设计模式之中介者模式
- 设计模式C++实现--中介者模式
- JS——Mediator(中介者)模式
- js之中介模式
- 中介者模式 Go语言实现
- 中介者模式(C++实现)
- 中介者模式的c++代码实现
- Java 实现中介者(Mediator)模式
- php,java实现中介者模式
- JS设计模式——中介者模式
- 69道Spring面试题和答案
- qt QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout. 错误
- [Leetcode] 17. Letter Combinations of a Phone Number
- [nginx] location定位
- 矩形内接任意多边形抠图(可以羽化边缘)
- js实现中介者模式
- android 几种单例模式的写法
- 随笔
- JVM参数配置大全
- java基本语法之 javadoc02
- JAVA操作Excel时文字自适应单元格的宽度设置方法
- 状态机思路在程序设计中的应用
- 使用curl进行模拟登录
- 栈