The application scenario for the project is that the meteorological Bureau needs us to build a system with two billboards that show the current real-time weather and the weather forecast for the next few days. When the Meteorological Bureau publishes new weather data, the weather data displayed on the two bulletin boards must be updated in real time. The meteorological Bureau also asked us to ensure that the program was sufficiently scalable, since a new bulletin board might be added at a later stage. Principle:
We implemented the theme interface with Weatherdate, two bulletin boards currentconditionsdisplay and Forcastdisplay realized the observer and Displayelement interfaces, In their constructor, the Registerobserver () method in Weatherdata is called to register themselves as observers and save them in a collection. When the observer is the Weatherdata data change, the Notifyobserver () method is called to notify the Observer, that is, two bulletin boards, and call the Updata () method to update their data.
Advantages of this mode:
1. The Observer (bulletin board) and the Observer (Meteorological Office) are fully decoupled, the observer only knows the abstract interface of the Observer list, and the observer does not know the specific observer.
2. Notifications are sent by the Observer, and all registered observers receive information that can implement the broadcasting mechanism.
Finally attach the source code address of the program: Https://github.com/BaronZ88/DesignPatterns/tree/master/src/com/baron/patterns/observer
Observer patterns in weather forecasting projects