观察者模式(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

注意: 通知的顺序是先入后出.

最新文章

  1. win7怎么显示隐藏文件夹
  2. js控制固定div和随屏滚动div兼容多浏览器和纯css控制(来自网络)
  3. NHibernate教程
  4. ubuntu下arm-linux-gcc安装
  5. oracle 表连接 - hash join 哈希连接
  6. android Spinner 续
  7. sql相关
  8. 架构师必备软件:安装Dubbo注册中心(Zookeeper-3.4.6)
  9. bat脚本:windows下一键启动zookeeper+kafka
  10. 自己封装element-ui树组件的过滤
  11. 精读《正则 ES2018》
  12. Mittag-Leffer函数, Matlab内部函数
  13. 全局解释器锁 GIL
  14. 3.python中的基本概念
  15. MyBatis之one2one与one2many
  16. 【转】每天一个linux命令(45):free 命令
  17. 2018 Multi-University Training Contest 3
  18. web.config设置之system.webServer 详细介绍,为网站设置默认文档
  19. ubantu 安装tree命令
  20. libevent源码深度剖析十

热门文章

  1. Python多态、鸭子类型
  2. python从2.6.x升级到2.7.x
  3. python开发web服务器——搭建简易网站
  4. [ python ] 集合的使用
  5. linux的fwrite()使用方法,当前时间写入文本的程序
  6. js复制文字
  7. 【PAT】1009. 说反话 (20)
  8. 实现手机端上下左右滑屏的jq原生代码和使用库·两种办法
  9. 【LOJ】#2131. 「NOI2015」寿司晚宴
  10. openssl解析国密X509证书