Last time I studied the Observer Mode, many articles said that it is also called Subscribe/Publish (Publish/Subscribe Mode). But in the book "Javascript Design Pattern", there are still some differences between the two patterns. The original words in the book are as follows:
1. Observer mode requires observers who wish to receive topic notifications must subscribe to events where content changes.
2.Subscribe/Publish mode uses a theme/event channel, which is between subscriber and publisher. The event system allows code to define specific events for the application, which can pass custom parameters that contain the values required by the subscriber. The purpose is to avoid dependencies between subscribers and publishers.
The difference from the Observer mode is that it allows any subscriber to execute appropriate event handlers to register and receive notifications from publishers.
OK, I don't know how to be so powerful. Here is my understanding:
1. In the observer mode, the target object is responsible for maintaining the observer. In the publish/subscribe mode, publishers do not care about subscribers, they are only responsible for throwing messages out and leaving them alone.
2. In the observer mode, the observer needs to provide an interface, and then call this interface when the target object changes to keep its own state and the target state consistent. That is, all observers must have a unified interface (for example, the update method written above, everyone's method must be called this name). In the publish/subscribe mode, the triggering of the subscriber event does not rely on such an interface, but is triggered by the subscriber by listening to a specific message (this message generally contains the name and parameters required by the subscriber). It can be understood that the subscriber is not listening to the publisher, but the message pool. As long as there is a message it cares about in the message pool, it triggers the event, no matter who posted the message in the past. Publishers and subscribers are decoupled.
The following is the implementation of the publish/subscribe mode in js. Copy and paste it into the console and you will understand it after trying:
The code copy is as follows:
var pubsub = (function(){
var q = {}
topics = {},
subUid = -1;
//Publish a message
q.publish = function(topic, args) {
if(!topics[topic]) {return;}
var subs = topics[topic],
len = subs.length;
while(len--) {
subs[len].func(topic, args);
}
return this;
};
//Subscribe to events
q.subscribe = function(topic, func) {
topics[topic] = topics[topic] ? topics[topic] : [];
var token = (++subUid).toString();
topics[topic].push({
token : token,
func : func
});
return token;
};
return q;
//Unsubscribe and stop writing, traverse topics, and then return token by saving the previous one, delete the specified element
})();
//Triggered event
var logmsg = function(topics, data) {
console.log("logging:" + topics + ":" + data);
}
// Listen to the specified message 'msgName'
var sub = pubsub.subscribe('msgName', logmsg);
//Posted a message 'msgName'
pubsub.publish('msgName', 'hello world');
//Posted an unlisted message 'msgName1'
pubsub.publish('anotherMsgName', 'me too!');