A learning journey based on design patterns-Observer patterns (with source code)

Source: Internet
Author: User
A learning journey based on design patterns ----- Observer Mode 1, Initial observer Mode

In a time when the network was not so developed, many people wanted to know that the weather forecast for the next day had to watch live TV at the specified time, but sometimes they wanted to watch the weather and found that live TV had passed. This is really an unpleasant thing.

Based on this situation, Mobile launched the"Scheduled query weather forecast business. The mobile phone user told mobile that he was concerned about the weather situation the next day and subscribed to the weather notification service. When mobile receives the weather information from the weather station, it sends it to all users who have subscribed to this service. In this way, users do not have to worry about the weather.

2, What is the observer mode?

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

Alias:Dependency(Dependents ),Release-Subscription(P u B l I s h-s u B s c r I B E)

3, Schema Structure

4, ModeCodeExamples 4.1Related Classes

Abstractobsever(Abstract observer),Abstractsubject(Abstract goal),Mnoweathersubject(Mobile meteorological station service objectives),Mobileuserobsever(Mobile users),Weatherstation(Weather Station)

4.2Code Implementation

Abstractobsever

 

Abstractsubject

 

Mnoweathersubject

 

Mobileuserobsever

 

Weatherstation

 

Patternrun

4.3Case output result

5, Mode participant

Objectives:Abstractsubject,Mnoweathersubject

Observer:Abstractobsever,Mobileuserobsever

6, Advantages and disadvantages

A,Abstract coupling between the target and the observer

All a target knows is that it has a series of observers.,Each is abstractO B S E R v e rClass. The target does not know which specific class any observer belongs. In this way, the coupling between the target and the observer is abstract and minimal.

 

B,Supports Broadcast Communication

Unlike common requests,The recipient of a notification sent by the target does not need to be specified. The notification is automatically broadcast to all objects registered with the target object. The target object does not care about how many objects are interested in itself.;Its only responsibility is to notify its observers. This gives you the freedom to add and delete observers at any time. Processing or ignoring a notification depends on the observer

 

C,Unexpected updates

Because one observer does not know the existence of other observers,It may have no idea about the final cost of changing the target. A seemingly harmless operation on the target may cause a series of updates to the observer and those objects dependent on the observer. In addition,If the dependency criterion is incorrectly defined or maintained, it will often cause incorrect updates.,This type of error is usually hard to capture

7, Applicability

A,When an abstract model has two aspects:,One aspect depends on the other. Encapsulate the two in independent objects so that they can be changed and reused independently.

 

B,When changing an object, you need to change other objects at the same time.,I don't know how many objects need to be changed.

 

C,When an object must notify other objects, it cannot assume who the other objects are. In other words,You don't want these objects to be tightly coupled..

 

Click to download "documentation and source code download"

 

Please refer to the source "http://www.cnblogs.com/luxiaofeng54/archive/2013/04/10/3012822.html"

 

 

 

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.