java 自带观察者模式_设计模式 - 观察者模式(Observer Pattern) Java内置 用法

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

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

2aaee95ef6ff1ddd68ed676b1fce5ccd.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值