Design Patterns - observer

Observer Pattern

Understanding observer mode

Subscribe to the newspaper chestnuts

1) the newspaper business is publishing newspaper

2) you subscribe to their newspapers to a newspaper, then you become their subscribers,

   As long as the newspaper has published a new newspaper, the newspaper will be sent to you

3) When you cancel subscriptions to newspapers, you will be removed from the newspaper's subscribers, the newspaper will not send the newspaper to you

4) As long as the newspaper still operating, some people will always subscribe or unsubscribe newspaper

 

Generally we call on the chestnut in the newspaper as a "theme", said the subscribers as "observers."

1) theme for managing some of the data, once the data is changed, the new data will be sent to the hands of the observer

2) When the topic of data changes, only subscribe to topics observer can receive data updates

3) When a new object is subscribed topic, it will become an observer, it can receive data updated topics

4) When an observer unsubscribe from a topic, it will no longer receive data updated topics

 Themes and map viewer (to be completed ...)

 

Observer Pattern

concept

Define dependencies between objects - many, when the state of an object is changed, all its dependents are notified and updated data.

 

UML class diagrams

 

 

 

UML class diagram illustrates

1) Theme interface object using this interface registered observer, delete, and update the viewer's observer status

2) observer interfaces, all potential viewers must implement this interface, only one update () method, when the subject of state changes, it will be invoked;

  Each topic can have multiple observer

3) a specific theme always realize theme interface and realize its registered observer, delete and update observer status observer methods;

  Specific methods may also be provided with a method / acquisition state.

4) Specific observer may be implemented in any type of interface to the observer, the observer must register a specific topic, and to receive the update data.

 

Design Principle III - for the loose coupling between the object and design efforts

So say under what is loosely coupled

There are two interdependent objects, change one of the parties does not affect the other, as long as they still comply with interfaces,

We can change them freely.

 

Loose coupling between the theme and the observer, they can interact with each other but do not know the details,

For the viewer, the viewer needs to know the theme implements an interface that does not need to know the specific class observer who is doing what other details

The only thing is dependent on the theme of a list of objects that implement the Observer interface, so any time we can add, delete observer, replacing the existing observer with a new observer,

 The theme will not be affected.

 

When a new type of observer appear, the theme does not need to modify the code

      V

You need to do is implement this new type of viewer interfaces

Then you can register as an observer

      V

Theme notification sent only an observer to the object that implements this interface

If we need to use the observer and themes, you can easily reuse them

 

The advantages of loose coupling

The interdependence between the objects to a minimum, be able to respond to change, and then you can build flexible object-oriented systems.

 

Case

demand

The establishment of a weather station, the weather station must be built on WeatherData object of our application,

WeatherData object tracking by the current weather conditions (temperature, humidity, pressure),

There are three bulletin boards, show the current situation, weather statistics and forecast simple,

When an object WeatherObject the latest measurement data, all three notices must be updated in real time.

 

analysis

WeatherData source file

 

1) may be obtained by the three measurements WeatherData: temperature, humidity and atmospheric pressure

2) Once the measurement of meteorological data update, measurementsChanged () method is called

3) the need to achieve three use weather data bulletin board: current status bulletin boards, bulletin boards and weather forecast weather statistics bulletin boards, once WeatherData new data, they Update Now

4) The system must be scalable, such as other developers can add or delete any bulletin board

 

Some people think that the observations directly to the incoming observer status update is the most direct way, but there are many hidden dangers of this form,

For example, the number of types of observer may change, and if so, we can modify the code to meet more of these changes,

But this is not what we want to see

Reference P57

So the updated status to observer, what a good solution?

 

UML class diagram design

 

 

Guess you like

Origin www.cnblogs.com/marton/p/11438839.html