(16) Viewer mode

Source: Internet
Author: User

 (+) Observer mode

definition: defines a one-to-many dependency between objects so that when each object changes state, all objects that depend on it are notified and updated automatically.

Type: behavior class mode

Class Diagram:

This is often required in software systems: if the state of an object changes, certain objects associated with it change accordingly. For example, we want to design a right-click menu function, as long as the software in the active area of the right mouse button, will pop up a menu, for example, we want to design an automatic deployment of functionality, just like Eclipse Development, as long as the file is modified,eclipse The modified file is automatically deployed to the server. The two features have a similar place where one object listens to another object at all times, and as long as its state changes, it has to act accordingly. In fact, there are a lot of scenarios that can be achieved, but there is no doubt that using the observer pattern is a mainstream choice.

Structure of the Observer pattern

In the most basic observer pattern, the following four characters are included:

  • Observer: from the class diagram you can see that there is a class that holds the object of the Observer. vector container (the reason for using vector without using list , Because of the multithreading operation, vector is safe, while list is unsafe), this Vector container is the core of the Observer class, there are three other methods: attach method is to add the Observer object to the container; detach The method is to remove the observer object from the container; notify method is to call the corresponding method of the Observer object in turn. This role can be an interface, an abstract class, or a specific class, because in many cases it is mixed with other schemas, so the use of abstract classes is more likely.

  • Observer: The observer role is generally an interface that has only one update method that is invoked when the observer state changes.

  • Specific Observer: This role is used for extensibility purposes, where specific business logic can be defined in this role.

  • Specific observer: the specific implementation of the Observer interface, in which the logic to be processed when the state of the Observer object is changed is defined.


Observer Pattern Code Implementation

Abstract class Subject {    private vector<observer> obs = new vector<observer> ();    public void Addobserver (Observer obs) {       this.obs.add (OBS);    }    public void Delobserver (Observer obs) {       this.obs.remove (OBS);    }    protected void Notifyobserver () {for       (Observer o:obs) {           o.update ();       }    }    public abstract void DoSomething ();}
Class ConcreteSubject extends Subject {public    void dosomething () {       System.out.println ("The observed event is reversed");       This.notifyobserver ();    }}
Interface Observer {public    void Update ();}
Class ConcreteObserver1 implements Observer {public    void Update () {       SYSTEM.OUT.PRINTLN ("Observer 1 receives the information and processes it. ");    }}
Class ConcreteObserver2 implements Observer {public    void Update () {       SYSTEM.OUT.PRINTLN ("Observer 2 receives the information and processes it. ");    }}
public class Client {public    static void Main (string[] args) {       Subject sub = new ConcreteSubject ();       Sub.addobserver (New ConcreteObserver1 ()); Add Observer 1       sub.addobserver (new ConcreteObserver2 ());//Add Observer 2       sub.dosomething ();}    }

Run results

The Observer event, the counter-observer, 1 receives the message and processes it. Observer 2 receives the message and processes it.

By running the results we can see that we have only called the Subject method, but at the same time the related methods of the two observers have been called simultaneously. A closer look at the code, in fact, is very simple, is simply to associate the Observer class in the Subject class, and in the dosomething method to traverse the Observer The update method is OK.

The advantages of the observer pattern

The observer and the observed are mildly correlated, and are abstract coupled, so that they are easier to extend for both.

The Observer pattern is a common trigger mechanism that forms a trigger chain and then processes each observer's method in turn. But at the same time, this is a disadvantage of the observer pattern, because it is a chain trigger, when the observer is more, performance problems are more worrying. Moreover, in the chain structure, it is easy to make a circular reference error, causing the system to feign death.

Summarize

In the Java language, there is an interface Observer, and its implementation class Observable, which is often implemented for observer roles. We can see how these two classes are used in the JDK 's API documentation.

A friend who has done VC + +,javascript DOM , or AWT has been amazed by the handling of their events, and the observer pattern, which has a certain understanding of the principle of the event handling mechanism. Using the Observer pattern is a good choice if you want to design an event-triggering processing mechanism, the event-handling DEMinAWT (Delegating event model delegation ) is implemented using the Observer pattern.

(16) Viewer mode

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.