<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


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

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

    用觀察者模式實現(xiàn)一個氣象站的實例:
    要求當(dāng)WeatherData對象(接受物理氣象站數(shù)據(jù)的對象)更新數(shù)據(jù)時,隨即會更新三個布告板的顯示:目前狀況(溫度、濕度、氣壓)、氣象統(tǒng)計和天氣預(yù)報。并且公布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();
    }

    //主題實現(xiàn)類
    public class WeatherData implements Subject {
     private ArrayList observers;
     private float temperature;
     private float humidity;
     private float pressure;
     //構(gòu)造方法中初始化觀察者記錄
     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);
      }
     }
     //當(dāng)主題狀態(tài)改變時調(diào)用,以通知所有觀察者
     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();
     }
     //更新數(shù)據(jù)方法
     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;
     }
    }

    //觀察者實現(xiàn)類
    public class CurrentConditionsDisplay implements Observer, DisplayElement {
     private float temperature;
     private float humidity;
     private Subject weatherData;
     //構(gòu)造方法中把此觀察者注冊到主題
     public CurrentConditionsDisplay(Subject weatherData) {
      this.weatherData = weatherData;
      weatherData.registerObserver(this);
     }
     //修改觀察者狀態(tài)
     public void update(float temperature, float humidity, float pressure) {
      this.temperature = temperature;
      this.humidity = humidity;
      display();
     }
     //顯示布告板更新內(nèi)容
     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);
     }
    }

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

    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();
     }
     //酷熱指數(shù)計算
     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);
     }
    }

    //加入酷熱指數(shù)布告板后的測試類

    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 閱讀(513) 評論(0)  編輯  收藏 所屬分類: 設(shè)計模式
     
    主站蜘蛛池模板: 国产av无码专区亚洲av桃花庵| 国产一级一毛免费黄片| 在线看片无码永久免费aⅴ| 日韩电影免费在线观看网址 | 国产.亚洲.欧洲在线| 国产免费131美女视频| 永久免费AV无码网站国产| 亚洲sss综合天堂久久久| 日产国产精品亚洲系列| 亚洲6080yy久久无码产自国产 | 亚洲精品乱码久久久久久蜜桃图片| 亚洲高清偷拍一区二区三区| 免费精品无码AV片在线观看| 日本亚洲欧美色视频在线播放 | 亚洲无码在线播放 | 亚洲黄色在线观看网站| 亚洲国产精品不卡毛片a在线| 久视频精品免费观看99| 亚洲综合激情视频| 亚洲av无码成人精品区在线播放 | 国产精品免费久久久久久久久| 亚洲伊人久久大香线蕉在观| 国产91精品一区二区麻豆亚洲| 成人网站免费观看| 无码专区AAAAAA免费视频| 亚洲国产中文在线视频| 亚洲最大激情中文字幕| 永久免费毛片手机版在线看| 18禁男女爽爽爽午夜网站免费| 色老头综合免费视频| 亚洲Av永久无码精品黑人| 亚洲综合在线成人一区| 亚洲国产精品无码成人片久久 | 亚洲真人无码永久在线观看| 亚洲一区影音先锋色资源| 中文字幕日韩亚洲| 国产国产成年年人免费看片| 一色屋成人免费精品网站| 亚洲一区二区在线免费观看| 国产免费内射又粗又爽密桃视频 | 最近免费中文字幕mv在线电影|