What is the Publish subscription mode (Observer mode)?
definition: defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated.
Main Solution: an object state changes to other object notification issues, and to consider the ease of use and low coupling, to ensure a high degree of collaboration.
when to use: the state of an object (the target object) changes, and all dependent objects (the Observer object) are notified of the broadcast notification.
How to Solve: using object-oriented technology, you can weaken this dependency.
Key code: for a topci with an array to store subscribers.
Application Example: 1, when the auction, the auctioneer observed the highest price, and then notify other bidders to bid. 2, Journey to the monkey inside Wu empty request Bodhisattva surrender Red Baby, Bodhisattva sprinkle a water to invite an old turtle, this turtle is the observer, he observed Bodhisattva watering this action.
Advantages: 1, the observer and the observer are abstract coupled. 2, set up a set of trigger mechanism.
Cons: 1, if an observer object has a lot of direct and indirect observers, all observers are notified to spend a lot of time. 2. If there is a cyclic dependency between the observer and the observation target, observing the target triggers a cyclic call between them, which can cause the system to crash. 3. The observer pattern does not have a mechanism for the observer to know how the target object is changing, but only to know that the observation target has changed.
Usage scenarios:
- An abstract model has two facets, one of which relies on another. These aspects are encapsulated in separate objects so that they can be individually changed and reused.
- The change of one object will cause the other one or more objects to change, without knowing how many objects will change, reducing the degree of coupling between the objects.
- An object must notify other objects without knowing who the objects are.
- A trigger chain needs to be created in the system, the behavior of the A object will affect the B object, and the behavior of the B object will affect the C object ..., you can use the observer pattern to create a chain-triggered mechanism.
Precautions : 1, avoid circular references. 2, if the sequence of execution, an observer error will cause the system to jam, generally using asynchronous mode.
Code implementation
/** Publish-Subscribe mode (viewer mode)*/functionpubsub () {var_pubsub = {},//Global object, which is the publication subscription object_topics = {},//array to which the callback function is stored_subuid = 0; //Publish Method_pubsub.publish =function(topic) {if(!_topics[topic]) { return false; } varargs = [].slice.call (arguments, 1); SetTimeout (function () { varSubscribers =_topics[topic]; for(vari = 0, j = subscribers.length; I < J; i++) {subscribers[i].callback.apply (NULL, args); } }, 0); return true; }; //Subscription Method_pubsub.subscribe =function(topic, callback) {if(!_topics[topic]) {_topics[topic]= []; } varToken = (+ +_subuid). toString (); _topics[topic].push ({token:token, callback:callback}); returntoken; }; //Unsubscribe Method_pubsub.unsubscribe =function(token) { for(varMinch_topics) { if(_topics[m]) { for(vari = 0, j = _topics[m].length; I < J; i++) { if(_topics[m][i].token = = =token) {_topics[m].splice (i,1); returntoken; } } } } return false; }; return{subscribe: _pubsub.subscribe, publish: _pubsub.publish, unsubscribe: _pubsub.unsubscribe}}
JS design mode (5) Publish subscription mode