Head First design mode 2nd Observer pattern

Source: Internet
Author: User

2nd Chapter, Observer pattern

1. Definition:

A one-to-many relationship is defined between objects, and when an object changes state, the object's dependencies are notified and updated automatically.

2. Introduction

Before introducing the observer pattern, let's start with something that is often encountered in everyday life (perhaps fewer people now meet, but the older generation is spraying more), which is about subscribing to newspapers. In the newspaper subscription this whole event involved the newspaper, subscribers, Postman, three characters, we have a look at the three roles in the event of the respective responsibility for what work.

  newspaper , the main work has (1) to the subscribers to transact/revoke the newspaper subscription, (2) issue a new newspaper, (3) will issue a new newspaper to the postman

  The postman, who has a major job (1) to deliver the newly issued newspaper to subscribers

  subscribers , the main work has (1) told the newspaper I want to [cancel] subscribe to the newspaper, (2) Receive couriers delivery of the newspaper

In this whole event, as long as subscribers do not voluntarily unsubscribe and the newspaper does not fail, subscribers will always receive the latest newspaper from the newspaper, so that we can elicit the Observer pattern: publisher (newspaper + couriers) + Subscriber = Observer mode

Let's use a few graphs to roughly describe the application scenario of the Observer pattern:

Observer Pattern Class Diagram:

  

Simply say this class diagram, the upper left part is the subject (that is, the observer or the subject provider [I prefer to call it]), and the lower right part is the Observer (subject subscriber). Where the subject interface is the theme provider interface, all the subject providers must implement the interface, Weatherdata is a specific topic provider; The Observer interface is the subject Subscriber interface, and all subscribers must implement the interface. The Displayelement interface is an interface for presentation, and the subscriber who wants the feature implements the interface (which is implemented in this example).

The following is a brief implementation process, the code I will pay to the end of the article for you to download reference.

A, Span style= "font-family: italics; font-size:16px; " >weatherdata implement subject interface;

B, subscriber class implementation Observer interface;

C. The subject provider (Subject) as a subscriber (Observer) is an attribute that automatically registers [invokes the Registerobserver () method] into the subject provider when the Subscriber is instantiated;

D. When the subject provider subject) Automatically calls the Measurementchanged () method to push notifications [call the Notifyobverser () method] to Subscribers (Observer) when the information inside is changed;

E, subscribers get the latest information and update [call Update () method] Local Information

3. Key points of this chapter

ж Observer definition: defines a one-to-many relationship, and when an object changes state, the object's dependencies are notified and updated automatically.

ж new design principles: strive for loosely coupled design between interacting objects  

  The goten theme, or observer, updates the observer with a common interface

  GOTEN Observer and Observer are loosely coupled, but the observer does not know the details of the observer, only that the observer implements the Observer interface

  Goten When using this mode, the observer can push (push) or pull data from the observer (however, the push is considered more "correct")

  Goten You have multiple observers, you cannot rely on a specific notification order

  Goten Java has a variety of implementation of the observation mode, including the general java.util.Observable. It is not difficult to identify the possible problems with the use of java.util.Observable and to implement the observer pattern yourself if necessary.

  

About the 2nd chapter of the Observer pattern is introduced here, if the above content has errors in the place, but also please do not hesitate to enlighten, if you feel that there is no understanding of the place, can also put forward, we learn together.

The Observer pattern for the 3rd chapter is updated in recent days ....

Reprint please indicate the source, thank you!

Head First design mode 2nd Observer pattern

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.