当前位置:网站首页>Observer mode

Observer mode

2022-06-23 08:09:00 Study notes of Aries orange

1、 demand

 Insert picture description here

2、 How observer mode works + The class diagram design

When subject When the data in the , All of them will be informed obervers;
It's actually subject One was maintained oberver Of list,observer Can dynamically join subject Of list, Or remove from it
 Insert picture description here
 Insert picture description here

3、 The observer pattern implements the above requirements

/** * @author houChen * @date 2022/6/19 21:51 * @Description: */
public interface Subject {
    

    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObservers();

}

/** * @Description: * 1、 Contains the latest weather information  * 2、  Have a collection of observers , Use ArrayList  Conduct management  * 3、 When the data is updated , Notify all access parties of the latest information  */
public class WeatherData implements Subject {
    
    private float temperatrue;
    private float pressure;
    private float humidity;
    // Observers gather 
    private ArrayList<Observer> observers;

    public WeatherData() {
    
        this.observers = new ArrayList<Observer>();
    }

    public float getTemperature() {
    
        return temperatrue;
    }

    public float getPressure() {
    
        return pressure;
    }

    public float getHumidity() {
    
        return humidity;
    }

    public void setData(float temperature, float pressure, float humidity) {
    
        this.temperatrue = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        this.notifyObservers();
    }

    // Register an observer 
    @Override
    public void registerObserver(Observer o) {
    
        this.observers.add(o);
    }

    // Remove an observer 
    @Override
    public void removeObserver(Observer o) {
    
        if (this.observers.contains(o)) {
    
            this.observers.remove(o);
        }
    }

    // Traverse all observers , And give notice 
    @Override
    public void notifyObservers() {
    
        for (int i = 0; i < this.observers.size(); i++) {
    
            this.observers.get(i).update(this.temperatrue, this.pressure, this.humidity);
        }
    }
}

/** * @author houChen * @date 2022/6/19 21:55 * @Description:  Observer interface  */
public interface Observer {
    

    public void update(float temperature,float pressure,float humidity);

}

/** * @author houChen * @date 2022/6/19 21:58 * @Description:  Concrete observer  */
public class CurrentCondition implements Observer {
    

    //  temperature , pressure , humidity 
    private float temperature;
    private float pressure;
    private float humidity;

    // to update   Weather conditions , By  WeatherData  To call , I use push mode 
    public void update(float temperature, float pressure, float humidity) {
    
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        display();
    }

    // Show 
    public void display() {
    
        System.out.println("***Today mTemperature: " + temperature + "***");
        System.out.println("***Today mPressure: " + pressure + "***");
        System.out.println("***Today mHumidity: " + humidity + "***");
    }
}

/** * @author houChen * @date 2022/6/19 22:14 * @Description: */
public class Client {
    
    public static void main(String[] args) {
    
        WeatherData weatherData = new WeatherData();
        // Create an observer 
        CurrentCondition currentCondition = new CurrentCondition();
        // Registered observer 
        weatherData.registerObserver(currentCondition);
        weatherData.setData(10, 100, 30.3f);
    }
}

4、 Observer mode benefits

  • After the observer pattern is designed , Will manage users in a collective way (Observer), Including registration removal and notification
  • such , When we add observers , There is no need to modify the core class WeatherData,

JDK Source code : Observale Class uses the observer pattern

原网站

版权声明
本文为[Study notes of Aries orange]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230742348400.html