Design Pattern thinking-Observer Pattern

Source: Internet
Author: User

Design Pattern thinking-Observer Pattern

The observer mode defines a one-to-many dependency between objects. When the State of an object changes, all objects dependent on it are notified and updated automatically.

Observer class diagram:

 

Subject (observer)
The object to be observed. When the state to be observed changes, all observer objects in the queue need to be notified. Subject needs to maintain (add, delete, and notify) the queue list of an observer object.

ConcreteSubject
The specific implementation of the observer. Includes some basic attribute statuses and other operations.

Observer (Observer)
Interface or abstract class. When the status of the Subject changes, the Observer object will be notified through a callback function.

ConcreteObserver
The specific implementation of the observer. Some specific business logic processing will be completed after the notification is received.

1. Relationship between the target and the observer: According to the schema definition, the relationship between the target and the observer is a typical one-to-many relationship, but it is acceptable if there is only one observer. This mode can also be used when processing the state changes of an object that affect another object.

2. One-way dependency: In the observer mode, the observer and the target are one-way dependent. Only the observer depends on the target, but the target does not depend on the observer. The initiative between them is in the hands of the target.

3. Implementation: The specific target implementation object must be able to maintain the registration information of the observer. The simplest thing is to use an opportunity to save the registration information of the observer. If an observer observes multiple targets, it is necessary to determine the target notification in the update method of the observer. The simple method is to extend the update method. For example, if you want to distinguish between multiple parameters in a method, another simpler method is to define different callback methods.

Observer mode in Java

In the java. util package, there is a class Observable, which implements most of the functions we need. There is also an interface Observer, which defines the update method, which is the Observer interface.

 

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.