美文网首页设计模式Java学习笔记
观察者模式与发布/订阅模式区别

观察者模式与发布/订阅模式区别

作者: MentallyL | 来源:发表于2017-08-30 10:39 被阅读218次

转载自:http://www.cnblogs.com/lovesong/p/5272752.html

在翻阅资料的时候,有人把观察者(Observer)模式等同于发布(Publish)/订阅(Subscribe)模式,也有人认为这两种模式还是存在差异,而我认为确实是存在差异的,本质上的区别是调度的地方不同。
观察者模式
比较概念的解释是,目标和观察者是基类,目标提供维护观察者的一系列方法,观察者提供更新接口。具体观察者和具体目标继承各自的基类,然后具体观察者把自己注册到具体目标里,在具体目标发生变化时候,调度观察者的更新方法。
比如有个“天气中心”的具体目标A,专门监听天气变化,而有个显示天气的界面的观察者B,B就把自己注册到A里,当A触发天气变化,就调度B的更新方法,并带上自己的上下文。

发布/订阅模式
比较概念的解释是,订阅者把自己想订阅的事件注册到调度中心,当该事件触发时候,发布者发布该事件到调度中心(顺带上下文),由调度中心统一调度订阅者注册到调度中心的处理代码。
比如有个界面是实时显示天气,它就订阅天气事件(注册到调度中心,包括处理程序),当天气变化时(定时获取数据),就作为发布者发布天气信息到调度中心,调度中心就调度订阅者的天气处理程序。

总结

  1. 从两张图片可以看到,最大的区别是调度的地方。
    虽然两种模式都存在订阅者和发布者(具体观察者可认为是订阅者、具体目标可认为是发布者),但是观察者模式是由具体目标调度的,而发布/订阅模式是统一由调度中心调的,所以观察者模式的订阅者与发布者之间是存在依赖的,而发布/订阅模式则不会。
  2. 两种模式都可以用于松散耦合,改进代码管理和潜在的复用。
    附录
    观察者模式实现代码(JavaScript版):
//观察者列表
function ObserverList(){
  this.observerList = [];
}
ObserverList.prototype.add = function( obj ){
  return this.observerList.push( obj );
};
ObserverList.prototype.count = function(){
  return this.observerList.length;
};
ObserverList.prototype.get = function( index ){
  if( index > -1 && index < this.observerList.length ){
    return this.observerList[ index ];
  }
};
ObserverList.prototype.indexOf = function( obj, startIndex ){
  var i = startIndex;
  while( i < this.observerList.length ){
    if( this.observerList[i] === obj ){
      return i;
    }
    i++;
  }
  return -1;
};
ObserverList.prototype.removeAt = function( index ){
  this.observerList.splice( index, 1 );
};

//目标
function Subject(){
  this.observers = new ObserverList();
}
Subject.prototype.addObserver = function( observer ){
  this.observers.add( observer );
};
Subject.prototype.removeObserver = function( observer ){
  this.observers.removeAt( this.observers.indexOf( observer, 0 ) );
};
Subject.prototype.notify = function( context ){
  var observerCount = this.observers.count();
  for(var i=0; i < observerCount; i++){
    this.observers.get(i).update( context );
  }
};

//观察者
function Observer(){
  this.update = function(){
    // ...
  };
}

发布/订阅模式实现代码(JavaScript经典版):

var pubsub = {};
(function(myObject) {
    // Storage for topics that can be broadcast
    // or listened to
    var topics = {};
    // An topic identifier
    var subUid = -1;
    // Publish or broadcast events of interest
    // with a specific topic name and arguments
    // such as the data to pass along
    myObject.publish = function( topic, args ) {
        if ( !topics[topic] ) {
            return false;
        }
        var subscribers = topics[topic],
            len = subscribers ? subscribers.length : 0;
        while (len--) {
            subscribers[len].func( topic, args );
        }
        return this;
    };
    // Subscribe to events of interest
    // with a specific topic name and a
    // callback function, to be executed
    // when the topic/event is observed
    myObject.subscribe = function( topic, func ) {
        if (!topics[topic]) {
            topics[topic] = [];
        }
        var token = ( ++subUid ).toString();
        topics[topic].push({
            token: token,
            func: func
        });
        return token;
    };
    // Unsubscribe from a specific
    // topic, based on a tokenized reference
    // to the subscription
    myObject.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;
    };
}( pubsub ));

相关文章

  • 中介者模式

    观察者与订阅/发布模式的区别 1.观察者模式中观察者必须对主题进行订阅后,才能收到主题的事件。 2.订阅/发布模式...

  • 【设计模式】观察者模式 和 发布订阅模式

    目录:观察者模式发布-订阅模式观察者模式和发布订阅模式的区别实现vue数据双向绑定 (1)观察者模式 (1)概念 ...

  • 从发布-订阅模式到消息队列

    发布-订阅模式 发布-订阅模式又称为观察者模式(网上也有很多说这两种模式区别,个人觉得区别不大),在发布-订阅模式...

  • JS设计模式-观察者模式

    观察者模式 包含发布订阅 与发布订阅的区别 : 发布和订阅是没有关系的,观察者模式观察者和被观察者是有关系的,观察...

  • 观察者模式和发布订阅模式的区别

    观察者模式和发布订阅模式最大的区别就是发布订阅模式有个事件调度中心。 在观察者模式中,观察者需要直接订阅目标事件;...

  • 发布订阅模式(观察者模式)

    发布订阅模式(观察者模式) 发布订阅也叫观察者模式 发布 && 订阅 使用

  • 【设计模式】---观察者模式与发布订阅者模式

    一、观察者模式和发布订阅者模式之间的区别 观察者模式中观察者和目标直接进行交互, 而发布订阅模式由统一的调度中心来...

  • 观察者模式和订阅发布模式

    观察者模式和发布/订阅模式的区别是: ,最大的区别是调度的地方。虽然两种模式都存在订阅者和发布者(具体观察者可认为...

  • 观察者模式

    观察者模式 (等同于发布(publisher)-订阅(Subscriber)模式,又有区别:观察者模式是知道Sub...

  • 面试必会2

    观察者模式与发布/订阅模式区别: 从两张图片可以看到,最大的区别是调度的地方。虽然两种模式都存在订阅者和发布者(具...

网友评论

    本文标题:观察者模式与发布/订阅模式区别

    本文链接:https://www.haomeiwen.com/subject/msvzdxtx.html