Head First 设计模式学习笔记(二)观察者模式
观察者模式
?
package com.weather;public interface Subject {public void registerObserver(Observer o);public void removeObserver(Observer o);public void notifyObservers();}?
package com.weather;public interface Observer {public void update(float temp, float humidity, float pressure);}?
package com.weather;import java.util.ArrayList;public class WeatherData implements Subject {private ArrayList observers;private float temperature;private float humidity;private float pressure;public WeatherData() {observers = new ArrayList();}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();}// WeatherData的其他方法}?
package com.weather;public interface DisplayElement {public void display();}?
package com.weather;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");}}