设计模式:观察者
2016-12-15 00:00
113 查看
现在,我们要模拟气象站观测天气,然后将天气预报给不同的对象去显示的情况。
首先,模拟新建一个气象站。其中WeatherData 看作为一个天气发布器,由WeatherStation 获取信息并传送给WeatherData。由WeatherData去通知各个对象显示。在新建发布器之后,就应该新建发布器要通知的信息的那些显示器。然后注册给WeatherData,让WeatherData知道当有新数据过来的时候通知那些对象进行相应处理。
接下来是天气发布器的接口。定义和观察者相关的一些方法。
发布器的实现:每次有新值过来的时候调用measurementsChanged()方法。
measurementsChanged()调用notifyObservers()方法。通知各个显示器进行处理。
接下来要实现气温更新(即观察者,这里的观察者定义为一个行为。)。
接下来是显示器。
最后是各种显示器的实现。
我感觉这么些表述意义不明。 观察者应该为
他观察气象站气温改变,然后通知各个显示器显示。由它获取数据,由它分发数据。
首先,模拟新建一个气象站。其中WeatherData 看作为一个天气发布器,由WeatherStation 获取信息并传送给WeatherData。由WeatherData去通知各个对象显示。在新建发布器之后,就应该新建发布器要通知的信息的那些显示器。然后注册给WeatherData,让WeatherData知道当有新数据过来的时候通知那些对象进行相应处理。
public class WeatherStation { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); weatherData.setMeasurements(78, 90, 29.2f); } }
接下来是天气发布器的接口。定义和观察者相关的一些方法。
public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObservers(); }
发布器的实现:每次有新值过来的时候调用measurementsChanged()方法。
measurementsChanged()调用notifyObservers()方法。通知各个显示器进行处理。
import java.util.ArrayList; public class WeatherData implements Subject { private ArrayList<Observer> observers; private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList<Observer>(); } public void registerObserver(Observer o) { observers.add(o); } public void removeObserver(Observer o) { int i = observers.indexOf(o); if (i >= 0) { observers.remove(i); } } public void notifyObservers() { for (Observer observer : observers) { observer.update(temperature, humidity, pressure); } } public void measurementsChanged() { notifyObservers(); } public void setMeasurements(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } public float getTemperature() { return temperature; } public float getHumidity() { return humidity; } public float getPressure() { return pressure; } }
接下来要实现气温更新(即观察者,这里的观察者定义为一个行为。)。
public interface Observer { public void update(float temp, float humidity, float pressure); }
接下来是显示器。
public interface DisplayElement { public void display(); }
最后是各种显示器的实现。
public class CurrentConditionsDisplay implements Observer, DisplayElement { private float temperature; private float humidity; private Subject weatherData; public CurrentConditionsDisplay(Subject weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); } public void update(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; display(); } public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } }
public class StatisticsDisplay implements Observer, DisplayElement { private float maxTemp = 0.0f; private float minTemp = 200; private float tempSum= 0.0f; private int numReadings; private WeatherData weatherData; public StatisticsDisplay(WeatherData weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); } public void update(float temp, float humidity, float pressure) { tempSum += temp; 7fe0 numReadings++; if (temp > maxTemp) { maxTemp = temp; } if (temp < minTemp) { minTemp = temp; } display(); } public void display() { System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings) + "/" + maxTemp + "/" + minTemp); } }
package designpatterns.observer.weather; public class ForecastDisplay implements Observer, DisplayElement { private float currentPressure = 29.92f; private float lastPressure; private WeatherData weatherData; public ForecastDisplay(WeatherData weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); } public void update(float temp, float humidity, float pressure) { lastPressure = currentPressure; currentPressure = pressure; display(); } public void display() { System.out.print("Forecast: "); if (currentPressure > lastPressure) { System.out.println("Improving weather on the way!"); } else if (currentPressure == lastPressure) { System.out.println("More of the same"); } else if (currentPressure < lastPressure) { System.out.println("Watch out for cooler, rainy weather"); } } }
我感觉这么些表述意义不明。 观察者应该为
WeatherData
他观察气象站气温改变,然后通知各个显示器显示。由它获取数据,由它分发数据。
相关文章推荐
- 设计模式:装饰者
- 设计模式:工厂
- 设计模式:单例
- 设计模式:命令
- 设计模式:适配器
- 微信SDK使用demo
- jquery.datetimepicker 使用感悟
- mongo的密集索引与稀疏索引
- Chrome 扩展 SecureShell 删除指定 known host fingerprint
- 1613-3-傅溥衍 总结《2016年12月14日》【连续第七十五天总结】
- docker 出现 client is newer than server 的解决方案
- redis-game-transaction
- 向tomcat学习jmx
- 自己开发的一个轻量级的记录工具,欢迎拍砖,觉得有意思的可以一起来开发。
- C语言求ln2
- Virtualbox设置虚拟机访问外网以及主机访问虚拟机
- thrift入门教程
- vs2013编译OGRE::error c1060 compiler is out of heap space
- 微信小程序调查报告(二)-账号注册篇
- dataTables本地刷新数据解决只能初始化一次问题