观察者模式

来源:互联网 发布:oracle .sql如们运行 编辑:程序博客网 时间:2024/06/05 18:33

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


一、问题描述


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

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


二、类图




三、实现代码


1.抽象主题Subject

[java] view plain copy
  1. public interface Subject {  
  2.     public void registerObserver(Observer o);  
  3.     public void removeObserver(Observer o);  
  4.     public void notifyObservers();  
  5. }  

2.抽象观察者Obsever

[java] view plain copy
  1. public interface Observer {  
  2.     public void update(float temp, float humidity, float pressure);  
  3. }  
可以看出,使用的是推数据的方式。


3.具体主题WeatherData

[java] view plain copy
  1. public class WeatherData implements Subject {  
  2.     private ArrayList observers;  
  3.     //temperature、humidity、pressure就是WeatherData的“状态”  
  4.     private float temperature;  
  5.     private float humidity;  
  6.     private float pressure;  
  7.       
  8.     public WeatherData() {  
  9.         observers = new ArrayList();  
  10.     }  
  11.       
  12.     public void registerObserver(Observer o) {  
  13.         observers.add(o);  
  14.     }  
  15.       
  16.     public void removeObserver(Observer o) {  
  17.         int i = observers.indexOf(o);  
  18.         if (i >= 0) {  
  19.             observers.remove(i);  
  20.         }  
  21.     }  
  22.       
  23.     public void notifyObservers() {  
  24.         for (int i = 0; i < observers.size(); i++) {  
  25.             Observer observer = (Observer)observers.get(i);  
  26.             observer.update(temperature, humidity, pressure);  
  27.         }  
  28.     }  
  29.       
  30.     public void measurementsChanged() {  
  31.         notifyObservers();  
  32.     }  
  33.       
  34.     public void setMeasurements(float temperature, float humidity, float pressure) {  
  35.         this.temperature = temperature;  
  36.         this.humidity = humidity;  
  37.         this.pressure = pressure;  
  38.         measurementsChanged();  
  39.     }  
  40.       
  41.     public float getTemperature() {  
  42.         return temperature;  
  43.     }  
  44.       
  45.     public float getHumidity() {  
  46.         return humidity;  
  47.     }  
  48.       
  49.     public float getPressure() {  
  50.         return pressure;  
  51.     }  
  52. }  
WeatherData中的temperature、humidity、pressure这三个属性就是WeatherData的“状态”(即我们在以前提到的State状态)


4.本系统的辅助接口DisplayElement

[java] view plain copy
  1. public interface DisplayElement {  
  2.     public void display();  
  3. }  

5.具体观察者


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

[java] view plain copy
  1. public class StatisticsDisplay implements Observer, DisplayElement {  
  2.     private float maxTemp = 0.0f;  
  3.     private float minTemp = 200;  
  4.     private float tempSum= 0.0f;  
  5.     private int numReadings;//记录观测的次数以便计算平均温度值  
  6.     private Subject weatherData;  
  7.   
  8.     public StatisticsDisplay(Subject weatherData) {  
  9.         this.weatherData = weatherData;  
  10.         weatherData.registerObserver(this);  
  11.     }  
  12.     //这属于“推”数据,本类只使用到了第一个参数  
  13.     public void update(float temp, float humidity, float pressure) {  
  14.         tempSum += temp;  
  15.         numReadings++;  
  16.   
  17.         if (temp > maxTemp) {  
  18.             maxTemp = temp;  
  19.         }  
  20.    
  21.         if (temp < minTemp) {  
  22.             minTemp = temp;  
  23.         }  
  24.   
  25.         display();  
  26.     }  
  27.   
  28.     public void display() {  
  29.         System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)  
  30.             + "/" + maxTemp + "/" + minTemp);  
  31.     }  
  32. }  

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

[java] view plain copy
  1. public class CurrentConditionsDisplay implements Observer, DisplayElement {  
  2.     private float temperature;//温度  
  3.     private float humidity;//湿度  
  4.     private Subject weatherData;  
  5.       
  6.     public CurrentConditionsDisplay(Subject weatherData) {  
  7.         this.weatherData = weatherData;  
  8.         weatherData.registerObserver(this);  
  9.     }  
  10.     //推数据,这里只使用到了前两个参数  
  11.     public void update(float temperature, float humidity, float pressure) {  
  12.         this.temperature = temperature;  
  13.         this.humidity = humidity;  
  14.         display();  
  15.     }  
  16.       
  17.     public void display() {  
  18.         System.out.println("Current conditions: " + temperature   
  19.             + "F degrees and " + humidity + "% humidity");  
  20.     }  
  21. }  

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

[java] view plain copy
  1. public class ForecastDisplay implements Observer, DisplayElement {  
  2.     private float currentPressure = 29.92f;    
  3.     private float lastPressure;  
  4.     private Subject weatherData;  
  5.   
  6.     public ForecastDisplay(Subject weatherData) {  
  7.         this.weatherData = weatherData;  
  8.         weatherData.registerObserver(this);  
  9.     }  
  10.   
  11.     public void update(float temp, float humidity, float pressure) {  
  12.         lastPressure = currentPressure;  
  13.         currentPressure = pressure;  
  14.         display();  
  15.     }  
  16.   
  17.     public void display() {  
  18.         System.out.print("Forecast: ");  
  19.         if (currentPressure > lastPressure) {  
  20.             System.out.println("Improving weather on the way!");  
  21.         } else if (currentPressure == lastPressure) {  
  22.             System.out.println("More of the same");  
  23.         } else if (currentPressure < lastPressure) {  
  24.             System.out.println("Watch out for cooler, rainy weather");  
  25.         }  
  26.     }  
  27. }  

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

[java] view plain copy
  1. public class HeatIndexDisplay implements Observer, DisplayElement {  
  2.     float heatIndex = 0.0f;  
  3.     private Subject weatherData;  
  4.   
  5.     public HeatIndexDisplay(Subject weatherData) {  
  6.         this.weatherData = weatherData;  
  7.         weatherData.registerObserver(this);  
  8.     }  
  9.   
  10.     public void update(float t, float rh, float pressure) {  
  11.         heatIndex = computeHeatIndex(t, rh);  
  12.         display();  
  13.     }  
  14.       
  15.     private float computeHeatIndex(float t, float rh) {  
  16.         float index = (float)((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh)   
  17.             + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh))   
  18.             + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +  
  19.             (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 *   
  20.             (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +   
  21.             (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +  
  22.             0.000000000843296 * (t * t * rh * rh * rh)) -  
  23.             (0.0000000000481975 * (t * t * t * rh * rh * rh)));  
  24.         return index;  
  25.     }  
  26.   
  27.     public void display() {  
  28.         System.out.println("Heat index is " + heatIndex);  
  29.     }  
  30. }  

6.测试

[java] view plain copy
  1. public class WeatherStationHeatIndex {  
  2.   
  3.     public static void main(String[] args) {  
  4.         WeatherData weatherData = new WeatherData();  
  5.         CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);  
  6.         StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);  
  7.         ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);  
  8.         HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData);  
  9.   
  10.         weatherData.setMeasurements(806530.4f);  
  11.         weatherData.setMeasurements(827029.2f);  
  12.         weatherData.setMeasurements(789029.2f);  
  13.     }  
  14. }  

四、说明


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


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

原创粉丝点击