1 概述
觀察者模式可以分為觀察者和被觀察者,觀察者通過(guò)注冊(cè)到一個(gè)被觀察者中,也可視為訂閱,當(dāng)被觀察者的數(shù)據(jù)發(fā)生改變時(shí),會(huì)通知到觀察者,觀察者可以據(jù)此做出反應(yīng)。
可以類(lèi)比訂閱報(bào)紙,報(bào)社就是被觀察者,訂閱者就是觀察者,訂閱者通過(guò)訂閱報(bào)紙與報(bào)社建立聯(lián)系,而報(bào)社有新報(bào)紙則主動(dòng)投遞給訂閱者。
2 實(shí)現(xiàn)
這里以Head First 設(shè)計(jì)模式中的觀察者模型為例。
講的是一個(gè)氣象監(jiān)測(cè)站模型,氣象站有三個(gè)傳感器,分別采集溫度、濕度和氣壓三個(gè)值。氣象站采集完數(shù)據(jù)之后會(huì)將數(shù)據(jù)設(shè)置到WeatherData對(duì)象中,而WeatherData數(shù)據(jù)更新后需要同時(shí)將數(shù)據(jù)更新到三個(gè)顯示裝置中。
這里就是使用了觀察者模式,WeatherData是數(shù)據(jù)中心,是被觀察者,而顯示裝置則是觀察者,當(dāng)觀察者訂閱之后,數(shù)據(jù)中心的變化都會(huì)主動(dòng)通知到觀察者。
這個(gè)模型的類(lèi)圖如下:
其中最重要的就是Subject和Observer接口,這里Subject就是被觀察者的總接口,而Observer接口則是觀察者總接口。Display接口則是因?yàn)槎鄠€(gè)顯示器都擁有共同的Display行為。
WeatherData實(shí)現(xiàn)Subject,成為一個(gè)具體的Subject,而三個(gè)Display則實(shí)現(xiàn)Observer接口,成為觀察者實(shí)例。
下面是代碼實(shí)例:
Subject接口
public interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}
擁有對(duì)Observer對(duì)象操作的注冊(cè)和刪除操作,也有通知各個(gè)Observer的方法。
Observer接口
public interface Observer {
void update(float temperature, float humidity, float pressure);
}
觀察者接口,擁有Observer的公用操作,Subject通過(guò)該接口來(lái)更新各個(gè)Observer中的數(shù)據(jù)。被觀察者其實(shí)就是通過(guò)這個(gè)接口來(lái)通知到各個(gè)觀察者的
DisplayElement接口
public interface DisplayElement {
public void display();
}
各個(gè)Display的公用方法
WeatherData類(lèi)
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) {
observers.remove(o);
}
public void notifyObservers() {
for (Observer observer : observers) {
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 float getTemperature() {
return temperature;
}
public float getHumidity() {
return humidity;
}
public float getPressure() {
return pressure;
}
}
具體的被觀察者類(lèi),會(huì)有一個(gè)存有所有觀察者對(duì)象的集合,當(dāng)數(shù)據(jù)變化時(shí),會(huì)遍歷這個(gè)集合來(lái)通知觀察者,而通知就是調(diào)用觀察者的update方法。
ForecastDisplay類(lèi)
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");
}
}
}
這是三個(gè)具體的觀察者其中的一個(gè),實(shí)現(xiàn)了Observer接口,并持有WeatherData對(duì)象,在ForecastDisplay對(duì)象創(chuàng)建的時(shí)候,會(huì)將自己加到WeatherData被觀察者對(duì)象的集合中保存。當(dāng)數(shù)據(jù)變化時(shí),WeatherData會(huì)從集合中遍歷到這個(gè)對(duì)象,并調(diào)用其update方法。
其他三個(gè)觀察者類(lèi)似。
測(cè)試代碼:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-613516.html
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);
weatherData.removeObserver(forecastDisplay);
weatherData.setMeasurements(62, 90, 28.1f);
}
}
三個(gè)觀察者顯示器獲取到了同樣的更新數(shù)據(jù),但是他們可以根據(jù)自身的顯示邏輯來(lái)做出不同的顯示結(jié)果文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-613516.html
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
Current conditions: 62.0F degrees and 90.0% humidity
Avg/Max/Min temperature = 75.5/82.0/62.0
Process finished with exit code 0
3 總結(jié)
- 觀察者加被觀察者組成觀察者模式
- 觀察者繼承Observer接口,該接口提供一個(gè)通知觀察者的方法
- 觀察者持有被觀察者的引用,在構(gòu)造方法中調(diào)用被觀察者的注冊(cè)方法將自身注冊(cè)為一個(gè)觀察者
- 被觀察者擁有一個(gè)觀察者集合,用于存儲(chǔ)所有注冊(cè)的觀察者的對(duì)象
- 觀察者可以自己調(diào)用注冊(cè)和注銷(xiāo)方法將自身添加到被觀察者的列表中或從列表中移除
- 被觀察者要通知觀察者時(shí),遍歷觀察者集合,調(diào)用觀察者接口中的方法通知觀察者
到了這里,關(guān)于設(shè)計(jì)模式——觀察者模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!