观察者模式

1. 介绍

  • 发布者与订阅者是多对多的方式
  • 通过推与拉获取数据:发布者推送到订阅者或订阅者到发布者那边拉
  • 使并行开发的多个实现能彼此独立地进行修改
  • 其实我们在前端开发中使用到的时间监听就是浏览器实现的观察者模式

2. 示例:订阅报纸

function Publisher() {
this.subscribers = [];
}
Publisher.prototype.deliver = function(data) {
this.subscribers.forEach(function(fn) {
fn(data)
})
return this;
} Function.prototype.subscribe = function(publisher) {
var that = this;
var aleradyExists = publisher.subscribers.some(function(el) {
return el === that;
});
if(!aleradyExists) {
publisher.subscribers.push(this);
}
return this;
} Function.prototype.unsubscribe = function(publisher) {
var that = this;
publisher.subscribers = publisher.subscribers.filter(function(el) {
return el !== that;
})
return this
} var NewYoukTimes = new Publisher;
var SfChronicle = new Publisher; var Joe = function(from) {
console.log('Delivery from ' + from + ' to Joe')
} var Same = function(from) {
console.log('Delivery from ' + from + ' to Same')
} // usage // 订阅
Joe.subscribe(NewYoukTimes).subscribe(SfChronicle)
Same.subscribe(NewYoukTimes) // 发布
NewYoukTimes.deliver('aa').deliver('cc')
SfChronicle.deliver('dd') // 取消订阅
Joe.unsubscribe(NewYoukTimes)

3. 示例:动画

里面有个 Animation 类提供了三个可以被订阅的观察对象 onStart、onComplete、onTween,有点类似于浏览器提供的事件监听,只是浏览器提供的是 onclick、onchange 等。由 Animation 内部决定这三个观察对象什么时候发布信息,其他的订阅者只要等待被触发就行。

<style>
#img {
margin-top: 20px;
}
</style> <body>
<button id="startBt">开始</button>
<button id="stopBt">停止</button>
<br/>
<img id="img" src="./images/fly1.png">
</body> <script>
Function.prototype.method = function(name, fn) {
this.prototype[name] = fn
return this
} function Publisher() {
this.subscribers = [];
}
Publisher.prototype.deliver = function(data) {
this.subscribers.forEach(function(fn) {
fn(data)
})
return this;
} Function.prototype.subscribe = function(publisher) {
var that = this;
var aleradyExists = publisher.subscribers.some(function(el) {
return el === that;
});
if(!aleradyExists) {
publisher.subscribers.push(this);
}
return this;
} Function.prototype.unsubscribe = function(publisher) {
var that = this;
publisher.subscribers = publisher.subscribers.filter(function(el) {
return el !== that;
})
return this
} var Animation = function(o) {
this.config = o
this.onStart = new Publisher // 可观察对象
this.onComplete = new Publisher // 可观察对象
this.onTween = new Publisher // 可观察对象
this.timer = undefined;
this.frame = 1;
this.state = 'stop'
};
Animation.method('start', function(){
if(this.state !== 'stop') {
return
}
this.state = 'start'
this.onComplete.deliver('start at: ' + new Date().getTime())
var that = this
this.timer = setInterval(function(){
that.onTween.deliver(that.frame)
that.frame++;
},this.config.timeLate);
}).method('stop', function() {
if(this.state !== 'start') {
return
}
this.state = 'stop'
clearInterval(this.timer);
// this.frame = 1;
this.onComplete.deliver('stop at: ' + new Date().getTime())
}) var animation = new Animation({ timeLate: 400}) // 超人飞行部分动画
var frameImg = document.getElementById('img')
var jumpToFly = function(info) { // 订阅者
console.log(info)
}
var superManFlying = function(i) {
var frame = i%3 + 1;
frameImg.src = "./images/fly" + frame + ".png"
console.log('frame: ' + i) // 订阅者
};
var downToLand = function(info) { // 订阅者
console.log(info)
}
jumpToFly.subscribe(animation.onStart)
superManFlying.subscribe(animation.onTween)
downToLand.subscribe(animation.onComplete) document.getElementById('startBt').addEventListener('click', function() {
animation.start();
})
document.getElementById('stopBt').addEventListener('click', function() {
animation.stop()
}) </script>

4. 观察者的适用场合

  • 希望把人的行为和应用程序的行为分开

5. 观察者模式的利与弊

  • 利:

    • 在基于行为的大型应用程序中,可能会连续的发生几十、几百、几千次各种事件,通过观察者模式可以削减为事件注册注册监听器的次数,让可观察对象借助一个事件监听器替你处理各种行为并将信息委托给他的订阅者,从而减低内存消耗和互动性能,这样有利于减少系统开销并提高程序的可维护性
  • 弊:
    • 创建可观察对象所带来的加载事件开销(可以通过惰性加载技术化解,具体而言就是把新的可观察对象的实例化推迟到需要发送事件通知的时候,这样一来,订阅者在事件尚未创建的时候就能订阅他,而程序的初始化时间也就不收影响)

