The observer pattern for Java design patterns

Source: Internet
Author: User

Viewer mode (Observerpattern). For real-time monitoring of some object's dynamics, as long as the object changes, then all of his observers observer will know, after the observer will be based on the change of object to do the next step. This is the most common in swing programming. Those listener are the observers.


The Observer mode role is as follows:

Abstract Theme (Subject) role : Abstract topic roles provide a way to maintain the aggregation of an observer object, add to the aggregation, delete, and so on.

specific subject (ConcreteSubject) role : The status of the state is deposited into a specific observer object, and all registered observers are notified when the internal state of the specific subject changes. The specific topic role is responsible for implementing the aggregation management approach in the abstract topic.

abstract Observer (Observer) Role : Provides an update interface for specific observers.

specific observer (concreteobserver) role : Stores the self-consistent state associated with a topic and implements an update interface provided by an abstract observer.


Six Big questions:

1. The relationship between the target and the viewer

A one-to-many or more pair-

2. One-way dependence: The Observer passively waits for the target

3. Naming recommendations: Subject Observer update

4, the time to trigger the update

5. The invocation order of the Observer pattern:

Create target create observer register Observer

6. Order of notifications

A parallel notification priority between observers


Two types of models:

1. Push the model: Update passes some information about the target object

2. Pull model: Update passes the target object itself


Advantages and Disadvantages

1. The abstract coupling between the observer and the target is realized.

2, the Observer mode realizes the dynamic linkage

3. Support Broadcast Communication

4, will cause unnecessary operation


When to use Observer mode:

Trigger linkage


The observer pattern with Java comes in:

Target code:

package com.nudt.design.javaobserver;import  java.util.observable;/** * created by jeffrey on 15-12-1. */public  class concreteweathersubject extends observable {    private  String weathercontend;    public string getweathercontend ()  {         return weatherContend;    }     public void setweathercontend (string weathercontend)  {         this.weatherContend = weatherContend;         this.setchanged ();         this.notifyobservers ( Weathercontend);//Push mode         //this.notifyobservers ();//Pull mode      }} 

     Observer Code:

package com.nudt.design.javaobserver;import java.util.observable;import java.util.observer;/**  * created by jeffrey on 15-12-1. */public class concreteobserver  implements Observer {    private String observerName;     public string getobservername ()  {         return observerName;    }    public void  Setobservername (String observername)  {         this.observername = observername;    }     @Override      public void update (Observable observable, object o)  {         //Push the way          System.out.println (observername +  "Get:" + o);The way of         //pull         // System.out.println (observername + "Get:"  +  ((concreteweathersubject) observable). Getweathercontend ();     }}

Treat the viewer scene differently

is a simple judgment statement.

The observer pattern for Java design patterns

Related Article

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.