Head first design mode note (Observer Mode)

Source: Internet
Author: User
Observer mode definition

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. How do I feel so familiar with the observer mode ~? Yes, delegation mechanism!

Background

Your team just won a paper contract to build the Next Generation Weather Station-Internet meteorological observatory for weather-o-rama. The weather station must be built on a patented weatherdata object that tracks the current weather conditions (temperature, humidity, and air pressure ). Requirement: Create an application with three types of boards to show the current situation, meteorological statistics, and simple forecasts respectively. When the weatherobject obtains the latest measurement data, the Three billboards must be updated in real time. In addition, this is a scalable meteorological station. The weather-o-rama meteorological station hopes to have a set of Apis so that other developers can write their own meteorological reports and insert them into this application.

Commencement

OK. Our job is to obtain data based on weatherdata and update the three bulletin boards (current conditions, meteorological statistics, and weather forecasts). Let's take a look at the weatherdata design:

List below:

  • The weatherdata class has three get Methods: temperature, humidity, and pressure.
  • When the new measurement data is obtained, the measurementschanged () method is called, so we don't care how it is called, as long as it is called
  • We need to implement three bulletin boards that use weather data. Once weatherdata has a new measurement, it will be immediately updated: "Current Situation" Notice, "meteorological statistics" Notice, and "Weather Forecast" Notice.
  • This system must be scalable to allow other developers to create custom dashboards. You can add or delete any dashboards as you like. Currently, three types of bulletin boards are initialized: "Current Situation" Notice, "meteorological statistics" Notice, and "Weather Forecast" Notice (what if a new announcement is to be added in the future ?)
What should I do?

It's easy to implement:

Public void meansurementschanged ()
{
// We only need to implement this method. // get the data float temp = gettemperature ();
Float humidity = gethumidity ();
Float Pressure = getpressure ();
// Call the update method currentconditionsdisplay of the respective Boards. update (temp, humidity, pressure); statisticsdisplay. update (temp, humidity, pressure); forecastdisplay. update (temp, humidity, pressure );}
Right ?? Recall the content of the previous text:
 
Understanding observer Mode
This is the observer mode:
 
There are more than one class chart that implements the observer mode. Generally, the subject and observer interfaces are commonly used in class design. Below, we define the class diagram of the observer mode:
  
Note:
  • Two interfaces: topic interface subject and observer interface observer
  • The object registers as an observer using the subject interface, or deletes itself from the observer.
  • All potential observers must implement the observer Interface
  • Each topic has many observers.
  • A specific topic concretesubject class implements the subject Interface
  • The specific observer concretobserver class implements the observer interface and must register a specific topic

Observer mode, which I think is quite understandable. After all, the C # Delegate mechanism we are using is the principle of an observer mode.

End 
I think the specific implementation code is clear in this book. At the end of this chapter, I also talked about "Push mode" and "pull mode ", the so-called "Push mode" means that the topic object transmits data,
"Pull Mode" is the active data of the observer object. It is very interesting! We should keep in mind some design principles:
  • Encapsulation changes
  • Multi-Purpose Combination and less inheritance
  • Efforts to design loose coupling between interaction objects
 
PS: MSN: ibin@live.com, welcome to exchange

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.