定义:在对象之间定义一对多的以来,这样一来,当一个对象改变状态,依赖它的对象都会收到通知,并自动更新。
观察者模式相当的普遍,这里使用一个head first书里的一个案例来加深印象。这里是自己的类,java有自己的观察者实现,不过是一个具体的类,并不符合“根据接口编程”原则和“少用继承,多用集合”的原则,而且也不能多重继承,所以复杂的情况下一般都需要自己写观察者模式的代码。
一、主题接口
//总分类
public interface Subject {
void registerObserver(Observer o);//注册观察者
void removeOberver(Observer o);//删除观察者
void notifyObservers();//通知观察者
}
二、观察者们实现的不变方法(这里紧紧是展示一句话)
public interface DisplayElement {
void display();//显示
}
三、观察者们实现的可变方法(通知的参数不同)
public interface Observer {
void update(float temp,float humidity,float pressure);//通知参数
}
四、实现总分类的接口
//所有分类的总实现类,下面的各个子类都是调用了该总类
public class WeatherDate implements Subject {
private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherDate() {//生成观察者集合
observers = new ArrayList();
}
@SuppressWarnings("unchecked")
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void removeOberver(Observer o) {
int i = observers.indexOf(o);
if(i>0){
observers.remove(i);
}
}
@Override
public void notifyObservers() {//通知每一个观察者
for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer)observers.get(i);
observer.update(temperature,humidity,pressure);
}
}
//定义两个为了测试用的方法
public void measurementsChanged(){
notifyObservers();//起始条件:得到新数值,通知所有观察者
}
public void setMeasurements(float temperature, float humidity, float pressure) {//测试用,得到数据
this.temperature = temperature;//这三个参数可以直接就是一个观察者的list
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}
五、其中一个分类的具体实现
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private Subject weatherDate;
public CurrentConditionsDisplay(Subject weatherDate) {//得到起始值注册用
this.weatherDate = weatherDate;
weatherDate.registerObserver(this);
}
@Override
public void update(float temp, float humidity, float pressure) {//传递的参数不同
temperature = temp;
this.humidity = humidity;
display();
}
@Override
public void display() {
System.out.println("CurrentConditionsDisplay:"+temperature+"\t"+humidity);
}
}
六、另一个子类
public class HeatIndexDisplay implements Observer,DisplayElement{
private Subject weatherDate;
private float index;
private float computeHeatIndex(float t, float rh) {
float index = (float)((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) +
(0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) +
(0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +
(0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 *
(rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +
(0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +
0.000000000843296 * (t * t * rh * rh * rh)) -
(0.0000000000481975 * (t * t * t * rh * rh * rh)));
return index;
}
public HeatIndexDisplay(Subject weatherDate) {
this.weatherDate = weatherDate;
weatherDate.registerObserver(this);
}
@Override
public void update(float temp, float humidity, float pressure) {
this.index = computeHeatIndex(temp, humidity);
display();
}
@Override
public void display() {
System.out.println("HeatIndexDisplay:"+index);
}
}
……
七、测试
public class WeatherStation {
public static void main(String[] args) {
WeatherDate weatherDate = new WeatherDate();
//给主题注册两个观察者,会保存进观察者集合中
CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherDate);
HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherDate);
//会通知所有的观察者
weatherDate.setMeasurements(80, 65, 30.4f);
weatherDate.setMeasurements(82, 70, 29.2f);
weatherDate.setMeasurements(78, 90, 29.2f);
}
}
//输出
CurrentConditionsDisplay:80.0 65.0
HeatIndexDisplay:82.95535
CurrentConditionsDisplay:82.0 70.0
HeatIndexDisplay:86.90124
CurrentConditionsDisplay:78.0 90.0
HeatIndexDisplay:83.64967