A detailed explanation of the Observer mode (Observer pattern)

Source: Internet
Author: User
Tags stub

Observer mode (Observer pattern): Defines a one-to-many dependency between objects, so that when an object changes state, all its dependents are notified and automatically updated.

How to use:

1. First new theme (subject) interface, responsible for registration (register) Delete (remove) \ Notice (notify) observer; Observer (Observer) interface, responsible for updating (update) data;

Subject (subject) Interface: Registered observer (REGISTEROBSERVER), delete Observer (Removeobserver), notify the Observer (Notifyobservers, notify all observers);

Observer (Observer) interface: Updates (update);

Code:

/** 
 * @time May 22, 2014 * * *
package observer;  
      
/** 
 * @author C.l.wang 
 * 
 *
/public interface Subject {public  
    void Registerobserver (Observer o); c10/>public void Removeobserver (Observer o);  
    public void Notifyobervers ();  
}  
      
      
/** 
 * @time May 22, 2014 * * *
package observer;  
      
/** 
 * @author C.l.wang 
 * 
 *
/public interface Observer {public  
    void update (float temp, float Humidity, float pressure);  
      
      
/** 
 * @time May 22, 2014 * * *
package observer;  
      
/** 
 * @author C.l.wang 
 * 
 *
/public interface Displayelement {public  
    void display ();  
}

2. Implementing the subject (subject) interface, registering (register) and Deleting (remove) observer in the form of lists (list),

Notifies (notify) The observer, iterates through the update (update) Action of all registered observers;

Through an interface function (set), encapsulates the notification (notify) action, passing in the parameters and notifying.

Code:

/** * @time May 22, 2014 * * * Package observer;  
      
Import java.util.ArrayList; /** * @author C.l.wang * */public class Weatherdata implements Subject {public arraylist<obser  
    Ver> observers;  
    private float temperature; private float humidity;  
          
    Humidity private float pressure;  
    Public Weatherdata () {observers = new arraylist<observer> (); }/* (NON-JAVADOC) * @see Observer. SUBJECT#REGISTEROBSERVER (Observer. Observer) */@Override public void Registerobserver (Observer o) {//TODO auto-generated Metho  
    D stub Observers.add (o); }/* (NON-JAVADOC) * @see Observer. SUBJECT#REMOVEOBSERVER (Observer. Observer) */@Override public void Removeobserver (Observer o) {//TODO auto-generated method  
        stub int i = Observers.indexof (o); if (i>=0) {Observers.remove (i); }/* (NON-JAVADOC) * @see Observer.  Subject#notifyobervers () */@Override public void Notifyobervers () {//TODO auto-generated method  
            Stub for (int i=0; i<observers.size (); ++i) {Observer Observer = (Observer) observers.get (i);  
        Observer.update (temperature, humidity, pressure);  
    } public void Measurementschanged () {notifyobervers (); public void setmeasurements (float temperature, float humidity, float pressure) {This.tempera  
        ture = temperature;  
        this.humidity = humidity;  
        This.pressure = pressure;  
    Measurementschanged (); }  
      
}

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.