// 定义中介者对象 var mediator = { // 存储对象 objects: [], // 注册对象 register: function(object) { this.objects.push(object); }, // 发送消息 send: function(message, sender) { for (var i = 0; i < this.objects.length; i++) { if (this.objects[i] !== sender) { this.objects[i].receive(message); } } } }; // 定义对象A var objectA = { receive: function(message) { console.log('Object A received message: ' + message); } }; // 定义对象B var objectB = { receive: function(message) { console.log('Object B received message: ' + message); } }; // 注册对象到中介者 mediator.register(objectA); mediator.register(objectB); // 发送消息 mediator.send('Hello, world!', objectA);
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com