觀察者(observer)模式定義了一對多的依賴關係,讓多個觀察者對象能夠同時監聽某一主題對象。這個主題對象中的狀態發生改變時,就會通知所有的觀察者對象。 觀察者模式的結構圖: 結構中各個部分的含義: 源代碼: 抽象主題類(Subject): 具體主題類(ConcreteSubject):
觀察者(observer)模式定義了一對多的依賴關係,讓多個觀察者對象能夠同時監聽某一主題對象。這個主題對象中的狀態發生改變時,就會通知所有的觀察者對象。
觀察者模式的結構圖:
結構中各個部分的含義:
- 抽象主題類(Subject):它把所有對觀察者對象的引用都保存在一個聚集內,每個主題可以有任意多的觀察者。
- 具體主題類(ConcreteSubject):具體主題,將有關狀態存入具體觀察者對象;當具體主題狀態改變時,向所有觀察者發出通知。
- 抽象觀察者類(Observer):抽象觀察者,為所有的具體觀察者定義一個介面。
- 具體觀察者類(ConcreteObserver):具體觀察者,實現抽象觀察者角色所要求的介面,以便更新本身的狀態
源代碼:
抽象主題類(Subject):
public interface Subject { public void registerObserver(Observer o); //增加觀察者 public void removeObserver(Observer o); //刪除觀察者 public void notifyObserver(String newState);//通知觀察者 }
具體主題類(ConcreteSubject):
public class ConcreteSubject implements Subject{ private ArrayList<Observer> observers; //觀察者集合 public ConcreteSubject() { observers = new ArrayList<Observer>(); } @Override public void registerObserver(Observer o) { observers.add(o); } @Override public void removeObserver(Observer o) { observers.remove(o); } @Override public void notifyObserver(String newstate) { for (Observer observer : observers) { observer.update(newstate); } } public void getChange(String newState){ notifyObserver(newState); } }
抽象觀察者類(Observer):
public interface Observer { public void update(String state); }
抽象觀察者類(ConcreteObserver):
public class ConcreteObserver implements Observer{ @Override public void update(String state) { System.out.println("更新後狀態為:"+ state); } }
客戶端:
public class Client { public static void main(String[] args) { ConcreteSubject s = new ConcreteSubject(); Observer observer = new ConcreteObserver(); s.registerObserver(observer); s.getChange("新狀態"); } }
運行結果:
更新後狀態為:新狀態
具體情境舉例:氣象站的溫度變化更新:
抽象主題類(Subject):
public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObservers(); }
具體主題類:
public class CurrentConditionDisplay implements Observer,DisplayElement{ private float temperature; private float humidity; private Subject weatherData; public CurrentConditionDisplay(Subject weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); } @Override public void display() { System.out.println("當前氣溫:"+ temperature + "F 濕度為:"+ humidity + "%"); } @Override public void update(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; display(); } }
displayElement介面:
public interface DisplayElement { public void display(); }
抽象觀察者類(Observer):
public interface Observer { public void update(float temperature, float humidity, float pressure); }
具體觀察者類(ConcreteObserver):
public class WeatherData implements Subject{ private ArrayList<Observer> observers; //觀察者 //要更新的觀察者的信息 private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList<Observer>(); } @Override public void registerObserver(Observer o) { observers.add(o); } @Override public void removeObserver(Observer o) { int i = observers.indexOf(o); if (i >= 0 ) { observers.remove(i); } } @Override public void notifyObservers() { // for(int i = 0; i < observers.size(); i++){ // Observer observer = (Observer) observers.get(i); // observer.update(temperature, humidity, pressure); // } for (Observer o : observers) { o.update(temperature, humidity, pressure); } } public void measurementsChanged(){ notifyObservers(); } public void setMeasurement(float temperature, float humidity, float pressure){ this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } }
客戶端:
public class WeatherStation { public static void main(String[] args){ WeatherData weatherData = new WeatherData(); CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData); weatherData.setMeasurement(80, 65, 30.4f); weatherData.setMeasurement(82, 70, 29.2f); weatherData.setMeasurement(78, 90, 29.2f); } }
運行結果:
當前氣溫:80.0F 濕度為:65.0% 當前氣溫:82.0F 濕度為:70.0% 當前氣溫:78.0F 濕度為:90.0%