Observer model of java learning notes
This article shares the observer pattern of Java design pattern for your reference. The specific contents are as follows
1. Preliminary understanding
Definition of observer mode:
One to many dependencies are defined between objects. In this way, when an object changes state, the objects that depend on it will receive notification and update automatically.
vernacular:
In fact, it is the publish and subscribe mode. Publishers publish information, Subscribers get information, and you can receive information after subscribing. You can't receive information without subscribing.
2. Structure diagram of this pattern
3. As you can see, the pattern contains four roles
Abstract observer role: that is, an abstract topic. It saves all references to observer objects in a collection. Each topic can have any number of observers. Abstract topics provide an interface to add and remove observer roles. It is generally implemented with an abstract class and interface. Abstract observer role: define an interface for all concrete observers and update themselves when notified by the topic. Specific observer role: that is, a specific topic. When the internal state of the collective topic changes, all registered observers will send a notice. Concrete observer role: implement the update interface required by the abstract observer role, while coordinating its own state with the drawing state.
4. Usage scenario example
There is a WeChat official account service. It releases messages from time to time. It can receive the push message when it concerns the official account.
5. Concrete implementation of observer mode
1) Define an abstract observer interface
2) Define an abstract observer interface
3) Define the observed, implement the observable interface, and implement the three methods of the observable interface. At the same time, there is a list set to save the registered observers. When the observer needs to be notified, traverse the set.
4) define the specific observers, the WeChat public official account is the user User.
5) Write a test class
First, three users were registered, Zhang San, Li Si and Wang Wu. The official account issued a message that "PHP is the best language in the world!" All three users received the message.
User ZhangSan was shocked after the news, and decisively canceled the subscription. At that time, the official account pushed another message. At this point, the user ZhangSan had no message, and the other users.
Push messages can still be received normally.
Test results:
6. Summary
This pattern is loosely coupled. Change the subject or one of the observers, and the other will not be affected by the image. The JDK also has its own observer mode. However, the observed is a class rather than an interface, which limits its reusability. The shadow of observer pattern can also be seen in JavaBean and swing.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.