设计模式学习--观察者模式(Observer Pattern) (二)

2014-11-24 11:07:15 · 作者: · 浏览: 3
(i);
}
}

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

public void measurementsChanged() {
notifyObserver();
}

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

}

5. 定义四个布告板类实现观察者接口和布告板接口
[java]
package observerPattern;
/**
* 观察者类实现观察者接口和显示板接口
* @author wwj
*
*/
public class CurrentConditionDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private Subject weathderData;


public CurrentConditionDisplay(Subject weathderData) {
this.weathderData = weathderData;
weathderData.registerObserver(this); //注册
}

@Override
public void display() {
System.out.println("Current coditions: " + temperature + "F degress and " + humidity + "% humidity");
}

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

}

package observerPattern;
/**
* 观察者类实现观察者接口和显示板接口
* @author wwj
*
*/
public class CurrentConditionDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private Subject weathderData;


public CurrentConditionDisplay(Subject weathderData) {
this.weathderData = weathderData;
weathderData.registerObserver(this); //注册
}

@Override
public void display() {
System.out.println("Current coditions: " + temperature + "F degress and " + humidity + "% humidity");
}

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

}

[java]
package observerPattern;
/**
* 天气统计布告板
* @author wwj
*
*/
public class StatisticsDisplay implements Observer, DisplayElement {
private float maxTemp = 0.0f;; //最大温度
private float minTemp = 200; //最小温度
private float tempSum = 0.0f; //统计温度和
private int numReadings; //统计温度次数
private WeatherData weatherData;


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

@Override
public void display() {
System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings) + "/" + maxTemp + "/" + minTemp);
}

@Override
public void update(float temp, float humidity, float pressure) {
tempSum += temp;
numReadings++;

if(temp > maxTemp) {
maxTemp = temp;
}
if(temp < minTemp) {
minTemp = temp;
}

display();
}

}

package observerPattern;
/**
* 天气统计布告板
* @author wwj
*
*/
public class StatisticsDisplay implements Observer, DisplayElement {
private float maxTemp = 0.0f;; //最大温度
private float minTemp = 200; //最小温度
private float tempSum = 0.0f; //统计温度和
private int numReadings; //统计温度次数
private WeatherData weatherData;


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

@Override
publi