js 디자인 모델 4 관찰자 모델

4361 단어
// 
  function ObserverList(){
  	this.observerList = [];
  }
  ObserverList.prototype.add = function(obj){
  	this.observerList.push(obj);
  }
  ObserverList.prototype.empty = function(){
  	this.observerList = [];
  }
  ObserverList.prototype.count = function(){
  	return this.observerList.length;
  }
  ObserverList.prototype.getByInd = function(ind){
  	if(ind>-1 && ind< this.observerList.length){
  		return this.observerList[ind];
  	}
  }
  ObserverList.prototype.indexof = function(obj){
  	var i = 0;
  	var point = -1;
  	while(i< this.observerList.length){
  		if(this.observerList[i++]===obj){
  				point = i;
  		}
  	}
  	return point;
  }
  ObserverList.prototype.remove = function(obj){
  	if(this.observerList.length ==0){
  		return;
  	}
  	for(var i=0;i< this.observerList.length;i++){
  	 if(this.observerList[i] === obj){
  	 		this.observerList.splice(i,1);
  	 		break;
  	 }	
  	}
  }
  
  // 
  function Subject(){
  	this.observers = new ObserverList();
  }
  Subject.prototype.add = function(obj){
  	this.observers.add(obj);	
  }
  Subject.prototype.remove = function(obj){
  	this.observers.remove(obj);
  }
  Subject.prototype.notify = function(context){
  	var count = this.observers.count();
  	if(count==0){
  		return;
  	}
  	for(var i=0;i<count;i++){
  		this.observers.getByInd(i).act(context);
  	}	
  }
  // 
  function Observer(str){
  		this.name = str;
  }
  Observer.prototype.act = function(context){
  	alert(this.name + context);
  }
  
  var testSubject = new Subject();
  var testObserver1 = new Observer("xiaomin");
  var testObserver2 = new Observer("xiaowang");
  var testObserver3 = new Observer("xiaozhang");
  var testObserver4 = new Observer("hehe");
  var testObserver5 = new Observer("test");
  testSubject.add(testObserver1);
  testSubject.add(testObserver2);
  testSubject.add(testObserver3);
  testSubject.add(testObserver4);
  testSubject.add(testObserver5);
 	testSubject.notify("test-----");
 	testSubject.remove(testObserver5);
 	testSubject.notify("222222");

강화 버전:
var p = {};
  (function(obj){
   	var topics = {};
   	var subUid = -1;
   	p.publish = function(topic, args){
   		if(!topics[topic]){
   			return;
   		}
   		var subcribers = topics[topic];
   		var len = subcribers?subcribers.length:0;
   		while(len--){
   			subcribers[len].func(topic,args);
   		}
   	}
   	p.subscribe = function(topic,func){
   		if(!topics[topic]){
   			topics[topic] = [];
   		}
   		var token = (++subUid).toString();
   		topics[topic].push({"token":token,"func":func});
   		return token;
   	}
   	p.unsubscribe = function(token){
   		for(var m in topics){
   				if(topics[m]){
   					for ( var i = 0, j = topics[m].length; i < j; i++ ) {
	                    if ( topics[m][i].token === token) {
	                        topics[m].splice( i, 1 );
	                        return token;
	                    }
                }
   				}
   		}
   		return this;
   	}
   })(p);
   
    var loginListener = function(topic,data){
    	alert("login****"+data);
    }
    var logoutListener = function(topic,data){
    	alert("logout****"+data);
    }
    var commentListener = function(topic,data){
    	alert("coment****"+data);
    }
    
    //test
    p.subscribe("user",loginListener);
    p.subscribe("user",logoutListener);
    p.subscribe("comment",commentListener);
    //p.publish("user",{"username":"hehe","pwd":"*****"});
    p.publish("comment","zhe shi li zi ")

좋은 웹페이지 즐겨찾기