Design pattern Thinking----observer pattern

Source: Internet
Author: User

The Observer pattern defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated.

Viewer class Diagram:

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

ConcreteSubject
The concrete realization of the observed. Contains some basic property states and other operations.

Observer (Observer)
Interface or abstract class. When the state of the Subject changes,theObserver object is notified via a callback function.

Concreteobserver
The specific implementation of the observer. After being notified, some specific business logic processing will be completed.

1. The relationship between the target and the Observer: according to the pattern definition, the target and the observer are a typical one-to-many relationship, but if the observer is only a single person is also possible. You can also consider using this pattern when dealing with the state of an object that affects another object.

2. One-way dependency: In the Observer pattern, the observer and the target are one-way dependent, only the observer depends on the target, and the target is not dependent on the observer. The initiative between them is in the hands of the target.

3. Realization: The target Realization object to be able to maintain the observer's registration information, the simplest is to take an opportunity to save the Observer's registration information. If an observer observes multiple targets, then in the Observer's Update method, it is necessary to determine which target is the notification. The simple approach is to extend the Update method. For example, there is a simpler way to define a different callback method than to pass a parameter in the method to differentiate it.

Observer patterns in Java

In the Java.util package there is a class observable, which implements most of the functions we need for the target, and an interface observer, which defines the method of update, which is the interface of the Observer.

Design mode Thinking----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.