Design mode-viewer mode (UP) < turn >

Source: Internet
Author: User

This article refers to the head first design model book, the feeling of the book is very good example, very appropriate. The knowledge point of the model is summarized, and the source of the book has made a certain comment. The observer pattern has two main points: the subject and the viewer. The most appropriate case is: Magazine subscription, magazine is the subject, the observer is the Subscriber. This event automatically notifies all subscribers when a new magazine is published. According to OO basic principles, programming for interfaces (of course many reasons), topics and subscribers are generally used as interfaces. Here is an example of head first, a weather station where users subscribe to weather information Services. Also, the information that the end user wants may be: apply the Observer pattern to this requirement: But this is only a theme and the observer's design, the observer itself also needs to display the information.  Then use an interface, specifically to achieve the functionality of the display. In fact, the above Displayelement interface is used here, essentially the application of the policy pattern. Understanding the characteristics of each mode of communication and design has a great help, when the OO thought environment to a high degree, should be to forget all the model, and can make the best design according to needs, this degree can be counted in the hands of the sword, the point of the sword. Below is the source code, only list the interface:/**
* Theme
*/
PublicInterfaceSubject {
PublicvoidRegisterobserver (Observer o);
PublicvoidRemoveobserver (Observer o);
PublicvoidNotifyobservers ();
}/**
* Viewer
*/
PublicInterfaceObserver {
PublicvoidUpdatefloatTempfloatHumidity,floatpressure);
}/**
* Bulletin Board
*/
PublicInterfacedisplayelement {
PublicvoidDisplay ();
} Test the Main method: Public StaticvoidMain (string[] args) {
//Create a theme
Weatherdata Weatherdata =NewWeatherdata ();
//creation of three observers
Currentconditionsdisplay Currentdisplay =NewCurrentconditionsdisplay (Weatherdata);
Statisticsdisplay Statisticsdisplay =NewStatisticsdisplay (Weatherdata);
Forecastdisplay Forecastdisplay =NewForecastdisplay (Weatherdata);
Heatindexdisplay Heatindexdisplay =NewHeatindexdisplay (Weatherdata);
//For meteorological measurements, the status of the theme will change due to measured values
Weatherdata.setmeasurements (30.4f);
Weatherdata.setmeasurements (29.2f);
Weatherdata.setmeasurements (29.2f);
This example is just to illustrate the pattern, and the design is far from perfect. In fact, the topic should have a thread to scan the state of the changes, when changed, automatically go to call the Measurementschanged () method. The use of observer patterns is common, and event registration in GUI programming is a typical application of observer patterns. These are the observer patterns that you implement, and the observer patterns implemented by the JDK are described below.

This article is from the lava blog, so be sure to keep this source http://lavasoft.blog.51cto.com/62575/201617

Design mode-viewer mode (UP) < turn >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.