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

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

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

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

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    文章分類

    相關鏈接

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    一、要完成的任務

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



     

    二、Observer模式

    1、定義觀察者模式

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



     

    2.設計氣象站




     

    三、代碼實現(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 扭曲的鉛筆 閱讀(259) 評論(0)  編輯  收藏 所屬分類: DesignPattern

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


    網站導航:
     
    主站蜘蛛池模板: 久草在视频免费福利| 亚洲AV第一页国产精品| 亚洲成AV人片在线播放无码| 亚洲精品无码人妻无码| 99视频精品全部免费观看| 亚洲国产综合精品一区在线播放| 亚洲成aⅴ人片在线观| 久久大香香蕉国产免费网站| 亚洲精品尤物yw在线影院| 99热这里只有精品6免费| 大桥未久亚洲无av码在线| 无码国产精品一区二区免费I6| 亚洲精品自在线拍| 五月亭亭免费高清在线| 婷婷精品国产亚洲AV麻豆不片 | 理论亚洲区美一区二区三区| 国产亚洲一区区二区在线| 一级女性全黄生活片免费看| 国产免费av片在线播放| 亚洲人av高清无码| 精品亚洲综合在线第一区| 免费看片免费播放| 亚洲熟女www一区二区三区| 色窝窝免费一区二区三区| 四虎国产精品免费永久在线| 亚洲最大AV网站在线观看| 中文字幕av免费专区| 国产成人亚洲精品青草天美| 最好免费观看韩国+日本| 亚洲一线产品二线产品| 亚洲第一视频网站| 亚洲人成色7777在线观看不卡| 成人免费一级毛片在线播放视频| 在线观看特色大片免费网站| 久久亚洲私人国产精品vA | 99在线精品免费视频九九视| 亚洲精品久久无码av片俺去也| 免费va在线观看| 久久成人无码国产免费播放| 日本一区二区在线免费观看| 亚洲精品美女久久777777|