Java設計模式(2 / 23):觀察者模式

定義

觀察者(Observer)模式定義了對象之間的一對多依賴,這樣一來,當一個對象改變狀態時,它的所有依賴者都會收到通知并自動更新。
在這里插入圖片描述

OO設計原則:為了交互對象之間的松耦合設計而努力。

案例:氣象監測應用

概括

此系統中的三個部分是

  1. 氣象站(獲取實際氣象數據的物理裝置)
  2. WeatherData對象(追蹤來自氣象站的數據,并更新布告板)
  3. 布告板(顯示目前天氣狀況給用戶看)

WeatherData對象知道如何跟物理氣象站聯系,以取得更新的數據。

WeatherData對象會隨即更新三個布告板的顯示:

  1. 目前狀況(溫度、濕度、氣壓)、
  2. 氣象統計
  3. 天氣預報。

目標是建立一個應用,利用WeatherData對象取得數據,并更新三個布告板:目前狀況、氣象統計和天氣預報 。

在這里插入圖片描述

設計圖

在這里插入圖片描述

放碼過來

氣象站主程序

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);System.out.println("---");weatherData.setMeasurements(82, 70, 29.2f);System.out.println("---");weatherData.setMeasurements(78, 90, 29.2f);System.out.println("---Remove Observer---");weatherData.removeObserver(forecastDisplay);weatherData.setMeasurements(62, 90, 28.1f);}
}

布告板

打印展現接口

public interface DisplayElement {public void display();
}

主題

public interface Subject {public void registerObserver(Observer o);public void removeObserver(Observer o);public void notifyObservers();
}
實現主題接口的氣象數據
import java.util.*;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;}}

觀察者

public interface Observer {public void update(float temp, float humidity, float pressure);
}
實現觀察者接口的目前狀況布告板
public class CurrentConditionsDisplay implements Observer, DisplayElement {private float temperature;private float humidity;private WeatherData weatherData;public CurrentConditionsDisplay(WeatherData 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");}}
}

運行結果

氣象站主程序運行結果:

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
---Remove Observer---
Current conditions: 62.0F degrees and 90.0% humidity
Avg/Max/Min temperature = 75.5/82.0/62.0

現成輪子

Java API有內置的觀察者模式。java.util包內包含最基本的Observer接口與Observable類,這和案例的Subject接口與Observer接口很相似。

Observer接口與Observable類使用上更方便,因為許多功能都已經事先準備好了。

package java.util;@Deprecated(since="9")
public interface Observer {void update(Observable o, Object arg);
}
package java.util;@Deprecated(since="9")
public class Observable {private boolean changed = false;private Vector<Observer> obs;public Observable() {obs = new Vector<>();}public synchronized void addObserver(Observer o) {if (o == null)throw new NullPointerException();if (!obs.contains(o)) {obs.addElement(o);}}public synchronized void deleteObserver(Observer o) {obs.removeElement(o);}public void notifyObservers() {notifyObservers(null);}public void notifyObservers(Object arg) {/** a temporary array buffer, used as a snapshot of the state of* current Observers.*/Object[] arrLocal;synchronized (this) {/* We don't want the Observer doing callbacks into* arbitrary code while holding its own Monitor.* The code where we extract each Observable from* the Vector and store the state of the Observer* needs synchronization, but notifying observers* does not (should not).  The worst result of any* potential race-condition here is that:* 1) a newly-added Observer will miss a*   notification in progress* 2) a recently unregistered Observer will be*   wrongly notified when it doesn't care*/if (!changed)return;arrLocal = obs.toArray();clearChanged();}for (int i = arrLocal.length-1; i>=0; i--)((Observer)arrLocal[i]).update(this, arg);}public synchronized void deleteObservers() {obs.removeAllElements();}protected synchronized void setChanged() {changed = true;}protected synchronized void clearChanged() {changed = false;}public synchronized boolean hasChanged() {return changed;}public synchronized int countObservers() {return obs.size();}
}

注意:Observer和Observable在Java 9標記為廢棄。

Deprecated. This class and the Observer interface have been deprecated. The event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using the java.beans package. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the java.util.concurrent package. For reactive streams style programming, see the java.util.concurrent.Flow API.

Link

參考資料

