In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Observer mode
The observer pattern defines one-to-many dependencies between objects so that when an object changes state, all its dependents are notified and updated automatically. One side can be called subject, the other can be called observer, and a topic can have multiple observers. When the data of a topic changes, all observers who subscribe to it are notified. There are two kinds of operations after that:
Tweet: the topic pushes the data directly to all observers.
Pull: after the topic notifies all observers, each observer pulls the required data from the topic according to their own needs.
Liezi in reality: newspaper publisher + subscriber = observer mode, job search + headhunting = observer mode
Concept map:
UML figure:
Let's look at a Liezi.
Demand: demand for Internet weather station
Description: track the data of the weather station, update the WeatherData object when there are changes, and the latest information is displayed on the corresponding display, as follows:
We analyze the requirements:
1. One-to-many relationship (different data are displayed on the corresponding bulletin board, such as humidity, temperature, etc.).
two。 Update the latest values when the weather data changes and display them on the bulletin board.
It is very suitable for the observer pattern to implement, and the final implementation of the UML diagram is as follows (code omitted):
JAVA built-in support for observer mode
Note: JAVA built-in support for observer mode, Subject is a class not an interface, resulting in must use inheritance, java itself does not support multi-inheritance, so it leads to hierarchical problems and inflexibility. It is recommended that you realize it yourself.
Java.util.Observable (theme)
Java.util.Observer (Observer)
We use the built-in method to implement the column above, and the class diagram is as follows:
Summary
The application scenario of the observer mode:
1. Updating the state of one object requires synchronous updates of other objects, and the number of other objects is dynamically variable.
2. Objects only need to notify other objects of their updates and do not need to know the details of other objects.
Design principle: strive for loosely coupled design between interactive objects.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.