package observer;
import java.util.ArrayList;
/**
* @author jiq
* 型別:Behavioral
* 定義: 觀察者模式定義了物件之間一對多的依賴,這樣一來,當一個物件狀態改變時,
* 它所有的依賴者都會受到通知並自動更新。
*
* 這個模式很好滴遵循了OO設計的三個原則:
* (1) 將變化的部分和不變的部分分離
* (2) 針對介面程式設計,不針對實現程式設計
* (3) 多用組合,少用繼承
*
* java中用到的觀察者模式:
* (1) Java Swing中為JButton註冊一個ActionListener時間監聽器。
* (2) Java中有一個Observable類提供了觀察者模式程式設計API,但是可擴充性不是很好。
*/
/* 主題 */
interface Subject{
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
/* 觀察者 */
interface Observer{
public void update(WeatherAttrib wb);
}
/* 顯示皮膚 */
interface Disply{
public void disply();
}
/******
* 為了方便天氣資料擴充,所以將其作為一個資料類定義出來
* */
class WeatherAttrib{
float temperature;
float humidity;
float pressure;
public WeatherAttrib(float temperature, float humidity, float pressure){
this.temperature = temperature;
this.humidity = humidity;
this.temperature = pressure;
}
}
/****
* 天氣資料類,繼承自主題(被觀察者)
* */
class WeatherData implements Subject{
private ArrayList<Observer> observers = new ArrayList<Observer>();
private WeatherAttrib wb;
public void registerObserver(Observer o) {
observers.add(o);
}
public void notifyObservers() {
for(int i=0;i<observers.size();i++){
Observer observer = observers.get(i);
observer.update(wb);
}
}
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if(i >= 0) observers.remove(i);
}
/* 主題狀態改變 */
public void measurementsChanged(){
notifyObservers();
}
public void setWeatherAttrib(WeatherAttrib wb){
this.wb = wb;
measurementsChanged();
}
}
/****
* 觀察者(繼承自觀察者介面)
* */
class CurrentConditionDisplay implements Observer, Disply{
private WeatherAttrib wb;
private Subject weatherData; //組合主題
public CurrentConditionDisplay(Subject weatherData){
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(WeatherAttrib wb) {
this.wb = wb;
disply();
}
public void cancelObserve(){
weatherData.removeObserver(this);
}
public void disply() {
System.out.println("Current Condition: "+ wb.humidity+","
+wb.temperature+""+wb.pressure);
}
}
class ForecastDisplay implements Observer, Disply{
private WeatherAttrib wb;
private Subject weatherData;
public ForecastDisplay(Subject weatherData){
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(WeatherAttrib wb) {
this.wb = wb;
disply(); //更新顯示
}
public void cancelObserve(){
weatherData.removeObserver(this);
}
public void disply() {
System.out.println("ForeCatst Disp: "+ wb.humidity+","+wb.temperature+""+wb.pressure);
}
}
/**
* 氣象站測試
* */
public class WeatherORama {
public static void main(String[] args) {
/*
* 建立主題
* */
WeatherData weatherData = new WeatherData();
/*
* 建立觀察者
* */
CurrentConditionDisplay cd_observer = new CurrentConditionDisplay(weatherData);
ForecastDisplay fd_observer = new ForecastDisplay(weatherData);
/*
* 更改主題狀態
* */
System.out.println("###WeatherData change:");
weatherData.setWeatherAttrib(new WeatherAttrib(80, 65, 30.4f));
System.out.println("###WeatherData change:");
weatherData.setWeatherAttrib(new WeatherAttrib(82, 67, 29.4f));
fd_observer.cancelObserve(); //不再關注主題
System.out.println("###WeatherData change:");
weatherData.setWeatherAttrib(new WeatherAttrib(85, 63, 30.1f));
}
}/** output
###WeatherData change:
Current Condition: 65.0,30.40.0
ForeCatst Disp: 65.0,30.40.0
###WeatherData change:
Current Condition: 67.0,29.40.0
ForeCatst Disp: 67.0,29.40.0
###WeatherData change:
Current Condition: 63.0,30.10.0
*/