Design Patterns | Second: Observer Pattern

The observer pattern defined

  Defines dependencies between objects of many, so that, when an object changes state, its dependents will be notified and updated automatically.

scenes to be used

  Publish and subscribe, inform the public and other micro-channel number of the magazine

Design Principles

  To loosely coupled interaction between design objects and work

Advantages and disadvantages

  advantage:

    1, the observer and observed abstract coupling.
    2, the establishment of a monitor trigger mechanism.

  Disadvantages:

    1, if the number of observers, notify all observers would be very time-consuming, there recommended asynchronous processing.    

    2, the cycle call can occur.

UML class diagrams

 

 

Examples

  Examples of applications where a book "Head First Design Patterns"

Theme (the observer) Interface  

Package com.study.headfirst.oberver; 

/ ** 
 * relating 
 * 
 * @author MDL 
 * @date 2019/12/02 
 * / 
public  interface the Subject { 

    / ** 
     * registered observer 
     * 
     * @param O
      * / 
    public  void RegisterObserver (the observer O); 
    
    / ** 
     * removed observer 
     * 
     * @param O
      * / 
    public  void removeObserver (the observer O); 
    
    / ** 
     * notify all observers 
     * / 
    public  void notifyObserver (); 
    
    
    
}

Theme (the observer):

Package com.study.headfirst.oberver; 

Import of java.util.ArrayList;
 Import java.util.List; 

/ ** 
 * the observed 
 * 
 * @author MDL 
 * @date 2019/12/02 
 * / 
public  class of the WeatherData the implements the Subject { 

    / ** 
     * viewer record 
     * / 
    Private List <the observer> OS; 

    / ** 
     * temperature 
     * / 
    Private  a float TEMP; 

    / ** 
     * humidity 
     * / 
    Private  a float humidity; 

    public of the WeatherData () { 
        OS= new ArrayList<Observer>();
    }

    /* (non-Javadoc)
     * @see com.study.headfirst.oberver.Subject#registerObserver(com.study.headfirst.oberver.Observer)
     */
    @Override
    public void registerObserver(Observer o) {
        os.add(o);
    }

    /* (non-Javadoc)
     * @see com.study.headfirst.oberver.Subject#removeObserver(com.study.headfirst.oberver.Observer)
     */
    @Override
    public void removeObserver(Observer o) {
        int i = os.indexOf(o);
        if (i > 0) {
            os.remove(i);
        }
    }

    /* (non-Javadoc)
     * @see com.study.headfirst.oberver.Subject#notifyObserver()
     */
    @Override
    public void notifyObserver() {
        for (Observer o : os) {
            o.update(temp, humidity);
        }
    }

    public void setMeasurements(float temp, float humidity) {
        this.temp = temp;
        this.humidity = humidity;
        notifyObserver();
    }

}

 

Observer A (current state signboard):

package com.study.headfirst.oberver;

/**
 * 当前观测布告:显示温度
 * 
 * @author mdl
 * @date 2019/12/02
 */
public class CurrentConditionsDisplay implements DisplayElement, Observer{

    private float temp;
    
    public Subject weatherData;
    
    public CurrentConditionsDisplay(Subject weatherData) {
        this.weatherData =weatherData;
        weatherData.registerObserver(this);
    }
    
    @Override
    public void display() {
        System.out.println("当前状况-----当前温度:" +  temp);
    }

    /* (non-Javadoc)
     * @see com.study.headfirst.oberver.Observer#update(float)
     */
    @Override
    public void update(float temp, float humidity) {
        // TODO Auto-generated method stub
        this.temp =temp;
        display();
    }

}

Observer B (weather forecast signboard):

package com.study.headfirst.oberver;

import com.study.headfirst.oberver.DisplayElement;
import com.study.headfirst.oberver.Observer;

/**
 * 天气预报:显示温度、湿度
 * 
 * @author mdl
 * @date 2019/12/09
 */
public class ForcastDisplay implements DisplayElement, Observer{

    private float temp;
    
    private float humidity;
    
    public Subject weatherData;
    
    public ForcastDisplay(Subject weatherData) {
        this.weatherData =weatherData;
        weatherData.registerObserver(this);
    }
    
    @Override
    public void display() {
        System.out.println("天气预报-----当前温度:" +  temp + ", 当前湿度:" + humidity);
    }

    /* (non-Javadoc)
     * @see com.study.headfirst.oberver.Observer#update(float)
     */
    @Override
    public void update(float temp, float humidity) {
        // TODO Auto-generated method stub
        this.temp =temp;
        this.humidity =humidity;
        display();
    }

}

test:

Package Penalty for com.study.headfirst.oberver; 

/ ** 
 * observer mode 
 * 1. The theme of the registration, removal notice 
 * 2. The observer operation after receiving notice 
 * 
 * @author mdl 
 * @date 2019/12/02 
 * / 
public  class the Test { 

    / ** 
     * @param args
      * / 
    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub 
        of the WeatherData Subject = new new of the WeatherData (); 
        CurrentConditionsDisplay CCD = new new CurrentConditionsDisplay (Subject) ; 
        forcastDisplay forcastDisplay = new new ForcastDisplay(subject);
        subject.setMeasurements(27f, 80f);
        System.out.println("==================================");
        subject.setMeasurements(35f, 60f);
    }

}

 

Guess you like

Origin www.cnblogs.com/bloodthirsty/p/12010563.html
Recommended