Study Notes of head. First design pattern (3) -- Observer Pattern

Source: Internet
Author: User

Intent:Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated.

Structure:

Example:

The following describes the simulated weather station system.

Requirement Analysis:

The requirements of the system are as follows:

1. The weather station can track the current weather conditions, including temperature, humidity, air pressure,

2. The weather station can provide three types of boards to show the current weather conditions, meteorological statistics, and simple forecasts.

3. The data on the bulletin board must be updated in real time.

4. The meteorological station must provide a set of APIS for other developers to develop other boards.

Design part:

Based on the above requirements, the system can be designed into three parts: meteorological stations (physical devices for obtaining actual meteorological data) and weatherdata objects (tracking data from meteorological stations and updating the bulletin board) and the Bulletin Board (display the current weather conditions for users ). As follows:

Incorrect class diagram design(That is, the first thought when I have never learned the design pattern) may be as follows:

CorrespondingCodeImplementation:

1 Public   Void Measurementschanged ()
2 {
3 Temperature =   This . Gettemperature (); // Obtain Temperature
4 Humidity =   This . Gethumidity (); // Obtain humidity
5 Pressure =   This . Getpressure (); // Obtain pressure
6
7 Mycurrentconditionsdisplay. Update (temperature, humidity, pressure ); // Update current weather status board
8 Mystatisticsdisplay. Update (temperature, humidity, pressure ); // Update meteorological statistics board
9 Myforcastdisplay. Update (temperature, humidity, pressure ); // Update weather forecast Board
10 }

Disadvantages of this class graph design:

1) The design is for specific implementation programming, rather than for interfaces.

2) We have to modify the code for each new bulletin board.

3) We cannot dynamically add or delete the bulletin board during exercise.

4) We have not encapsulated the changed part.

So how can we correct these shortcomings?

First, we must understand the root causes of these shortcomings. Obviously, when designing a class diagram, the dependency relationship is incorrect. We should put the dependency upside down. The currshortconditionsdisplay class, statisticsdisplay class, And forcastdisplay class should depend on the weatherdata class, rather than the opposite, so that they can be decoupled.

Second, the currshortconditionsdisplay class, statisticsdisplay class, And forcastdisplay class both have an update () method. Therefore, we should extract an interface to implement "interface-specific programming" to make the code more flexible, it also facilitates other developers to develop other billboards.

Further consideration:

1) after correcting these shortcomings, our class chart has a similar structure with the observer pattern.

2) The biggest problem of our meteorological station system is actually the one-to-many dependency, and the observer mode is the only way to lift the one-to-many relationship. Therefore, we need to adopt the observer mode.

The class chart designed with the observer pattern should be like this:

Weatherdatea implements the isubject interface, currentconditionsdisplay, forcastdisplay, and statisticsdisplay implement the iobserver interface, isubject calls iobserver, currentconditionsdisplay, forcastdisplay, and statisticsdisplay calls isubject.

Code implementation:

1 Public   Class Weatherdata: isubject
2 {
3 Private   Float Temperature;
4 Private   Float Humidity;
5 Private   Float Pressure;
6 Private List < Iobserver > Mylist =   New List < Iobserver > ();
7
8 Public   Void Setweatherdata ( Float Paramtemp, Float Paramhumidity, Float Parampressure)
9 {
10 This . Temperature = Paramtemp;
11 This . Humidity = Paramhumidity;
12 This . Pressure = Parampressure;
13 Measurementschanged ();
14 }
15
16 Public   Void Measurementschanged ()
17 {
18This. Yyobservers ();
19}
20
21 Public   Void Registerobserver (iobserver paramiobserver)
22 {
23Mylist. Add (paramiobserver );
24}
25
26 Public   Void Removeobserver (iobserver paramiobserver)
27 {
28Mylist. Remove (paramiobserver );
29}
30
31 Public   Void Yyobservers ()
32 {
33 Foreach (Iobserver observer In Mylist)
34 {
35Observer. Update (temperature, humidity, pressure );
36}
37 }
38 }

Source codeDownload:/files/wxj1020/weatherstation.rar

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.