首页 技术 正文
技术 2022年11月6日
0 收藏 950 点赞 1,073 浏览 3063 个字

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,488
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289