Mark's Java Blog
          MyEclipse, Java EE, Spring, Struts, Hibernate, JPA, SWT, Swing, AJAX, JavaScript,SOA


                  觀察者模式:在對象之間定義一對多的依賴,當一個對象改變狀態,依賴他的對象都會收到通知,并自動更新。(以松耦合方式在一系列對象之間溝通狀態,代表人物--MVC)

                  注意事項:主題(可觀察者)用一個共同的接口來更新觀察者,主題不知道觀察者的細節,只知道觀察者實現了觀察者接口。使用此模式時,你可從被觀察者處推(push)或拉(pull)數據(推的方式被認為更正確)。有多個觀察者時,不可以依賴特定的通知順序。java有多種觀察者模式的實現,包括java.util.Observable(有一些違背設計原則的問題,有必要的話可以實現自己的Observable),JavaBeans,RMI等,Swing大量使用此模式,許多GUI框架也是如此。

          用觀察者模式實現一個氣象站的實例:
          要求當WeatherData對象(接受物理氣象站數據的對象)更新數據時,隨即會更新三個布告板的顯示:目前狀況(溫度、濕度、氣壓)、氣象統計和天氣預報。并且公布API可以讓其他人寫出自己的布告板。

          代碼:
          //主題接口,提供注冊、刪除、更新布告板的方法
          public interface Subject {
           public void registerObserver(Observer o);
           public void removeObserver(Observer o);
           public void notifyObservers();
          }

          //觀察者接口,提供更新本布告板的方法
          public interface Observer {
           public void update(float temp, float humidity, float pressure);
          }

          //布告板顯示接口,提供布告板顯示的方法
          public interface DisplayElement {
           public void display();
          }

          //主題實現類
          public class WeatherData implements Subject {
           private ArrayList observers;
           private float temperature;
           private float humidity;
           private float pressure;
           //構造方法中初始化觀察者記錄
           public WeatherData() {
            observers = new ArrayList();
           }
           //注冊觀察者
           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 notifyObservers() {
            for (int i = 0; i < observers.size(); i++) {
             Observer observer = (Observer)observers.get(i);
             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();
           }
           
           // other WeatherData methods here
           
           public float getTemperature() {
            return temperature;
           }
           
           public float getHumidity() {
            return humidity;
           }
           
           public float getPressure() {
            return pressure;
           }
          }

          //觀察者實現類
          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);
           }
           //修改觀察者狀態
           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 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");
            }
           }
          }


          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 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);
            weatherData.setMeasurements(82, 70, 29.2f);
            weatherData.setMeasurements(78, 90, 29.2f);
           }
          }

          當我們想添加一個布告板的時候只要實現觀察者借口就可以加入主題通知的記錄中,實際代碼如下:
          //我們要添加一個酷熱指數的布告板,利用一套公式來計算酷熱指數。

          public class HeatIndexDisplay implements Observer, DisplayElement {
           float heatIndex = 0.0f;
           private WeatherData weatherData;

           public HeatIndexDisplay(WeatherData weatherData) {
            this.weatherData = weatherData;
            weatherData.registerObserver(this);
           }

           public void update(float t, float rh, float pressure) {
            heatIndex = computeHeatIndex(t, rh);
            display();
           }
           //酷熱指數計算
           private float computeHeatIndex(float t, float rh) {
            float index = (float)((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh)
             + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh))
             + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +
             (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 *
             (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +
             (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +
             0.000000000843296 * (t * t * rh * rh * rh)) -
             (0.0000000000481975 * (t * t * t * rh * rh * rh)));
            return index;
           }

           public void display() {
            System.out.println("Heat index is " + heatIndex);
           }
          }

          //加入酷熱指數布告板后的測試類

          public class WeatherStationHeatIndex {

           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);
            HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData);

            weatherData.setMeasurements(80, 65, 30.4f);
            weatherData.setMeasurements(82, 70, 29.2f);
            weatherData.setMeasurements(78, 90, 29.2f);
           }
          }

          posted on 2008-03-18 13:52 Mark 閱讀(514) 評論(0)  編輯  收藏 所屬分類: 設計模式
           
          主站蜘蛛池模板: 托克逊县| 新疆| 任丘市| 定远县| 奉化市| 胶南市| 常州市| 织金县| 芦溪县| 思南县| 司法| 土默特右旗| 宾川县| 巴马| 新和县| 米泉市| 娄烦县| 视频| 伊宁市| 越西县| 周口市| 休宁县| 武威市| 潜江市| 朔州市| 浦县| 霞浦县| 抚顺市| 台前县| 保德县| 平原县| 峨边| 闻喜县| 湘潭市| 贵阳市| 通海县| 疏附县| 穆棱市| 新昌县| 淮南市| 利辛县|