注意

转载、引用,但请标明作者和原文地址

JavaScript设计模式(10)-观察者模式的更多相关文章

  1. JavaScript设计模式之观察者模式(学习笔记)

    设计模式(Design Pattern)对于软件开发来说其重要性不言而喻,代码可复用.可维护.可扩展一直都是软件工程中的追求!对于我一个学javascript的人来说,理解设计模式似乎有些困难,对仅切 ...

  2. 再起航,我的学习笔记之JavaScript设计模式18(观察者模式)

    观察者模式 观察者模式(Observer): 又被称为发布-订阅者模式或消息机制,定义了一种依赖关系,解决了主体对象与观察者之间功能的耦合. 创建一个观察者对象 首先我们创建一个闭包对象,让其在页面加 ...

  3. 再起航,我的学习笔记之JavaScript设计模式10(单例模式)

    单例模式 单例模式(Singleton) : 又被称为单体模式,是只允许实例化一次的对象类.一个类有且仅有一个实例,并且自行实例化向整个系统提供. 命名空间 单例模式可能是JavaScript中我们最 ...

  4. JavaScript设计模式-22.观察者模式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. javascript设计模式之观察者模式

    观察者模式又称发布/订阅模式   publish/subscribe 它是一种一对多的关系,让多个观察者对象同时监听某一主题对象,这个主题对象的状态发生变化时就会通知所有的观察者对象,使得他们能够自动 ...

  6. javascript 设计模式之观察者模式

    观察者模式又叫发布——订阅模式,顾名思义pub——sub就是被动触发的,:不要给我......,我会给你.......就是一个发布订阅的解释,实质就是对程序中的某个对象状态进行监听观察,并且在该对象发 ...

  7. JavaScript设计模式-10.工厂模式实例xhr

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. JavaScript设计模式与开发实践 - 观察者模式

    概述 观察者模式又叫发布 - 订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个目标对象(为了方便理解,以下将观察者对象叫做订阅者,将目标对象叫做 ...

  9. javaScript 设计模式系列之一:观察者模式

    介绍 观察者模式又叫发布订阅模式(Publish/Subscribe),一个目标对象管理所有相依于它的观察者对象.该模式中存在两个角色:观察者和被观察者.目标对象与观察者之间的抽象耦合关系能够单独扩展 ...

随机推荐

  1. HDU 4333 [SAM WRONG!!!]

    题意:给一个数字,每一次把它的最后一位拿到最前面,一直那样下去,分别求形成的数字小于,等于和大于原来数的个数. SAM上就是走n步 相等好好做啊,但是大于小于不好做啊,用类似弦论的思想也不能处理出怎样 ...

  2. POJ 3608 Bridge Across Islands [旋转卡壳]

    Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10455   Accepted: ...

  3. poj1265&&2954 [皮克定理 格点多边形]【学习笔记】

    Q:皮克定理这种一句话的东西为什么还要写学习笔记啊? A:多好玩啊... PS:除了蓝色字体之外都是废话啊...  Part I 1.顶点全在格点上的多边形叫做格点多边形(坐标全是整数) 2.维基百科 ...

  4. SDN第三次作业

    作业链接 阅读文章:http://www.sdnlab.com/19777.html 阅读<重构网络>第一二章 列举openflow1.0的12元组? 入端口 源MAC地址 目的MAC地址 ...

  5. linear-grident的属性和使用以及对颜色后面参数(百分比)的理解

    linear-grident的属性和使用   css3新增Gradient属性,用来增加渐变的效果,渐变分为线性渐变 linear-grident 和 径向渐变 radial-grident,这篇文章 ...

  6. git命令行工作的正确姿势

    git命令行创建并提交新分支到mater分支的常规步骤 git branch new_branch git status 查看修改的文件 git add changed_files git commi ...

  7. java-redis字符类数据操作示例(一)

    对于大部分程序猿来讲,学习新知识重在编码实践,于我也是这样.现在初识redis,一直看文章难免感觉是浮光掠影,印象不深.所以间隙中,将自己的测试代码整理成博客,旨在加深记忆并提醒自己对待编程要用心沉下 ...

  8. Python——Web.py詳解

    ubuntu安裝Web.py sudo pip install web.py 測試代碼: import web urls = ( '/(.*)','hello' ) app = web.applica ...

  9. nyoj886 取石子(八) 威佐夫博弈

    好累,坐了一天火车, 终于到学校了. 思路:仔细观察威佐夫博弈,发现P态的所有数字都是不重复的,例如(0,0).(1,2).(3,5).(4,7).(6, 10).(8,13).(9,15).(11, ...

  10. OPENCV 旋转图像算法-汇总

      void ImgRotate(cv::Mat imgIn, float theta, cv::Mat& imgOut) { int oldWidth = imgIn.cols; int o ...