System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings) + "/" + maxTemp + "/" + minTemp);
}
@Override
public void update(float temp, float humidity, float pressure) {
tempSum += temp;
numReadings++;
if(temp > maxTemp) {
maxTemp = temp;
}
if(temp < minTemp) {
minTemp = temp;
}
display();
}
}
[java]
package observerPattern;
/**
* 天气预报布告板
* @author wwj
*
*/
public class ForecastDisplay implements Observer, DisplayElement {
private float currentPressure = 29.92f; //当前气压
private float lastPressure; //以往气压
private WeatherData weatherData;
public ForecastDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void display() {
System.out.println("Forcast:");
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");
}
}
@Override
public void update(float temp, float humidity, float pressure) {
lastPressure = currentPressure;
currentPressure = pressure;
display();
}
}
package observerPattern;
/**
* 天气预报布告板
* @author wwj
*
*/
public class ForecastDisplay implements Observer, DisplayElement {
private float currentPressure = 29.92f; //当前气压
private float lastPressure; //以往气压
private WeatherData weatherData;
public ForecastDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void display() {
System.out.println("Forcast:");
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");
}
}
@Override
public void update(float temp, float humidity, float pressure) {
lastPressure = currentPressure;
currentPressure = pressure;
display();
}
}
[java]
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)) -