设计模式学习--观察者模式(Observer Pattern) (五)

2014-11-24 11:07:15 · 作者: · 浏览: 5
(0.0000000000481975 * (t * t * t * rh * rh * rh)));
return index;
}

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

package observerPattern;

/**
* 酷热指数布告板
*
* @author wwj
* 注:那个计算酷热指数的公式不必深究
*/
public class HeatIndexDisplay implements Observer, DisplayElement {
float heatIndex = 0.0f;
private WeatherData weatherData;

public HeatIndexDisplay(WeatherData 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]
package observerPattern;

/**
* 测试类
* @author wwj
*
*/
public class WeatherStation {
public static void main(String[] args) {
//建立一个WeatherData对象
WeatherData weatherData = new WeatherData();

//第一个布告板
CurrentConditionDisplay currentDisplay = new CurrentConditionDisplay(
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);
}
}

package observerPattern;

/**
* 测试类
* @author wwj
*
*/
public class WeatherStation {
public static void main(String[] args) {
//建立一个WeatherData对象
WeatherData weatherData = new WeatherData();

//第一个布告板
CurrentConditionDisplay currentDisplay = new CurrentConditionDisplay(
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);
}
}

7. 测试结果:
[plain]
Current coditions: 80.0F degress and 65.0% humidity
Avg/Max/Min temperature = 80.0/80.0/80.0
Forcast:
Improving weather on the way!
Heat index is 82.95535
Current coditions: 82.0F degress and 70.0% humidity
Avg/Max/Min temperature = 81.0/82.0/80.0
Forcast:
Watch out for cooler, rainy weather
Heat index is 86.90124
Current coditions: 78.0F degress and 90.0% humidity
Avg/Max/Min temperature = 80.0/82.0/78.0
Forcast:
more of the same
Heat index is 83.64967

Current coditions: 80.0F degress and 65.0% humidity
Avg/Max/Min temperature = 80.0/80.0/80.0
Forcast:
Improving weather on the way!
He