javascript设计模式-----观察者模式

一、观察者模式

javascript中所有的事件应用了此模式,此模式大大降低对象间的耦合,并且易于扩展。简单来说,其实现思路就是函数回调,由订阅者提供对应事件的处理函数,当发布者发布消息,就会调用事件处理队列中的函数。静态语言,用接口来实现不同事件和不同处理函数的对应。javascript动态语言直接用hash表,和函数传递。

实现:

			var publisher = {
				listeners:{
					'any':[]//事件类型:订阅者
				},
				//注册
				addListener:function(fn,type){
					type  = type || 'any';
					if(typeof this.listeners[type] === 'undefined'){
						this.listeners[type] = [];
					}
					this.listeners[type].push(fn);
				},
				//取消注册
				remove:function(type,fn){
					this.visitlisteners('remove',fn,type);
				},
				//发布信息
				publish:function(publication,type){
					this.visitlisteners('publish',publication,type);
				},
				//遍历某事件的订阅者
				visitlisteners:function(action,arg,type){
					var pubtype = type || 'any',
						listeners = this.listeners[pubtype],
						i,
						max = listeners.length;
						
					for (i=0;i



1.有一个'any'的事件类型,用于处理没有对应事件名字的事件函数。

2.此实现可以将任意对象变成发布者,利用makePublisher()函数。


你可能感兴趣的:(javascript,javascript,设计模式,观察者模式)