《JavaScript设计模式与开发实践》读书笔记之观察者模式
2015-07-16 22:47
791 查看
1.观察者模式
观察者模式定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知。JavaScript中通常采用事件模型替代传统的观察者模式
1.1 逐步实现观察者模式
以客户看房为例首先指定谁充当发布者,如售楼处
然后给发布者添加一个缓存列表,用于存放回调函数以便通知订阅者。这里为了让订阅者只接收自己感兴趣的消息,增加一个标识key
最后发布消息时候,发布者遍历缓存列表,依次触发里面存放的订阅者的回调函数
var salesOffices={}; salesOffices.clientList=[]; salesOffices.listen=function(key,fn){ if(!this.clientList[key]){ this.clientList[key]=[]; } this.clientList[key].push(fn); } salesOffices.trigger=function(){ var key=Array.prototype.shift.call(arguments), fns=this.clientList[key]; if(!fns || fns.length === 0){ return false; } for(var i=0,fn;fn=fns[i++];){ fn.apply(this,arguments); } } salesOffices.listen('squareMeter88',function(price){//A订阅88平房子消息 console.log('价格='+price); }); salesOffices.listen('squareMeter100',function(price)){//B订阅100平房子消息 console.log('价格='+price); } salesOffices.trigger('squareMeter88',200000); salesOffices.trigger('squareMeter100',300000);
1.2 观察者模式通用实现
var Event=(function(){ var clientList={}, listen, trigger, remove; listen=function(key,fn){ f(!this.clientList[key]){ this.clientList[key]=[]; } this.clientList[key].push(fn); }; trigger=function(){ var key=Array.prototype.shift.call(arguments), fns=this.clientList[key]; if(!fns || fns.length === 0){ return false; } for(var i=0,fn;fn=fns[i++];){ fn.apply(this,arguments); } }; remove=function(key,fn){ var fns=clientList[key]; if(!fns){ return false; } if(!fn){ fns&&(fns.length=0);//没指定fn时,删除全部 }else{ for(var l=fns.length-1;l>=0;l--){ var _fn=fns[l]; if(_fn === fn){ fns.splice(l,1); } } } }; return{ listen:listen, trigger:trigger, remove:remove } })(); Event.listen('squareMeter88',function(price){//A订阅88平房子消息 console.log('价格='+price); }); Event.trigger('squareMeter88',200000);
相关文章推荐
- 深入理解JavaScript系列(47):对象创建模式(上篇)
- 用Javascript评估用户输入密码的强度(Knockout版)
- js阻止a链接
- JS的面向对象的基础
- JS实现点击跳转登陆邮箱
- 13个js图表插件
- javascript日历控件
- JS相关方法总计
- 关于JavaScript刷新一次页面的代码块
- 如何直接强制客户端刷新.js文件
- 如何将json数据传回前台
- 初步探索jackson对json格式处理
- 怎样判断js对象的类型
- Top 10:HTML5、JavaScript 3D游戏引擎和框架
- 黑马day19 javaScript基本语法
- JavaScript Document
- Add listitem with javascript
- Add listitem with javascript 分类: Sharepoint 2015-07-16 20:23 4人阅读 评论(0) 收藏
- 【JavaScript 11—应用总结】:下拉菜单
- 【JavaScript 11—应用总结】:下拉菜单