Interaction between objects-Observer mode (2)

Source: Internet
Author: User
Tags response code
22.2 observer mode Overview

The observer mode is one of the most frequently used design modes. It is used to establish a dependency between an object and an object. When an object changes, other objects are automatically notified, other Objects will respond accordingly. In the observer mode, the changed object is called the observation object, and the notified object is called the observer. One observation object can correspond to multiple observers, and there can be no mutual relationship between these observers, you can add and delete observers as needed to make the system more scalable.

The observer mode is defined as follows:

Observer pattern: defines a one-to-many dependency between objects, so that every time an object state changes, its dependent objects are notified and automatically updated. The alias of observer mode includes publish/subscribe mode, model/view mode, source/listener mode, and dependents) mode. The observer mode is an object behavior mode.

The observer mode structure generally includes two inheritance hierarchies: The observation object and the observer. The structure is shown in 2-3:

Figure 22-3 structure of the observer Mode

The observer mode structure includes the following roles:


Subject (GOAL): a target, also known as a topic, refers to the object to be observed. An observer set is defined in the target. An observation target can be observed by any number of observers. It provides a series of methods to add and delete observer objects, it also defines the notification method notify (). The target class can be an interface, an abstract class, or a specific class.


Concretesubject (target): the target is a subclass of the target class. Generally, it contains frequently changed data. When its status changes, it sends a notification to its observers; it also implements the abstract business logic methods defined in the target class (if any ). If you do not need to extend the target class, the specific target class can be omitted.


Observer (observer): The observer will respond to changes to the observed object. The observer is generally defined as an interface, which declares the update () method for updating data, and is also called an abstract observer.


Concreteobserver (Specific observer): maintains a reference pointing to a specific target object in a specific observer, which stores the relevant States of the specific observer, which must be consistent with the status of the specific object; it implements the update () method defined in the abstract observer. Generally, you can call the attach () method of the target class to add yourself to the collection of the target class or delete yourself from the collection of the target class by using the detach () method.

The observer mode describes how to establish dependencies between objects and construct a system that meets such requirements.The observer mode contains two types of objects: The Observer and the observer. A target can have any number of dependent observers. Once the status of the observed object changes, all observers will be notified. As a response to this notification, each observer will monitor the status of the observed target to synchronize its status with the target status. This interaction is also called publish-subscribe ). The observation target is the publisher of the notification. It does not need to know who is its observer when sending the notification. It can have any number of observers to subscribe to it and receive the notification.

The following code is used to further analyze the mode. First, we define an abstract target subject. The typical code is as follows:

Import Java. util. *; abstract class subject {// defines a set of observers used to store all observer objects protected arraylist Observers <observer> = new arraylist (); // The registration method, adds public void attach (Observer observer) {observers to the observer set. add (observer);} // logout method, used to delete an observer public void detach (Observer observer) {observers. remove (observer);} // declare the abstract notification method public abstract void y ();}

The specific target class concretesubject is a concrete subclass that implements the abstract target class subject. The typical code is as follows:

Class concretesubject extends subject {// implement the notification method public void y () {// traverse the observer set and call the response method of each observer for (Object Obs: Observers) {(observer) obs ). update ();}}}

The abstract observer role is generally defined as an interface. Generally, only one update () method is declared, and the same interface is defined for the update (response) behavior of different observers. This method is implemented in its subclass, different observers have different response methods. The typical code of the abstract observer is as follows:

Interface observer {// declare the response method public void Update ();}

The Update () method is implemented in the concreteobserver of a specific observer. The typical code is as follows:

Class concreteobserver implements observer {// implement the response method public void Update () {// specific response code }}

In some more complex situations,The Update () method of the concreteobserver of the specific observer class needs to use the status (attribute) in the concretesubject of the specific target class during execution)Therefore, there are sometimes associations or dependencies between concreteobserver and concretesubject. Define a concretesubject instance in concreteobserver and obtain the State stored in concretesubject through this instance. If the concreteobserver Update () method does not need to use the state attribute in concretesubject, You can simplify the standard structure of the observer mode, the object reference does not need to be maintained between the concreteobserver of a specific observer and the concretesubject of a specific target. If there is a correlation between the specific layer, the scalability of the system will be affected. To add a new target class, you sometimes need to modify the code of the original observer, to some extent, it violates the "open and closed principle". However, if the original observer class does not need to be associated with new targets, the system scalability will not be affected.

 

Thoughts

Does the observer mode comply with the "open and closed principle "? [Add a specific observer and add a specific target .]

【Author】 Liu Wei
Http://blog.csdn.net/lovelion]

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.