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

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          相關(guān)鏈接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

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

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



           

          二、Observer模式

          1、定義觀察者模式

          觀察者模式定義了對象之間的一對多依賴,這樣一來,當一個對象改變狀態(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 扭曲的鉛筆 閱讀(264) 評論(0)  編輯  收藏 所屬分類: DesignPattern

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 郁南县| 兰西县| 万源市| 沂水县| 晋宁县| 黄骅市| 旺苍县| 宜章县| 那曲县| 寿阳县| 南陵县| 济阳县| 大悟县| 金溪县| 张家港市| 阿克陶县| 盈江县| 禄丰县| 泸西县| 商都县| 夏津县| 衡水市| 阿图什市| 韶山市| 吉林市| 横山县| 电白县| 奇台县| 渭源县| 舟山市| 安龙县| 三门县| 灌阳县| 玉树县| 丘北县| 临漳县| 顺昌县| 定南县| 永定县| 双鸭山市| 鄂伦春自治旗|