<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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)  編輯  收藏 所屬分類: 設計模式
     
    主站蜘蛛池模板: 麻豆va在线精品免费播放| 亚洲午夜电影在线观看| 麻豆亚洲AV成人无码久久精品| 久久亚洲中文字幕精品有坂深雪| 国产精品午夜免费观看网站 | 国产大片线上免费观看| 亚洲精品电影天堂网| 成人福利免费视频| 亚洲中文字幕无码爆乳app| 免费被黄网站在观看| 亚洲av无码专区国产不乱码| 免费一级一片一毛片| 久久久久久久久久免免费精品 | 最新国产AV无码专区亚洲| 91视频免费观看| 亚洲精品国产成人专区| 97人妻无码一区二区精品免费| 亚洲国产成人手机在线电影bd | 国产aⅴ无码专区亚洲av麻豆| 日本免费污片中国特一级| 久久亚洲AV成人无码| 最近2019中文字幕mv免费看| 美女被爆羞羞网站免费| 亚洲精品制服丝袜四区| 在线a免费观看最新网站| 亚洲国产无线乱码在线观看| 亚洲国产成人久久综合野外| 国产精品区免费视频| 亚洲中文字幕久久精品无码A | 男女免费观看在线爽爽爽视频| 亚洲精品无码久久久久久| 在线日韩日本国产亚洲| 1000部拍拍拍18勿入免费视频软件| 久久精品国产亚洲av瑜伽| 亚洲精品无码久久久影院相关影片| 4hu四虎最新免费地址| 手机永久免费的AV在线电影网| 67pao强力打造67194在线午夜亚洲 | 中文字幕永久免费| 四虎永久在线精品免费影视| 成人性生交大片免费看中文|