觀察者模式(Observer Pattern) 詳解
本文地址: http://blog.csdn.net/caroline_wendy/article/details/26583157
版權所有, 禁止轉載, 如有轉載, 請站內聯繫.
觀察者模式(Observer Pattern): 定義了對象之間的一對多的依賴, 這樣一來, 當一個對象改變狀態時, 它的所有依賴者都會收到通知並自動更新.
使用方法:
1. 首先新建主題(subject)接口, 負責註冊(register)\刪除(remove)\通知(notify)觀察者; 觀察者(observer)接口, 負責更新(update)數據;
主題(subject)接口: 註冊觀察者(registerObserver), 刪除觀察者(removeObserver), 通知觀察者(notifyObservers, 通知所有觀察者);
觀察者(observer)接口: 更新(update);
代碼:
- /**
- * @time 2014年5月22日
- */
- package observer;
- /**
- * @author C.L.Wang
- *
- */
- public interface Subject {
- public void registerObserver(Observer o);
- public void removeObserver(Observer o);
- public void notifyObervers();
- }
- /**
- * @time 2014年5月22日
- */
- package observer;
- /**
- * @author C.L.Wang
- *
- */
- public interface Observer {
- public void update(float temp, float humidity, float pressure);
- }
- /**
- * @time 2014年5月22日
- */
- package observer;
- /**
- * @author C.L.Wang
- *
- */
- public interface DisplayElement {
- public void display();
- }
/**
* @time 2014年5月22日
*/
package observer;
/**
* @author C.L.Wang
*
*/
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObervers();
}
/**
* @time 2014年5月22日
*/
package observer;
/**
* @author C.L.Wang
*
*/
public interface Observer {
public void update(float temp, float humidity, float pressure);
}
/**
* @time 2014年5月22日
*/
package observer;
/**
* @author C.L.Wang
*
*/
public interface DisplayElement {
public void display();
}
2. 實現主題(subject)接口, 通過列表(list)的形式註冊(register)和刪除(remove)觀察者,
通知(notify)觀察者時, 循環調用所有已註冊的觀察者的更新(update)動作;
通過接口函數(set), 封裝通知(notify)動作, 傳入參數並進行通知.
代碼:
- /**
- * @time 2014年5月22日
- */
- package observer;
- import java.util.ArrayList;
- /**
- * @author C.L.Wang
- *
- */
- public class WeatherData implements Subject {
- public ArrayList<Observer> observers;
- private float temperature;
- private float humidity; //溼度
- private float pressure;
- public WeatherData() {
- observers = new ArrayList<Observer>();
- }
- /* (non-Javadoc)
- * @see observer.Subject#registerObserver(observer.Observer)
- */
- @Override
- public void registerObserver(Observer o) {
- // TODO Auto-generated method stub
- observers.add(o);
- }
- /* (non-Javadoc)
- * @see observer.Subject#removeObserver(observer.Observer)
- */
- @Override
- public void removeObserver(Observer o) {
- // TODO Auto-generated method stub
- int i = observers.indexOf(o);
- if (i>=0) {
- observers.remove(i);
- }
- }
- /* (non-Javadoc)
- * @see observer.Subject#notifyObervers()
- */
- @Override
- public void notifyObervers() {
- // TODO Auto-generated method stub
- for (int i=0; i<observers.size(); ++i) {
- Observer observer = (Observer)observers.get(i);
- observer.update(temperature, humidity, pressure);
- }
- }
- public void measurementsChanged() {
- notifyObervers();
- }
- public void setMeasurements(float temperature, float humidity, float pressure) {
- this.temperature = temperature;
- this.humidity = humidity;
- this.pressure = pressure;
- measurementsChanged();
- }
- }
/**
* @time 2014年5月22日
*/
package observer;
import java.util.ArrayList;
/**
* @author C.L.Wang
*
*/
public class WeatherData implements Subject {
public ArrayList<Observer> observers;
private float temperature;
private float humidity; //溼度
private float pressure;
public WeatherData() {
observers = new ArrayList<Observer>();
}
/* (non-Javadoc)
* @see observer.Subject#registerObserver(observer.Observer)
*/
@Override
public void registerObserver(Observer o) {
// TODO Auto-generated method stub
observers.add(o);
}
/* (non-Javadoc)
* @see observer.Subject#removeObserver(observer.Observer)
*/
@Override
public void removeObserver(Observer o) {
// TODO Auto-generated method stub
int i = observers.indexOf(o);
if (i>=0) {
observers.remove(i);
}
}
/* (non-Javadoc)
* @see observer.Subject#notifyObervers()
*/
@Override
public void notifyObervers() {
// TODO Auto-generated method stub
for (int i=0; i<observers.size(); ++i) {
Observer observer = (Observer)observers.get(i);
observer.update(temperature, humidity, pressure);
}
}
public void measurementsChanged() {
notifyObervers();
}
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}
3. 實現觀察者(observer)接口, 主要保存收到的數據, 並實現更新(update)動作, 即把數據保存在本地;
在構造函數中, 把自己註冊(register)入, 傳入的主題(subject)參數, 使主題可以通知觀察者.
代碼:
- /**
- * @time 2014年5月22日
- */
- package observer;
- /**
- * @author C.L.Wang
- *
- */
- public class CurrentConditionsDisplay implements Observer, DisplayElement {
- private float temperature;
- private float humidity;
- private Subject weatherData;
- public CurrentConditionsDisplay(Subject weatherData) {
- this.weatherData = weatherData;
- weatherData.registerObserver(this);
- }
- /* (non-Javadoc)
- * @see observer.DisplayElement#display()
- */
- @Override
- public void display() {
- // TODO Auto-generated method stub
- System.out.println("Current conditions: " + temperature +
- "F degrees and " + humidity + "% humidity");
- }
- /* (non-Javadoc)
- * @see observer.Observer#update(float, float, float)
- */
- @Override
- public void update(float temperature, float humidity, float pressure) {
- // TODO Auto-generated method stub
- this.temperature = temperature;
- this.humidity = humidity;
- display();
- }
- }
- package observer;
- import java.util.*;
- 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");
- }
- }
- }
- package observer;
- import java.util.*;
- 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);
- }
- }
/**
* @time 2014年5月22日
*/
package observer;
/**
* @author C.L.Wang
*
*/
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
/* (non-Javadoc)
* @see observer.DisplayElement#display()
*/
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println("Current conditions: " + temperature +
"F degrees and " + humidity + "% humidity");
}
/* (non-Javadoc)
* @see observer.Observer#update(float, float, float)
*/
@Override
public void update(float temperature, float humidity, float pressure) {
// TODO Auto-generated method stub
this.temperature = temperature;
this.humidity = humidity;
display();
}
}
package observer;
import java.util.*;
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");
}
}
}
package observer;
import java.util.*;
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);
}
}
4. 測試, 創建不同的觀察者(observer), 並把主題(subject)作爲參數傳入, 通知觀察者.
代碼:
- /**
- * @time 2014年5月22日
- */
- package observer;
- /**
- * @author C.L.Wang
- *
- */
- public class WeatherStation {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- WeatherData weatherData = new WeatherData();
- CurrentConditionsDisplay currentConditionsDisplay =
- new CurrentConditionsDisplay(weatherData); //new的時候進行註冊
- 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);
- }
- }
/**
* @time 2014年5月22日
*/
package observer;
/**
* @author C.L.Wang
*
*/
public class WeatherStation {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentConditionsDisplay =
new CurrentConditionsDisplay(weatherData); //new的時候進行註冊
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);
}
}
5. 輸出:
- 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