观察者模式(Observer Pattern)(三):使用java内置的支持实现HeadFirst气象站

一、问题描述


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

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


使用java内置的对观察者模式的支持实现该系统,java.util.Observable为抽象主题(可观察者),java.util.Observer接口为抽象观察者。


二、类图


观察者模式(Observer Pattern)(三):使用java内置的支持实现HeadFirst气象站_第1张图片



三、实现代码


1.具体主题WeatherData

public class WeatherData extends Observable {
	private float temperature;
	private float humidity;
	private float pressure;
	
	public WeatherData() { }
	
	public void measurementsChanged() {
		setChanged();
		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;
	}
}

2.辅助接口DisplayElement

public interface DisplayElement {
	public void display();
}

3.具体观察者:各种布告板


(1)布告板CurrentConditionsDisplay:显示当前温度和湿度

import java.util.Observable;
import java.util.Observer;
	
public class CurrentConditionsDisplay implements Observer, DisplayElement {
	Observable observable;//维护一个Observable类型的引用
	private float temperature;
	private float humidity;
	public CurrentConditionsDisplay(Observable observable) {
		this.observable = observable;
		observable.addObserver(this);
	}
	
	public void update(Observable obs, Object arg) {
		if (obs instanceof WeatherData) {
			WeatherData weatherData = (WeatherData)obs;
			//拉数据
			this.temperature = weatherData.getTemperature();
			this.humidity = weatherData.getHumidity();
			display();
		}
	}
	
	public void display() {
		System.out.println("Current conditions: " + temperature 
			+ "F degrees and " + humidity + "% humidity");
	}
}


(2)布告板StatisticsDisplay:显示温度的最大、最小及平均值。

public class StatisticsDisplay implements Observer, DisplayElement {
	Observable observable;//维护一个Observable类型的引用
	private float maxTemp = 0.0f;
	private float minTemp = 200;
	private float tempSum= 0.0f;
	private int numReadings;

	public StatisticsDisplay(Observable observable) {
		this.observable = observable;
		observable.addObserver(this);
	}

	public void update(Observable observable, Object arg) {
		if (observable instanceof WeatherData) {
			WeatherData weatherData = (WeatherData)observable;
			//拉数据
			float temp = weatherData.getTemperature();
			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);
	}
}

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

public class ForecastDisplay implements Observer, DisplayElement {
	Observable observable;//维护一个Observable类型的引用
	private float currentPressure = 29.92f;  
	private float lastPressure;

	public ForecastDisplay(Observable observable) {
		this.observable = observable;
		observable.addObserver(this);
	}

	public void update(Observable observable, Object arg) {
		if (observable instanceof WeatherData) {
			WeatherData weatherData = (WeatherData)observable;
			lastPressure = currentPressure;
			//拉数据
			currentPressure = weatherData.getPressure();
			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)增加“酷热指数”布告板

public class HeatIndexDisplay implements Observer, DisplayElement {
	Observable observable;//维护一个Observable类型的引用
	float heatIndex = 0.0f;

	public HeatIndexDisplay(Observable observable) {
		this.observable = observable;
		observable.addObserver(this);
	}

	public void update(Observable observable, Object arg) {
		if (observable instanceof WeatherData) {
			WeatherData weatherData = (WeatherData)observable;
			float t = weatherData.getTemperature();
			float rh = weatherData.getHumidity();
			heatIndex = (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)));
			display();
		}
	}

	public void display() {
		System.out.println("Heat index is " + heatIndex);
	}
}


4.测试

public class WeatherStationHeatIndex {

	public static void main(String[] args) {
		WeatherData weatherData = new WeatherData();
		CurrentConditionsDisplay currentConditions = 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.各个具体观察者(即布告板)实现了Observer接口的update(Observable observable, Object arg)方法,该方法支持推拉数据。

Observable类中的方法notifyObservers()就是用“拉”数据的方式进行主题和观察者数据的传递。

    public void notifyObservers() {
        notifyObservers(null);
    }

Observable类中的方法notifyObservers(Object arg)在参数arg不为null时就是“推”数据,arg就是要“推”的数据(状态)。


2.具体主题WeatherData中使用到了超类Observable中的方法setChanged(),其作用如下:

抽象超类Observable中有一个属性changed属性用来表示状态,初始值为false,只有changed为true时,主题才会通知各个观察者,代码如下:

    protected synchronized void setChanged() {
        changed = true;
    }

    public void notifyObservers(Object arg) {
        Object[] arrLocal;

        synchronized (this) {
            if (!changed)//changed为false时直接返回
                return;
            arrLocal = obs.toArray();//获得所有已注册的观察者
            clearChanged();//将changed属性设为false
        }

        for (int i = arrLocal.length-1; i>=0; i--)
            ((Observer)arrLocal[i]).update(this, arg);
    }

setChanged()方法可以让我们在更新观察者时具有更多的弹性,我们可以适当地通知观察者。比如,如果不使用setChanged()方法,我们的气象站测量是如此敏锐,以至于温度计读数每十分之一度就会更新,这造成WeatherData对象持续不断地通知观察者,我们并不希望看到这样的事情发生。我们希望半度以上才更新,就可以在温度差距达到半度时,调用setChanged()方法,进行有效地更新。


3.以上代码中,具体观察者(即布告板)中都持有一个Observable类型的引用,在上面的需求中该引用并没有用到,但是以后可能会用得到,比如以后可能让观察者自行取消对主题的注册,或者调用主题的其它方法时,这个引用就有用处了,所以我们要应付变化的需求


4.因为java是单继承的,这里的Observable是个抽象类,并且它的方法有些是protected的,会导致产生一些弊端。



转载请注明出处:http://blog.csdn.net/jialinqiang/article/details/8878576

你可能感兴趣的:(java,设计模式,observer)