zoukankan      html  css  js  c++  java
  • 6.js模式-中介者模式

    1. 中介者模式

    所有对象通过中介者进行通信

    var playDirector = (function(){

          var players = [];

          var options = {};

         

          options.add = function(p){

                 players.push(p);

          }

         

          options.die = function(p){

                 for(var i = 0;i<players.length;i++){

                        if(players[i] === p){

                               players.splice(i,1);

                        }else{

                               players[i].die('die');

                        }

                 }

          }

         

          var receiveMsg = function(){

                 var key = Array.prototype.shift.call(arguments);

                

                 options[key].apply(this,arguments);

          }

         

          return {

                 receiveMsg:receiveMsg

          }

    })();

    function play(name){

          this.name = name;

          this.state = 'alive';

    }

    play.prototype.die = function(){

          this.state = 'die';

          playDirector.receiveMsg('die',this);

    }

    play.prototype.receive = function(msg){

          console.log(msg);

    }

  • 相关阅读:
    行政区划遮罩 -Leaflet
    WebGIS vs WebGL图形编程
    oracle 中 group by 加强
    Nagios监控服务搭建
    MySQL触发器使用详解
    mybatis--一对多关联
    mybatis--一对一关联查询
    mybatis--实现数据库增删改查
    mybatis--使用接口注解的方式实现Helloword
    mybatis--第一个mybatis程序
  • 原文地址:https://www.cnblogs.com/SLchuck/p/4869716.html
Copyright © 2011-2022 走看看