观察者模式(Observer Pattern) Java内置 用法
本文地址: http://blog.csdn.net/caroline_wendy/article/details/26601659
观察者模式(observer pattern)具体解释, 參见: http://blog.csdn.net/caroline_wendy/article/details/26583157
Java内置的观察者模式, 是通过继承父类, 实现观察者模式的几个主要函数:
Observerable(可被观察的): 是一个父类(class),addObserver(), 加入观察者; deleteObserver(), 删除观察者;
notifyObservers(), 通知观察者;setChanged(), 确认更改;
Observer(观察者): 是一个接口(interface), update(), 更新观察者数据;
setChanged()->notifyObservers(), 必需要先使用setChanged(), 再使用notifyObservers(), 即先确认提交, 再通知观察者;
观察者的更新接口update(Observerable o, Object arg), 即能够使用推(push), 也能够使用拉(pull);
假设notifyObservers(arg), 传递參数, 则为推(push)的方法, 假设没有參数, 则为拉(pull)的方式, 即使用get()方法获取;
观察者的通知(notify)顺序是先入后出的模式.
Observerable(可被观察的) 的 代码:
/** * @time 2014年5月22日 */ package observer.java; import java.util.Observable; /** * @author C.L.Wang * */ public class WeatherData extends Observable { private float temperature; private float humidity; private float pressure; public WeatherData() {} public void measurementsChanged() { setChanged(); 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; } }
Observer(观察者)的代码:
/** * @time 2014年5月22日 */ package observer.java; import java.util.Observable; import java.util.Observer; /** * @author C.L.Wang * */ public class CurrentConditionsDisplay implements Observer, DisplayElement { Observable observable; private float temperature; private float humidity; public CurrentConditionsDisplay(Observable observable) { this.observable = observable; observable.addObserver(this); } /* (non-Javadoc) * @see observer.java.DisplayElement#display() */ @Override public void display() { // TODO Auto-generated method stub System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } /* (non-Javadoc) * @see java.util.Observer#update(java.util.Observable, java.lang.Object) */ @Override public void update(Observable o, Object arg) { // TODO Auto-generated method stub if (o instanceof WeatherData) { WeatherData weatherData = (WeatherData)o; this.temperature = weatherData.getTemperature(); this.humidity = weatherData.getHumidity(); display(); } } }
其余代码不一一列出, 类似參见: http://blog.csdn.net/caroline_wendy/article/details/26583157
測试代码:
package observer.java; public class WeatherStation { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentConditions = 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); } }
输出:
Forecast: Improving weather on the way! Avg/Max/Min temperature = 80.0/80.0/80.0 Current conditions: 80.0F degrees and 65.0% humidity Forecast: Watch out for cooler, rainy weather Avg/Max/Min temperature = 81.0/82.0/80.0 Current conditions: 82.0F degrees and 70.0% humidity Forecast: More of the same Avg/Max/Min temperature = 80.0/82.0/78.0 Current conditions: 78.0F degrees and 90.0% humidity
注意: 通知的顺序是先入后出.