The difference between Viewer Mode and publish / subscribe model

Reprinted from:

When people read the information, the observer (Observer) model is equivalent to (Publish) publish / subscribe (Subscribe) mode, some people think that there are still differences between the two modes, and I think there is a difference, the essential difference is not the same place of dispatch. The
observer model, the
comparison concept, explains that the target and observer are the base classes, the target provides a series of methods for maintaining the observer, and the observer provides update interfaces. The specific observer and the specific target inherit their base class, then the specific observer registers himself into the specific target, and updates the observer’s updating method when the specific target changes. The specific target A
such as a “weather center”, specifically to monitor changes in the weather, but there is a weather display interface observer B, B took his registration to A, when the A trigger changes in the weather, B scheduling update method, and bring their own context.

The difference between Viewer Mode and publish / subscribe model

Publish / subscribe model
concept explanation is that their subscribers want to subscribe event registration to the dispatch center, when the event is triggered when publishing the event to the dispatching center (by the context), registered by the dispatch center unified dispatch subscriber to the dispatch center handling code. For example, a
interface is a real-time display of the weather, it will subscribe to weather events (registration to the dispatch center, including processing program), when the weather changes (timing data), as publishers of weather information to the dispatch center, the weather process of scheduling the subscriber of the dispatch center.

The difference between Viewer Mode and publish / subscribe model


  1. As you can see from the two picture, the biggest difference is the place of dispatch. Although
    two models have publishers and subscribers (divergers is considered to be a subscriber, specific goals can be considered to be the publisher), but the observer pattern is scheduled by the specific objectives, and publish / subscribe model is unified by the dispatch center adjustment, so between the observer pattern and the publisher is the subscriber the existence of dependent, and will not publish / subscribe model.
  2. Both of the two patterns can be used for loose coupling, improving code management and potential reuse.
    viewer mode implementation code (JavaScript version):
/ / observer list function () {this.observerList ObserverList = [];} ObserverList.prototype.add {return = function (obj) 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) {i++}}; return I; return -1;}; ObserverList.prototype.removeAt = function (index) {this.observerList.splice (index, 1);}; function (Subject) {/ / target This.observers = new (ObserverList);} Subject.prototype.addObserver {this.observers.add = function (observer) (observer);}; Subject.prototype.removeObserver = function (observer) {this.observers.removeAt (this.observers.indexOf (observer, 0));}; Subject.prototype.notify = function (context) {var = this.observers.count (observerCount); for (VaR i=0; I < observerCount; i++) {this.observers.get (I).Update (context);}}; / / function Observer (observer) this.update (function) = {{};} / /...

Publish / subscribe mode implementation code (JavaScript classic):

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; to events of; / / Subscribe / / with interest a specific topic name and a / / callback function. To be e Xecuted when the topic/event is observed / / myObject.subscribe = function (topic, func) {if (topics[topic]!) [] = {topics[topic]}; VAR token = (++subUid) (.ToString); topics[topic].push (token: func: func {token}); return token;}; / / Unsubscribe / / topic from a specific, based on a tokenized reference the subscription myObject.unsubscribe / / to = 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);