?通俗的說:我們向報社訂閱報紙,這個就是觀察者模式的實例,我們是觀察者(Observer),報社就是主題(Subject)。
?
觀察者模式定義了對象之間的一對多依賴,這樣一來,當(dāng)一個對象改變狀態(tài)時,它的所有依賴者都會收到通知并自動更新。
?
?現(xiàn)在我們設(shè)計一個氣象監(jiān)測站系統(tǒng),此系統(tǒng)中的三個部分分別是氣象站(獲取實際氣象數(shù)據(jù))、布告板(顯示當(dāng)前的天氣狀況)
?
、WeatherData(跟蹤來自氣象站的數(shù)據(jù),并更新布告板),布告板可以提供擴展。
?
系統(tǒng)設(shè)計類圖如下:
?
/** * 主題 * @author wengn * */ public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObserver(); } /** * 觀察者 * @author wengn * */ public interface Observer { public void update(float temp,float humidity,float pressure); } /** * 布告板接口 * @author wengn * */ public interface DisplayElement { public void display(); } /** * 主題實現(xiàn)類 * @author Administrator * */ public class WeatherData implements Subject { private ArrayList observers; private float temperature; private float humidity; private float pressure; public WeatherData(){ observers = new ArrayList(); } public void notifyObserver() { for(int i=0;i<observers.size();i++){ Observer o = (Observer)observers.get(i); o.update(temperature, humidity, pressure); } } 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 measurementsChanged(){ this.notifyObserver(); } public void setMeasurements(float temperature,float humidity,float pressure){ this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } } /** * 布告板實現(xiàn)類 * @author wengn * */ public class CurrentConditionsDisplay implements Observer, DisplayElement { private float temperature; private float humidity; private Subject weatherData; public CurrentConditionsDisplay(Subject s){ this.weatherData = s; weatherData.registerObserver(this); } public void update(float temp, float humidity, float pressure) { this.temperature = temp; this.humidity = humidity; display(); } public void display() { System.out.println("show:"+temperature+"||"+humidity); } } /** * 測試類 * @author wengn * */ public class Test { public static void main(String[] args) { WeatherData wd = new WeatherData(); CurrentConditionsDisplay ccd = new CurrentConditionsDisplay(wd); wd.setMeasurements(0, 0, 0); wd.setMeasurements(1, 1, 1); wd.setMeasurements(2, 2, 2); } }
?
? 附上java awt中觀察者模式的模擬代碼:
?
?
package com.hanbing.awt; import java.util.ArrayList; import java.util.List; public class MyAwt { public static void main(String[] args) { Button bt = new Button(); MyActionLisenter lisenter = new MyActionLisenter(); MyActionLisenter2 lisenter2 = new MyActionLisenter2(); bt.addActionLisenter(lisenter); bt.addActionLisenter(lisenter2); bt.buttonPressed(); } } class Button { private List<ActionLisenter> list = new ArrayList<ActionLisenter>(); public void buttonPressed() { ActionEvent e = new ActionEvent(System.currentTimeMillis(), this); for (ActionLisenter lisenter:list) { lisenter.actionPerformed(e); } } public void addActionLisenter(ActionLisenter lisenter) { list.add(lisenter); } } interface ActionLisenter { public void actionPerformed(ActionEvent e); } class MyActionLisenter implements ActionLisenter { public void actionPerformed(ActionEvent e) { System.out.println("oh my god!!!"); } } class MyActionLisenter2 implements ActionLisenter { public void actionPerformed(ActionEvent e) { System.out.println("oh my god2!!!"); } } class ActionEvent { private long when; private Object source; public ActionEvent(long when, Object source) { super(); this.when = when; this.source = source; } public long getWhen() { return when; } public Object getSource(){ return source; } }
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
