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

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          相關(guān)鏈接

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

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

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



           

          二、Observer模式

          1、定義觀察者模式

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



           

          2.設(shè)計(jì)氣象站




           

          三、代碼實(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.實(shí)現(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.實(shí)現(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) 評(píng)論(0)  編輯  收藏 所屬分類: DesignPattern

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 南部县| 洞头县| 聂荣县| 交城县| 定日县| 长子县| 宣城市| 林芝县| 且末县| 百色市| 河西区| 嫩江县| 江都市| 古交市| 綦江县| 万安县| 黄浦区| 彭州市| 杨浦区| 剑河县| 伊春市| 和静县| 绥化市| 湘潭县| 广宗县| 四子王旗| 毕节市| 和顺县| 塔河县| 罗山县| 广安市| 曲阳县| 丰城市| 丹江口市| 陕西省| 蒙山县| 东源县| 无为县| 微山县| 静海县| 屏东市|