观察者设计模式 —> 行为型模式

观察者模式:对象之间多对一依赖的一种设计方案,被依赖的对象为 Subject,依赖的对象为 Observer。

Subject 通知 Observer 变化。

  • Observer:接收输入
  • Subject:登记注册、移除和通知
    • registerObserver:注册
    • removeObserver:移除
    • notifyObservers():通知所有注册用户,可以更新数据,让主动获取,也可以 Subject 时时推送。

image-20200511205158698

Subject

public interface Subject {
    void registerObserver(Observer o);

    void removeObserver(Observer o);

    void notifyObserver();
}

Observer

public interface Observer {
    void update(float temperature,float pressure,float humidity);
}

WeatherData

public class WeatherData implements Subject {
    private float temperature;
    private float pressure;
    private float humidity;
    private ArrayList<Observer> observers;//观察者集合

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

    public float getTemperature() {
        return temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public float getHumidity() {
        return humidity;
    }

    public void seData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        notifyObserver();//推送所有观察者 数据更新
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        if (observers.contains(o)) {
            observers.remove(o);
        }
    }
	
    //推送所有
    @Override
    public void notifyObserver() {
        for (int i = 0; i < observers.size(); i++) {
            observers.get(i).update(this.temperature, this.pressure, this.humidity);
        }
    }
}

CurrentConditions

public class CurrentConditions implements Observer {
    private float temperature;
    private float pressure;
    private float humidity;

    //更新数据
    @Override
    public void update(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        dispaly();
    }
    //展示数据
    private void dispaly() {
        System.out.println("============CurrentConditions data==========");
        System.out.println("today temperature =" + temperature);
        System.out.println("today pressure =" + pressure);
        System.out.println("today humidity=" + humidity);
    }

}



public class OtherCurrentConditions implements Observer {
    private float temperature;
    private float pressure;
    private float humidity;

    //更新数据
    @Override
    public void update(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        dispaly();
    }
    //展示数据
    private void dispaly() {
        System.out.println("============OtherCurrentConditions data==========");
        System.out.println("today temperature =" + temperature);
        System.out.println("today pressure =" + pressure);
        System.out.println("today humidity=" + humidity);
    }
}

Client

public class Client {
    public static void main(String[] args) {
        //创建 Subject WeatherData 数据源
        WeatherData weatherData = new WeatherData();
        //创建观察者 Observer
        Observer currentConditions = new CurrentConditions();
        Observer otherCurrentConditions = new OtherCurrentConditions();

        //把观察者注册进 Subject
        weatherData.registerObserver(currentConditions);
        weatherData.registerObserver(otherCurrentConditions);

        System.out.println("========更新数据,并通知观察者========");
        weatherData.seData(20f,20f,133f);


        weatherData.removeObserver(currentConditions);
        System.out.println("=======Remove CurrentConditions=======");
        weatherData.notifyObserver();
    }
}

观察者模式 JDK 应用源码分析

image-20200511212249538