博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
发布/订阅模式 vs 观察者模式
阅读量:3889 次
发布时间:2019-05-23

本文共 3316 字,大约阅读时间需要 11 分钟。

1.发布订阅

var publisherSubscriber = {
};// we send in a container object which will handle the subscriptions and publishings(function(container) {
// the id represents a unique subscription id to a topic var id = 0; // we subscribe to a specific topic by sending in // a callback function to be executed on event firing container.subscribe = function(topic, f) {
if (!(topic in container)) {
container[topic] = []; } container[topic].push({
"id": ++id, "callback": f }); return id; } // each subscription has its own unique ID, which we use // to remove a subscriber from a certain topic container.unsubscribe = function(topic, id) {
var subscribers = []; for (var subscriber of container[topic]) {
if (subscriber.id !== id) {
subscribers.push(subscriber); } } container[topic] = subscribers; } container.publish = function(topic, data) {
for (var subscriber of container[topic]) {
// when executing a callback, it is usually helpful to read // the documentation to know which arguments will be // passed to our callbacks by the object firing the event subscriber.callback(data); } }})(publisherSubscriber);

2.观察者模式

  • 什么是观察者模式
    一种消息机制,解耦多个模块相互依赖,提供一种通信机制。
// 发布订阅模式        const pubSub = (() => {
// 这里是存储事件和其对应得回调函数 const subTopics = {
}; // 订阅一个事件的时候,如果该事件不存在,那怎么办? // 那就注册该事件,并且传入回调函数 function subscrible (topic, callback) {
if (!subTopics[topic]) {
subTopics[topic] = []; } subTopics[topic].push(callback); } function publish (topic, ...args) {
if (subTopics[topic].length) {
for (let fnc of subTopics[topic]) {
fnc(...args); } } } return {
publish, subscrible } })() pubSub.subscrible('test', (a, b) => {
console.log(( a + b )); }) pubSub.subscrible('test', (a, b) => {
console.log(( a - b )); }) pubSub.publish('test', 1, 2); // 观察者模式 // 目标主体 class Subject {
constructor () {
this.sub = []; } addSub (ob) {
this.sub.push(ob); } notify () {
for (let ob of this.sub) {
ob.update(); } } } // 观察者1 class Observer1 {
update () {
console.log('update1'); } } // 观察者2 class Observer2 {
update () {
console.log('update2'); } } const sub = new Subject(); const ob1 = new Observer1(); const ob2 = new Observer2(); sub.addSub(ob1); sub.addSub(ob2); sub.notify();

3.二者的区别

  • 发布订阅模式中,发布者订阅者是靠第三方来完成事件通信的。发布者发布事件到第三方,订阅者从第三方订阅事件。但是,观察者模式中,被观察者是直接触及观察者的
    在这里插入图片描述

转载地址:http://fgthn.baihongyu.com/

你可能感兴趣的文章
Windows 7 下登录界面里 Ctrl + Alt + Del 无法使用
查看>>
惠山赏菊 & 梅园赏桂
查看>>
[小技巧] cat /proc/modules 显示的地址为 0
查看>>
[游戏] chrome 的小彩蛋
查看>>
napi
查看>>
_GNU_SOURCE和__USE_GNU的差别
查看>>
Linux 有了 “DTrace”
查看>>
Linux 系统中僵尸进程
查看>>
一个 2 年 Android 开发者的 18 条忠告
查看>>
标志性文本编辑器 Vim 迎来其 25 周年纪念日
查看>>
[小技巧] chrome 的 vim 插件
查看>>
在 Linux 中查看你的时区
查看>>
[小技巧] [trac] Fix AttributeError: 'NullTranslations' object has no attribute 'add'
查看>>
[小技巧] Mac OS X上键盘的键位重映射
查看>>
Java对Oracle中Clob类型数据的读取和写入
查看>>
Spring中Quartz的配置
查看>>
MyBatis 防止 % _ sql 注入攻击 解决方法
查看>>
plsql oracle 无法解析指定的连接标识符
查看>>
Linux后台开发应该具备技能
查看>>
Eclipse Tomcat 无法添加项目
查看>>