您的位置:首页 > 其它

观察者模式

2017-12-05 16:23 134 查看

转自:http://blog.csdn.net/jialinqiang/article/details/8878570


一、问题描述

Weather-O-Rama气象站计划建立下一代的Internet气象观察站,该气象站必须建立在WeatherData对象的基础上,WeatherData对象提供天气数据,有三种布告板,分别显示目前的状况、气象统计及简单的预报。并且以后可以方便地增加布告板进行扩展。

使用观察者模式进行设计,WeatherData对象即观察者模式中的主题对象,三个布告板即观察者。

二、类图



三、实现代码

1.抽象主题Subject

[java]
view plain
copy

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

2.抽象观察者Obsever

[java]
view plain
copy

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

可以看出,使用的是推数据的方式。

3.具体主题WeatherData

[java]
view plain
copy

public class WeatherData implements Subject {  
    private ArrayList observers;  
    //temperature、humidity、pressure就是WeatherData的“状态”  
    private float temperature;  
    private float humidity;  
    private float pressure;  
      
    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);  
        }  
    }  
      
    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();  
    }  
      
    public void setMeasurements(float temperature, float humidity, float pressure) {  
        this.temperature = temperature;  
        this.humidity = humidity;  
        this.pressure = pressure;  
        measurementsChanged();  
    }  
      
    public float getTemperature() {  
        return temperature;  
    }  
      
    public float getHumidity() {  
        return humidity;  
    }  
      
    public float getPressure() {  
        return pressure;  
    }  
}  

WeatherData中的temperature、humidity、pressure这三个属性就是WeatherData的“状态”(即我们在以前提到的State状态)

4.本系统的辅助接口DisplayElement

[java]
view plain
copy

public interface DisplayElement {  
    public void display();  
}  

5.具体观察者

(1)布告板StatisticsDisplay:显示最小、平均和最大的温度观测值

[java]
view plain
copy

public class StatisticsDisplay implements Observer, DisplayElement {  
    private float maxTemp = 0.0f;  
    private float minTemp = 200;  
    private float tempSum= 0.0f;  
    private int numReadings;//记录观测的次数以便计算平均温度值  
    private Subject weatherData;  
  
    public StatisticsDisplay(Subject 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);  
    }  
}  

(2)布告板CurrentConditionsDisplay:显示当前的温度、湿度

[java]
view plain
copy

public class CurrentConditionsDisplay implements Observer, DisplayElement {  
    private float temperature;//温度  
    private float humidity;//湿度  
    private Subject weatherData;  
      
    public CurrentConditionsDisplay(Subject weatherData) {  
        this.weatherData = weatherData;  
        weatherData.registerObserver(this);  
    }  
    //推数据,这里只使用到了前两个参数  
    public void update(float temperature, float humidity, float pressure) {  
        this.temperature = temperature;  
        this.humidity = humidity;  
        display();  
    }  
      
    public void display() {  
        System.out.println("Current conditions: " + temperature   
            + "F degrees and " + humidity + "% humidity");  
    }  
}  

(3)布告板ForecastDisplay:天气预报

[java]
view plain
copy

public class ForecastDisplay implements Observer, DisplayElement {  
    private float currentPressure = 29.92f;    
    private float lastPressure;  
    private Subject weatherData;  
  
    public ForecastDisplay(Subject 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");  
        }  
    }  
}  

(4)增加新的布告板,增加一个“酷热指数”布告板HeatIndexDisplay

[java]
view plain
copy

public class HeatIndexDisplay implements Observer, DisplayElement {  
    float heatIndex = 0.0f;  
    private Subject weatherData;  
  
    public HeatIndexDisplay(Subject weatherData) {  
        this.weatherData = weatherData;  
        weatherData.registerObserver(this);  
    }  
  
    public void update(float t, float rh, float pressure) {  
        heatIndex = computeHeatIndex(t, rh);  
        display();  
    }  
      
    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);  
    }  
}  

6.测试

[java]
view plain
copy

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);  
    }  
}  

四、说明

1.通过上面的update()方法,我们可以看出主题是通过“推”数据将数据传给观察者的。

2.通过以上代码可以看出,每个布告板(即具体观察者)中都有一个Subject类型的引用,用于指向其所注册的具体主题。在以上的需求中,我们可能会发现保存的这个Subject类型的引用没有使用到,因为直接通过update(float temp, float humidity, float pressure)方法就将WeatherData的状态数据传送给了观察者。然而这个引用并不是没有用处的,比如我们以后产生了新的需求,让观察者自行解除对主题的注册或使用主题的方法时,这时这个引用就派上了用场。有时候我们不得不将引用的类型由Subject改为ConcreteSubject(具体主题的类型),因为我们使用到的可能是具体主题所独有的方法。但是不管这个引用是Subject类型的还是ConcreteSubject(如WeatherData)类型的,这并不是观察者模式所关心的,《设计模式》中的这个引用的类型就是ConcreteSubject的,但是能使用Subject就尽量使用Subject而不要使用ConcreteSubject,比如本例就是将该引用的类型声明成Subject的,毕竟这样会使主题和观察者的耦合度更低(观察者可以注册到实现Subject接口的所有具体主题)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: