$weatherData */ private $weatherData; public function __construct($weatherData) { // 创建一个 WeatherData 实例 $this->weatherData = $weatherData; $this->weatherData->registerObserver // 创建一个 WeatherData 实例 $this->weatherData = $weatherData; $this->weatherData->registerObserver 对象 $weatherData = new WeatherData(); // 创建显示装置 1,传入 WeatherData 对象 $firstDisplay = new FirstDisplay( $weatherData); // 传入模拟气象数据 $weatherData->setMeasurements(80, 70, 30.4); $weatherData->setMeasurements
= weatherData; weatherData.RegisterObserver(this); } public void Update ; public StatisticDisplay(Subject weatherData) { this.weatherData = (weatherData); StatisticDisplay statisticDisplay = new StatisticDisplay(weatherData); weatherData.SetMeasurements(23, 15, 20); weatherData.SetMeasurements(28, 12 , 25); weatherData.SetMeasurements(30, 14, 23); weatherData.SetMeasurements
type WeatherData struct { observers []Observer temperature float64 humidity float64 pressure float64 } func NewWeatherData() *WeatherData { return &WeatherData{ observers = append(wd.observers, o) } func (wd *WeatherData) RemoveObserver(o Observer) { wd.observers = (ForecastDisplay).Update = weatherData.RegisterObserver weatherData.SetMeasurements(80, 65, 30.4 ) weatherData.SetMeasurements(82, 70, 29.2) weatherData.SetMeasurements(78, 90, 29.2) } 总结
=0.06), WeatherData(time=2020-10-05 16:30:00, level=, value=0), WeatherData(time=2020-10-05 16:35:00, 16:45:00, level=, value=0), WeatherData(time=2020-10-05 16:50:00, level=小雨, value=0.04), WeatherData :00, level=, value=0), WeatherData(time=2020-10-05 17:15:00, level=, value=0), WeatherData(time=2020- 10-05 17:20:00, level=, value=0), WeatherData(time=2020-10-05 17:25:00, level=, value=0), WeatherData , value=0.02), WeatherData(time=2020-10-05 17:50:00, level=小雨, value=0.04), WeatherData(time=2020-10-
有一个气象站,由WeatherData对象负责追踪目前的天气情况(温度、湿度、气压)。现在需要建立一个应用,有三种布告板,分别显示目前的状况、气象统计及简单的预报。 image 现在已经提供了WeatherData类,对比刚才的需求,来做进一步的分析。 ? image 我们知道些什么? 一旦WeatherData有新的测量,这些布告必须马上更新 此系统必须可扩展,让其他开发人员建立定制的布告板,用户可以随心所欲地添加或删除任何布告板。 ; public CurrentConditionDisplay(Subject weatherData) { this.weatherData = weatherData; 对象 WeatherData weatherData = new WeatherData(); // 建立布告板,把WeatherData对象传给它们
实现 继承Observable,直接实现 public class WeatherData extends Observable { //天气状态 private String state } 测试 public class JDKObserverTest { public static void main(String[] args) { Observable weatherData =new WeatherData(); ObserverHZ hz=new ObserverHZ(); ObserverNJ nj=new ObserverNJ(); weatherData.addObserver(hz); weatherData.addObserver(nj); ((WeatherData) weatherData ).setState("温度29度,湿度30\""); //((WeatherData) weatherData).setChanged(); //weatherData.notifyObservers
概况 这套系统中主要包括三个部分:气象站(获取天气数据的物理设备)、WeatherData(追踪来自气象站的数据,并更新公告牌)、公告牌(用于展示天气数据) WeatherData知道如何跟气象站联系, 当天气数据有更新时,WeatherData会更新两个公告牌用于展示新的天气数据。 ;//气压 public CurrentConditionsDisplay(WeatherData weatherData) { this.weatherData = weatherData weatherData) { this.weatherData = weatherData; this.weatherData.registerObserver(this (weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); List
pressure float64 } func NewWeatherData() *WeatherData { return &WeatherData{} } func (w *WeatherData) RegisterObserver(o Observer) { w.observers = append(w.observers, o) } func (w *WeatherData = &CurrentConditionsDisplay{weatherData: weatherData} weatherData.RegisterObserver(c) return := NewWeatherData() NewCurrentConditionsDisplay(weatherData) weatherData.SetMeasurements(80 , 65, 30.4) weatherData.SetMeasurements(82, 70, 29.2) weatherData.SetMeasurements(78, 90, 29.2
; public CurrentConditionDisply(Subject weatherData) { this.weatherData = ; public StaisticsDisply(Subject weatherData) { this.weatherData = weatherData ; public ForcastDisply(Subject weatherData) { this.weatherData = weatherData static void Main(string[] args) { WeatherData weatherData = new WeatherData(); = new ForcastDisply(weatherData); weatherData.SetMeasurments(30, 65, 30.5F);
并将 接入方 currentConditions 传递到 WeatherData中 WeatherData weatherData = new WeatherData(currentConditions WeatherData weatherData = new WeatherData(); //创建观察者 CurrentConditions currentConditions = new CurrentConditions(); //注册到weatherData weatherData.registerObserver(currentConditions); weatherData.registerObserver WeatherData weatherData = new WeatherData(); //创建观察者 CurrentConditions currentConditions = , 看看信息"); weatherData.setData(10f, 100f, 30.3f); // 删除操作 weatherData.removeObserver(currentConditions
) { weatherData.registerObserver(this); } @Override public void update(float temp ) { weatherData.registerObserver(this); } @Override public void update(float temp weatherData = new WeatherData(); CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay (weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); weatherData.setMeasurements(0, 0, 0); weatherData.setMeasurements(1, 1, 1); } } CurrentConditionsDisplay.update
weatherData["daily"][0]["precip"] + " mm"; weatherDay1['Pressure'] = weatherData["daily"][0][ '] = weatherData["daily"][2]["tempMax"] + " °C"; weatherDay3['Humidity'] = weatherData["daily Sunset'] = weatherData["daily"][2]["sunset"]; weatherDay3['Condition'] = weatherData["daily"] weatherData["daily"][0]["precip"] + " mm"; weatherDay1['Pressure'] = weatherData["daily"][0][ '] = weatherData["daily"][2]["tempMax"] + " °C"; weatherDay3['Humidity'] = weatherData["daily
) { WeatherData weatherData = (WeatherData) o; Float humidity = weatherData.getHumidity ) { WeatherData weatherData = (WeatherData) o; Float pressure = weatherData.getPressure ) { WeatherData weatherData = (WeatherData) o; Float temperature = weatherData.getTemperature weatherData = new WeatherData(24.0F,152F,100F); // 观察者订阅 new HumidityObserver(weatherData); new PressureObserver(weatherData); new TemperatureObserver(weatherData); // 事件发生 weatherData.measurementsChanged
; public CurrentConditionsDisplay(Subject weatherData) { this.weatherData = weatherData; weatherData = new WeatherData(); CurrentConditionsDisplay currentDisplay = new (weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements ) { WeatherData weatherData = (WeatherData)obs; this.temperature = weatherData.getTemperature (weatherData,forecastDisplay); currentDisplay.update(weatherData,forecastDisplay); weatherData.setMeasurements
并将 接入方 currentConditions 传递到 WeatherData中 WeatherData weatherData = new WeatherData(currentConditions 当数据有更新时,就主动的调用 ArrayList, 通知所有的(接入方)就看到最新的信息 */ public class WeatherData implements Subject { WeatherData weatherData = new WeatherData(); //创建观察者 CurrentConditions currentConditions weatherData.registerObserver(currentConditions); weatherData.registerObserver(baiduSite 这样,我们增加观察者(这里可以理解成一个新的公告板),就不需要去修改核心类WeatherData不会修改代码,遵守了ocp原则。
weatherDay1['Sunrise'] = weatherData["daily"][0]["sunrise"]; weatherDay1['Sunset'] = weatherData = weatherData["daily"][1]["textDay"]; weatherDay2['UV Index'] = weatherData["daily"][1]["uvIndex weatherDay3['Sunrise'] = weatherData["daily"][2]["sunrise"]; weatherDay3['Sunset'] = weatherData weatherDay1['Sunrise'] = weatherData["daily"][0]["sunrise"]; weatherDay1['Sunset'] = weatherData weatherDay3['Sunrise'] = weatherData["daily"][2]["sunrise"]; weatherDay3['Sunset'] = weatherData
天气预报设计方案 1 - 普通方案 通过对气象站项目的分析,设计出一个WeatherData类 ? 说明 : 1)通过getXxx方法,可以让第三方接入,并得到相关信息。 并将 接入方 currentConditions 传递到 WeatherData 中 WeatherData weatherData = new WeatherData(currentConditions WeatherData weatherData = new WeatherData(); // 创建观察者 CurrentConditions currentConditions = new CurrentConditions(); BaiduSite baiduSite = new BaiduSite(); // 注册到weatherData weatherData.registerObserver 2)这样,我们增加观察者(可以理解为一个新的公告板),就不需要去修改核心类WeatherData不会修改代码,遵守类ocp原则。 观察者模式在JDK应用的源码分析 ? ? ?
并将 接入方 currentConditions 传递到 WeatherData 中 WeatherData weatherData = new WeatherData(currentConditions 2) 无法在运行时动态的添加第三方 (新浪网站) 3) 违反 ocp 原则=>观察者模式 //在 WeatherData 中,当增加一个第三方,都需要创建一个对应的第三方的公告板对象,并加入到 WeatherData weatherData = new WeatherData(); //创建观察者 CurrentConditions currentConditions weatherData.registerObserver(currentConditions); weatherData.registerObserver(baiduSite 2) 这样,我们增加观察者(这里可以理解成一个新的公告板),就不需要去修改核心类 WeatherData 不会修改代码,遵守了 ocp 原则
) { weatherData.reisterObserver(this); } @Override public void update(float temp ) { weatherData.registerObserver(this); } @Override public void update(float temp weatherData = new WeatherData(); CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay (weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); weatherData.setMeasurements(0, 0, 0); weatherData.setMeasurements(1, 1, 1); } } Java 中有哪些类用了观察者模式
= new WeatherData(); CurrentConditionsDisplay display = new CurrentConditionsDisplay(weatherData ); weatherData.setMeasurements(25.5f, 65, 1013.1f); weatherData.setMeasurements ; public CurrentConditionsDisplay(Subject weatherData) { this.weatherData = weatherData; = weatherData.getTemperature(); this.humidity = weatherData.getHumidity(); display(); ) { // 拉取数据 WeatherData wd = (WeatherData) o; // 或使用arg参数获取推送数据