观察者模式 - 在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖他的对象都会收到通知,并更新
/** * @description 观察者 更新消息 */ public interface Observer { /** * 功能描述: 更新 * @author a * @date 14:53 2020/11/23 * @param temp * @param humidity * @param pressure */ void update(float temp, float humidity, float pressure); }
package com.design.demo5; /** * @description 主题 */ public interface Subject { /** * 功能描述: 添加观察者 * @param observer 观察者对象 */ void registerObserver(Observer observer); /** * 功能描述: 删除观察者 * @param observer 观察者对象 */ void removeObserver(Observer observer); /** * 功能描述: 通知观察者 */ void notifyObserver(); }
package com.design.demo5; import java.util.ArrayList; import java.util.List; /** * @description 天气数据 改变气象数据 通知观察者 */ public class WeatherData implements Subject { private List<Observer> observers; private float temperature; private float humidity; private float pressure; public WeatherData() { this.observers = new ArrayList<>(); } //改变气象数据 public void setMeasurements(float temperature, float humidity, float pressure) { // 更改气象数据 this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; // 通知观察者 measurementsChanged(); } private void measurementsChanged() { notifyObserver(); } @Override public void registerObserver(Observer observer) { observers.add(observer); } @Override public void removeObserver(Observer observer) { int i = observers.indexOf(observer); if (i >= 0) { observers.remove(i); } } @Override public void notifyObserver() { for (Observer observer : observers) { observer.update(temperature, humidity, pressure); } } public float getTemperature() { return temperature; } public void setTemperature(float temperature) { this.temperature = temperature; } public float getHumidity() { return humidity; } public void setHumidity(float humidity) { this.humidity = humidity; } public float getPressure() { return pressure; } public void setPressure(float pressure) { this.pressure = pressure; } }
package com.design.demo5; /** * @description 观察者对象 */ public class ObserverTest01 implements Observer { private float temperature; private float humidity; private float pressure; private WeatherData weatherData; public ObserverTest01(WeatherData weatherData) { this.weatherData = weatherData; //订阅 weatherData.registerObserver(this); } @Override public void update(float temp, float humidity, float pressure) { this.temperature = temp; this.humidity = humidity; this.pressure = pressure; display(); } public void display() { System.out.println("[push] 当前温度01:" + temperature + " 当前湿度01: " + humidity + " 当前压力01:" + pressure); } } public class ObserverTest02 implements Observer { private float temperature; private float humidity; private float pressure; private WeatherData weatherData; public ObserverTest02(WeatherData weatherData) { this.weatherData = weatherData; //订阅 weatherData.registerObserver(this); } @Override public void update(float temp, float humidity, float pressure) { this.temperature = temp; this.humidity = humidity; this.pressure = pressure; display(); } public void display() { System.out.println("[push] 当前温度02:" + temperature + " 当前湿度02: " + humidity + " 当前压力02:" + pressure); } }
public class Test { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); ObserverTest01 observerTest01 = new ObserverTest01(weatherData); ObserverTest02 observerTest02 = new ObserverTest02(weatherData); weatherData.setMeasurements(36, 56, 99); } } //[push] 当前温度01:36.0 当前湿度01: 56.0 当前压力01:99.0 //[push] 当前温度02:36.0 当前湿度02: 56.0 当前压力02:99.0