Home >Web Front-end >JS Tutorial >Multiple implementation examples of Javascript design pattern Observer pattern_javascript skills
Introduction
The observer mode is also called the publish/subscribe mode (Publish/Subscribe). It defines a one-to-many relationship, allowing multiple observer objects to monitor a certain topic object at the same time. When the status of the topic object changes, All observer objects will be notified so that they can update themselves automatically.
Benefits of using the observer pattern:
1. Support simple broadcast communication and automatically notify all subscribed objects.
2. After the page is loaded, the target object can easily have a dynamic relationship with the observer, which increases flexibility.
3. The abstract coupling relationship between the target object and the observer can be independently extended and reused.
Text (version 1)
The implementation of the observer pattern in JS is achieved through callbacks. Let's first define a pubsub object, which contains 3 methods: subscribe, unsubscribe, and publish.
Use as follows:
How about it? Isn’t it great to use? But there is a problem with this method, that is, there is no way to unsubscribe. If you want to unsubscribe, you must specify the name of the unsubscription, so let’s come up with another version:
Version 2
We can also use the characteristics of prototypes to implement an observer pattern. The code is as follows:
If you are prompted that the filter or forEach function cannot be found, it may be because your browser is not new enough and currently does not support the new standard functions. You can define it yourself using the following method:
Version 3
If we want multiple objects to have the observer publish and subscribe function, we can define a general function, and then apply the function of this function to the objects that need the observer function. The code is as follows:
Then subscribe to two objects blogger and user, and use the observer.make method to make these two objects have observer functions. The code is as follows:
The usage method is relatively simple. Subscribe to different callback functions so that you can register to different observer objects (you can also register to multiple observer objects at the same time):
jQuery version
According to the new on/off function of jQuery version 1.7, we can also define jQuery version of observers:
The calling method is simpler than the above three versions:
As you can see, his subscription and unsubscription use string names instead of callback function names, so even if an anonymous function is passed in, we can still unsubscribe.
Summary
The use case of observers is: when changes to one object require changes to other objects at the same time, and it does not know how many objects need to be changed, you should consider using the observer pattern.
In general, what the observer pattern does is decoupling, making both parties of the coupling rely on abstraction rather than concreteness. So that changes on each side will not affect changes on the other side.