  1. 《Head First 設計模式》
  2. Observable (Java SE 9 & JDK 9 )
  3. Java 9:Observer和Observable廢棄原因及解決方案

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445600.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445600.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445600.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

二叉樹概述

各種實現和應用以后放鏈接 一、二叉樹的基本概念 二叉樹&#xff1a;二叉樹是每個節點最多有兩個子樹的樹結構。 根節點&#xff1a;一棵樹最上面的節點稱為根節點。 父節點、子節點&#xff1a;如果一個節點下面連接多個節點&#xff0c;那么該節點稱為父節點&#xff0c;它…

Java設計模式(1 / 23):策略模式

定義 策略&#xff08;Strategy&#xff09;模式定義了算法族&#xff0c;分別封裝起來&#xff0c;讓它們之間可以互相替換 &#xff0c;此模式讓算法的變化獨立于使用算法的客戶。 案例&#xff1a;模擬鴨子應用 一開始 新需求&#xff1a;模擬程序需要會飛的鴨子 在父類新…

Java設計模式(3 / 23):裝飾者模式

文章目錄定義案例1&#xff1a;三點幾啦首次嘗試再次嘗試設計原則&#xff1a;類應該對擴展開放&#xff0c;對修改關閉嘗用裝飾者模式裝飾者模式特征本例的類圖放碼過來飲料類HouseBlendDarkRoastEspressoDecaf調料裝飾類MilkMochaSoyWhip運行測試類案例2&#xff1a;編寫自己…

c語言知識體系

原文&#xff1a;https://blog.csdn.net/lf_2016/article/details/80126296#comments

《游戲編程入門 4th》筆記(1 / 14):Windows初步

文章目錄Windows編程概述獲取Windows理解Windows消息機制多任務多線程事件處理DirectX快速概覽Direct3D是什么Window程序基礎創建第一個Win32項目理解WinMainWinMain函數調用完整的WinMainGetMessage函數調用尋求幫助Windows編程概述 DirectX&#xff0c;流行的游戲編程庫。它…

17校招真題題集(1)1-5

注&#xff1a;本系列題目全是按照通過率降序來排列的&#xff0c;基本保證題目難度遞增。 1、 題目名稱&#xff1a;游戲任務標記 來源&#xff1a;騰訊 題目描述 游戲里面有很多各式各樣的任務&#xff0c;其中有一種任務玩家只能做一次&#xff0c;這類任務一共有1024個…

《游戲編程入門 4th》筆記(2 / 14):監聽Windows消息

文章目錄編寫一個Windows程序理解InitInstanceInitInstance函數調用InitInstance的結構理解MyRegisterClassMyRegisterClass函數調用MyRegisterClass的作用揭露WinProc的秘密WinProc函數調用WinProc的大秘密什么是游戲循環The Old WinMain對持續性的需要實時終止器WinMain和循環…

17校招真題題集(2)6-10

注&#xff1a;本系列題目全是按照通過率降序來排列的&#xff0c;基本保證題目難度遞增。 6、 題目名稱&#xff1a;Fibonacci數列 來源&#xff1a;網易 題目描述 Fibonacci數列是這樣定義的&#xff1a; F[0] 0 F[1] 1 for each i ≥ 2: F[i] F[i-1] F[i-2] 因此&am…

QT5的數據庫

#include <QtSql> QT sql QSqlDatabase類實現了數據庫連接的操作 QSqlQuery類執行SQL語句 QSqlRecord類封裝數據庫所有記錄 QSqlDatabase類 [cpp] view plaincopy print?QSqlDatabase db QSqlDatabase::addDatabase("QOCI"); db.setHostName("localh…

數據結構課上筆記6

本節課介紹了單鏈表的操作實現細節&#xff0c;介紹了靜態鏈表。 鏈表帶頭的作用&#xff1a;對鏈表進行操作時&#xff0c;可以對空表、非空表的情況以及 對首元結點進行統一處理&#xff0c;編程更方便。 下面給出帶頭的單鏈表實現思路&#xff1a; 按下標查找&#xff1a; …

《Unity2018入門與實戰》筆記(9 / 9):個人總結

個人總結 腳本語言學習的竅門 盡可能多讀、多寫、多說腳本語言&#xff01; Link 游戲制作步驟 設計游戲時一般會遵循5個步驟&#xff1a; 羅列出畫面上所有的對象。確定游戲對象運行需要哪些控制器腳本。確定自動生成游戲對象需要哪些生成器腳本。準備好用于更新UI的調度…

17校招真題題集(3)11-15

注&#xff1a;本系列題目全是按照通過率降序來排列的&#xff0c;基本保證題目難度遞增。 11、 題目名稱&#xff1a;買蘋果 來源&#xff1a;網易 題目描述 小易去附近的商店買蘋果&#xff0c;奸詐的商販使用了捆綁交易&#xff0c;只提供6個每袋和8個每袋的包裝(包裝不…

Qt學習:QDomDocument

QDomDocument類代表了一個XML文件 QDomDocument類代表整個的XML文件。概念上講&#xff1a;它是文檔樹的根節點&#xff0c;并提供了文檔數據的基本訪問方法。由于元素、文本節點、注釋、指令執行等等不可能脫離一個文檔的上下文&#xff0c;所以文檔類也包含了需要用來創建這些…

《事實:用數據思考,避免情緒化決策》筆記

文章目錄一分為二負面思維直線思維恐懼本能規模錯覺以偏概全命中注定單一視角歸咎他人情急生亂一分為二 要做到實事求是&#xff0c; 就要做到當你聽到一分為二的說法時&#xff0c; 你就能迅速認識到這種說法描述的是一種兩極分化的圖畫&#xff0c; 而兩極之間存在一道巨大的…

順序存儲線性表實現

在計算機中用一組地址連續的存儲單元依次存儲線性表的各個數據元素,稱作線性表的順序存儲結構。 順序存儲結構的主要優點是節省存儲空間&#xff0c;因為分配給數據的存儲單元全用存放結點的數據&#xff08;不考慮c/c語言中數組需指定大小的情況&#xff09;&#xff0c;結點之…

QT5生成.exe文件時,出現缺少QT5core.dll文件解決方法

在 http://qt-project.org/downloads 下載Qt SDK安裝需要Qt版本。在QtCreator下&#xff0c;程序可以正常運行&#xff0c;但是當關閉QtCreator后&#xff0c;在DeBug目錄下再運行相應的*.exe程序時&#xff0c;會提示缺少Qt5Core.dll錯誤。解決方法&#xff1a;添加電腦環境變…

《基于Java實現的遺傳算法》筆記(7 / 7):個人總結

文章目錄為何采用遺傳算法哪些問題適合用遺傳算法解決遺傳算法基本術語一般遺傳算法的過程基本遺傳算法的偽代碼為何采用遺傳算法 遺傳算法是機器學習的子集。在實踐中&#xff0c;遺傳算法通常不是用來解決單一的、特定問題的最好算法。對任何一個問題&#xff0c;幾乎總有更…

單鏈表不帶頭標準c語言實現

鏈表是一種物理存儲單元上非連續、非順序的存儲結構&#xff0c;數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。鏈表由一系列結點&#xff08;鏈表中每一個元素稱為結點&#xff09;組成&#xff0c;結點可以在運行時動態生成。每個結點包括兩個部分&#xff1a;一個是…

Java設計模式(4 / 23):單例模式

文章目錄單例模式的應用場景餓漢式單例模式懶漢式單例模式改進&#xff1a;synchronized改進&#xff1a;雙重檢查鎖改進&#xff1a;靜態內部類破壞單例用反射破壞單例用序列化破壞單例解密注冊式單例模式枚舉式單例模式解密容器式單例線程單例實現ThreadLocal單例模式小結參考…

約瑟夫環-(數組、循環鏈表、數學)

約瑟夫環&#xff08;約瑟夫問題&#xff09;是一個數學的應用問題&#xff1a;已知n個人&#xff08;以編號1&#xff0c;2&#xff0c;3...n分別表示&#xff09;圍坐在一張圓桌周圍。從編號為k的人開始報數&#xff0c;數到m的那個人出列&#xff1b;他的下一個人又從1開始報…