设计模式笔记_二_观察者模式
最后更新于
最后更新于
在观察者模式中,会改变的是主题的状态,以及观察者的数目和类型。用这个模式,可以改变依赖于主题状态的对象,却不必改变主题。观察者模式利用“组合”将许多观察者组合进主题。对象之间的这种关系不是通过继承产生的,而是在运行时利用组合的方式产生。主题与观察者都使用接口:观察者利用主题的接口向主题注册,而主题利用观察者接口通知观察者。这样可以让两者之间运作正常,又同时具有松耦合的优点。/**
* 观察者模式案例
* 气象站主题接口
*/
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}/**
* 观察者模式案例
* 气象站观察者接口
*/
public interface Observer {
public void update(float temp, float humidity, float pressure);
}/**
* 观察者模式案例 - 气象站
* 布告板接口
*/
public interface DisplayElement {
public void display();
}import java.util.ArrayList;
/**
* 观察者模式案例 - 气象站
*/
public class WeatherData implements Subject {
private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData() {
this.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 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;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}/**
* 观察者模式案例 - 气象站
* 目前状况布告板
*/
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("Current conditions:" + temperature + "F degrees" +
" and " + humidity + "% humidity");
}
}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);
}
public void update(float temp, float humidity, float pressure) {
tempSum += temp;
numReadings++;
if (temp > maxTemp) {
maxTemp = temp;
}
if (temp < minTemp) {
minTemp = temp;
}
display();
}
public void display() {
System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)
+ "/" + maxTemp + "/" + minTemp);
}
}public class ForecastDisplay implements Observer, DisplayElement {
private float currentPressure = 29.92f;
private float lastPressure;
private WeatherData weatherData;
public ForecastDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure) {
lastPressure = currentPressure;
currentPressure = pressure;
display();
}
public void display() {
System.out.print("Forecast: ");
if (currentPressure > lastPressure) {
System.out.println("Improving weather on the way!");
} else if (currentPressure == lastPressure) {
System.out.println("More of the same");
} else if (currentPressure < lastPressure) {
System.out.println("Watch out for cooler, rainy weather");
}
}
}/**
* 观察者模式案例 - 气象站
* 启动气象站
*/
public class WeatherStation {
public static void main(String args[]) {
WeatherData weatherData = new WeatherData(); // 气象站:主题
CurrentConditionsDisplay currentDisplay = 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); // 主题更新数据
}
}Note: ./WeatherData.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Current conditions:80.0F degrees and 65.0% humidity
Avg/Max/Min temperature = 80.0/80.0/80.0
Forecast: Improving weather on the way!
Current conditions:82.0F degrees and 70.0% humidity
Avg/Max/Min temperature = 81.0/82.0/80.0
Forecast: Watch out for cooler, rainy weather
Current conditions:78.0F degrees and 90.0% humidity
Avg/Max/Min temperature = 80.0/82.0/78.0
Forecast: More of the same