学习笔记之观察者模式

观察者(observer)模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。

observer模式中有两个关键要素,一是Observer,一是Subject,
Subject就是被观察者,它一般是事件的来源,
Observer是观察者,它一般是事件发生的时候应该进行相应的操作的对象。

[code]
package org.kangsg219.intf;

public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}

[/code]
[code]
package org.kangsg219.intf;

public interface Observer {
public void update(float temperature,float humidity,float pressure);
}

[/code]
[code]
package org.kangsg219.intf;

public interface DisplayElement {
public void display();

}

[/code]
[code]
package org.kangsg219.impl;

import java.util.ArrayList;
import java.util.List;

import org.kangsg219.intf.Observer;
import org.kangsg219.intf.Subject;

public class WeatherData implements Subject {

private List<Observer> observers;

private float temperature;

private float humidity;

private float pressure;

public WeatherData() {
observers = new ArrayList<Observer>();
}

public void registerObserver(Observer o) {
observers.add(o);
}

public void removeObserver(Observer o) {
int i=observers.indexOf(o);
if(i>=0){
observers.remove(i);
}
}

public void notifyObservers() {
for(int i=0;i<observers.size();i++){
Observer tempo=observers.get(i);
tempo.update(temperature, humidity, pressure);
}
}

public void measurementsChanged(){
notifyObservers();
}

public void setMeasurements(float temperature,float humidity,float pressure){
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
measurementsChanged();
}

}

[/code]
[code]
package org.kangsg219.impl;

import org.kangsg219.intf.DisplayElement;
import org.kangsg219.intf.Observer;
import org.kangsg219.intf.Subject;

public class CurrentConditionDisplay implements Observer,DisplayElement {

private float temperature;

private float humidity;

private float pressure;

@SuppressWarnings("unused")
private Subject weatherData;

public CurrentConditionDisplay(Subject weatherData){
this.weatherData=weatherData;
weatherData.registerObserver(this);
}

public void update(float temperature, float humidity, float pressure) {
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
display();

}

public void display() {
System.out.println("CurrentConditionDisplay:");
System.out.println("temperature:"+temperature+" "+"humidity:"+humidity+" "+"pressure:"+pressure);
System.out.println("=============================================");
}

}

[/code]
[code]
package org.kangsg219.impl;

import org.kangsg219.intf.DisplayElement;
import org.kangsg219.intf.Observer;
import org.kangsg219.intf.Subject;

public class CurrentConditionDisplay implements Observer,DisplayElement {

private float temperature;

private float humidity;

private float pressure;

@SuppressWarnings("unused")
private Subject weatherData;

public CurrentConditionDisplay(Subject weatherData){
this.weatherData=weatherData;
weatherData.registerObserver(this);
}

public void update(float temperature, float humidity, float pressure) {
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
display();

}

public void display() {
System.out.println("CurrentConditionDisplay:");
System.out.println("temperature:"+temperature+" "+"humidity:"+humidity+" "+"pressure:"+pressure);
System.out.println("=============================================");
}

}

[/code]
[code]
package org.kangsg219.impl;

import org.kangsg219.intf.DisplayElement;
import org.kangsg219.intf.Observer;
import org.kangsg219.intf.Subject;

public class StatisticsDisplay implements Observer, DisplayElement {

private float temperature;

private float humidity;

private float pressure;

@SuppressWarnings("unused")
private Subject weatherData;

public StatisticsDisplay(Subject weatherData){
this.weatherData=weatherData;
weatherData.registerObserver(this);
}

public void update(float temperature, float humidity, float pressure) {
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
display();

}

public void display() {
System.out.println("StatisticsDisplay:");
System.out.println("temperature:"+temperature+" "+"humidity:"+humidity+" "+"pressure:"+pressure);
System.out.println("=============================================");
}

}

[/code]
[code]
package org.kangsg219.test;

import org.kangsg219.impl.CurrentConditionDisplay;
import org.kangsg219.impl.ForecastDisplay;
import org.kangsg219.impl.StatisticsDisplay;
import org.kangsg219.impl.WeatherData;

public class WeatherStation {

public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionDisplay ccd=new CurrentConditionDisplay(weatherData);
new StatisticsDisplay(weatherData);
new ForecastDisplay(weatherData);
weatherData.setMeasurements(33, 30, 30.4F);
weatherData.removeObserver(ccd);
weatherData.setMeasurements(34, 35, 30.4F);
}

}

[/code]

[b]java API中内置了观察者模式[/b]
[code]
package org.kangsg219.intf;

import java.util.Observer;

public interface IObserver extends Observer {
public void display();
}

[/code]
[code]
package org.kangsg219.impl;

import java.util.Observable;

public class WeatherData2 extends Observable {


private float temperature;

private float humidity;

private float pressure;

public void measurementsChanged(){
setChanged();
System.out.println("before notifyObservers,hasChanged="+hasChanged());
notifyObservers();
System.out.println("after notifyObservers,hasChanged="+hasChanged());
}

public void setMeasurements(float temperature,float humidity,float pressure){
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
measurementsChanged();
}

public float getHumidity() {
return humidity;
}

public float getPressure() {
return pressure;
}

public float getTemperature() {
return temperature;
}
}

[/code]
[code]
package org.kangsg219.impl;

import java.util.Observable;

import org.kangsg219.intf.IObserver;

public class WeatherDisplay implements IObserver {

private float temperature;

private float humidity;

private float pressure;

@SuppressWarnings("unused")
private Observable observerable;

public WeatherDisplay(Observable observerable){
this.observerable=observerable;
observerable.addObserver(this);
}

public void update(Observable o, Object arg) {
if(o instanceof WeatherData2){
WeatherData2 weatherdata2=(WeatherData2)o;
this.humidity=weatherdata2.getHumidity();
this.temperature=weatherdata2.getTemperature();
this.pressure=weatherdata2.getPressure();
display();
}

}

public void display(){
System.out.println("WeatherDisplay:");
System.out.println("temperature:"+temperature+" "+"humidity:"+humidity+" "+"pressure:"+pressure);
System.out.println("=============================================");
}
}

[/code]
[code]
package org.kangsg219.test;

import org.kangsg219.impl.WeatherData2;
import org.kangsg219.impl.WeatherDisplay;

public class WeatherStation2 {


public static void main(String[] args) {
WeatherData2 weatherdata2=new WeatherData2();
WeatherDisplay wdisplay=new WeatherDisplay(weatherdata2);
new WeatherDisplay(weatherdata2);
weatherdata2.setMeasurements(30, 30, 30F);
weatherdata2.deleteObserver(wdisplay);
weatherdata2.setMeasurements(33, 33, 33.3F);
}

}

[/code]


java api 中内置的观察者模式是怎么实现的我们并不清楚,它对结果的输出顺序我们也无法把握,有必要的话建议自己实现这个模式!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值