隨筆 - 37  文章 - 14  trackbacks - 0
          <2008年3月>
          2425262728291
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          相關(guān)鏈接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          一、要完成的任務(wù)

          此系統(tǒng)中的三個部分是氣象站(獲取實際氣象數(shù)據(jù)的物理裝置)、WeatherData對象(追蹤來自氣象站的數(shù)據(jù),并更新布告板)和布告板(顯示目前天氣狀況給用戶看)。



           

          二、Observer模式

          1、定義觀察者模式

          觀察者模式定義了對象之間的一對多依賴,這樣一來,當(dāng)一個對象改變狀態(tài)時,它的所有依賴者都會收到通知并自動更新。



           

          2.設(shè)計氣象站




           

          三、代碼實現(xiàn)

          1.定義接口

          1Subject接口

          Subject.java

          package com.sterning.ch2_observer;

          public interface Subject {
              
          public void registerObserver(Observer o);
              
          public void removeObserver(Observer o);
              
          public void notifyObservers();
          }


           

          2Observer接口

          Observer.java

          package com.sterning.ch2_observer;

          public interface Observer {
              
          public void update(float temp,float humidity,float pressure);
          }

           

          3Displayment接口

          Displayment.java

          package com.sterning.ch2_observer;

          public interface Displayment {
              
          public void display();
          }

          2.實現(xiàn)接口

          1WeatherData

          WeatherData.java

          package com.sterning.ch2_observer;

          import java.util.ArrayList;

          public class WeatherData implements Subject {
              
          private ArrayList observers;
              
          private float temperature;
              
          private float humidity;
              
          private float pressure;
              
              
          public WeatherData(){
                  observers
          =new ArrayList();
              }

              
              
          public void notifyObservers() {
                  
          for(int i=0;i<observers.size();i++){
                      Observer observer
          =(Observer)observers.get(i);
                      observer.update(temperature, humidity, pressure);
                  }

              }


              
          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 measurementsChanged(){
                  notifyObservers();
              }

              
          public void setMeasurements(float temperature,float humidity,float pressure){
                  
          this.temperature=temperature;
                  
          this.humidity=humidity;
                  
          this.pressure=pressure;
                  measurementsChanged();
              }

          }


           

          2CurrentConditionsDisplay

          CurrentConditionsDisplay.java

          package com.sterning.ch2_observer;

          public class CurrentConditionsDisplay implements Observer, Displayment {
              
          private float temperature;
              
          private float humidity;
              
          private Subject weatherData;
              
              
          public CurrentConditionsDisplay(Subject weatherData){
                  
          this.weatherData=weatherData;
                  weatherData.registerObserver(
          this);
              }

              
              
          public void update(float temp, float humidity, float pressure) {
                  
          this.temperature=temp;
                  
          this.humidity=humidity;
                  display();
              }


              
          public void display() {
                  System.out.println(
          "Current conditions:"+temperature+"F degrees and "+humidity+"% humidity");
              }


          }


           

          3StatisticsDisplay

          StatisticsDisplay.java

          package com.sterning.ch2_observer;

          import java.util.*;

          public class StatisticsDisplay implements Observer, Displayment {
              
          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);
              }

          }


           

          4ForecastDisplay

          ForecastDisplay.java

          package com.sterning.ch2_observer;

          import java.util.*;

          public class ForecastDisplay implements Observer, Displayment {
              
          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");
                  }

              }

          }


           

          3.實現(xiàn)氣象站

          WeatherStation.java

          package com.sterning.ch2_observer;

          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(
          806530.4f);
                  weatherData.setMeasurements(
          827029.2f);
                  weatherData.setMeasurements(
          789029.2f);        
                  
              }

          }

          posted on 2008-03-11 09:35 扭曲的鉛筆 閱讀(260) 評論(0)  編輯  收藏 所屬分類: DesignPattern

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 邢台县| 黄山市| 桃园县| 前郭尔| 东港市| 夏河县| 广平县| 高邑县| 榆树市| 加查县| 邛崃市| 开封市| 雷州市| 林州市| 洛阳市| 黄大仙区| 石家庄市| 合阳县| 铁岭县| 亚东县| 和静县| 合江县| 房山区| 吴川市| 南通市| 榆社县| 庆元县| 广饶县| 射洪县| 霍州市| 望江县| 称多县| 德兴市| 盐山县| 通山县| 屏山县| 松潘县| 巴南区| 教育| 东乡族自治县| 辽阳市|