觀察者模式:在對象之間定義一對多的依賴,當(dāng)一個對象改變狀態(tài),依賴他的對象都會收到通知,并自動更新。(以松耦合方式在一系列對象之間溝通狀態(tài),代表人物--MVC)
注意事項:主題(可觀察者)用一個共同的接口來更新觀察者,主題不知道觀察者的細(xì)節(jié),只知道觀察者實現(xiàn)了觀察者接口。使用此模式時,你可從被觀察者處推(push)或拉(pull)數(shù)據(jù)(推的方式被認(rèn)為更正確)。有多個觀察者時,不可以依賴特定的通知順序。java有多種觀察者模式的實現(xiàn),包括java.util.Observable(有一些違背設(shè)計原則的問題,有必要的話可以實現(xiàn)自己的Observable),JavaBeans,RMI等,Swing大量使用此模式,許多GUI框架也是如此。
用觀察者模式實現(xiàn)一個氣象站的實例:
要求當(dāng)WeatherData對象(接受物理氣象站數(shù)據(jù)的對象)更新數(shù)據(jù)時,隨即會更新三個布告板的顯示:目前狀況(溫度、濕度、氣壓)、氣象統(tǒng)計和天氣預(yù)報。并且公布API可以讓其他人寫出自己的布告板。
代碼:
//主題接口,提供注冊、刪除、更新布告板的方法
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();
}
//主題實現(xiàn)類
public class WeatherData implements Subject {
private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;
//構(gòu)造方法中初始化觀察者記錄
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);
}
}
//當(dāng)主題狀態(tài)改變時調(diào)用,以通知所有觀察者
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();
}
//更新數(shù)據(jù)方法
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
// other WeatherData methods here
public float getTemperature() {
return temperature;
}
public float getHumidity() {
return humidity;
}
public float getPressure() {
return pressure;
}
}
//觀察者實現(xiàn)類
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private Subject weatherData;
//構(gòu)造方法中把此觀察者注冊到主題
public CurrentConditionsDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
//修改觀察者狀態(tài)
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
//顯示布告板更新內(nèi)容
public void display() {
System.out.println("Current conditions: " + temperature
+ "F degrees and " + humidity + "% humidity");
}
}
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 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 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);
}
}
當(dāng)我們想添加一個布告板的時候只要實現(xiàn)觀察者借口就可以加入主題通知的記錄中,實際代碼如下:
//我們要添加一個酷熱指數(shù)的布告板,利用一套公式來計算酷熱指數(shù)。
public class HeatIndexDisplay implements Observer, DisplayElement {
float heatIndex = 0.0f;
private WeatherData weatherData;
public HeatIndexDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float t, float rh, float pressure) {
heatIndex = computeHeatIndex(t, rh);
display();
}
//酷熱指數(shù)計算
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 void display() {
System.out.println("Heat index is " + heatIndex);
}
}
//加入酷熱指數(shù)布告板后的測試類
public class WeatherStationHeatIndex {
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);
HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}
}