观察者调用的顺序是无需的,且,从松耦合的角度考虑,也不应该之间有先后依托关系。
package cn.partner4java.weather2; import java.util.Observable; /** * 服务对象 * 我们不需要追踪观察者,也不需要管理注册。 * @author partner4java * */ public class WeatherData extends Observable { /** 温度 */ private float temperature; /** 湿度 */ private float humidity; /** 气压 */ private float pressure; public WeatherData(){ } public void measurementsChanged() { //来开启状态通知 setChanged(); //我们没有调用带参数,表明是拉 // If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed. // Each observer has its update method called with two arguments: this observable object and null. In other words, this method is equivalent to: // notifyObservers(null) notifyObservers(); } /** * 设置气象改变数据 * @param temperature 温度 * @param humidity 湿度 * @param pressure 气压 */ public void setMeasurements(float temperature,float humidity,float pressure){ this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } //下面的这些get方法,是方便与拉取数据 public float getTemperature() { return temperature; } public float getHumidity() { return humidity; } public float getPressure() { return pressure; } }
package cn.partner4java.weather2; import java.util.Observable; //实现的java API提供的 import java.util.Observer; import cn.partner4java.weather.DisplayElement; /** * 当前气象布告板 * @author partner4java * */ public class CurrentConditionsDisplay implements Observer, DisplayElement { /** 气温 */ private float temperature; /** 湿度 */ private float humidity; public CurrentConditionsDisplay(Observable observable){ observable.addObserver(this); } @Override public void update(Observable observable, Object arg) { if(observable instanceof WeatherData){ WeatherData weatherData = (WeatherData) observable; this.temperature = weatherData.getTemperature(); this.humidity = weatherData.getHumidity(); display(); } } @Override public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } }
package cn.partner4java.weather2; import java.util.Observable; import java.util.Observer; import cn.partner4java.weather.DisplayElement; /** * 天气统计 * @author partner4java * */ public class StatisticsDisplay implements Observer, DisplayElement { /** 最高温度 */ private float maxTemp = 0.0f; /** 最低温度 */ private float minTemp = 200; /** 记录总温度 */ private float tempSum = 0.0f; /** 温度读取次数 */ private int countReadings; public StatisticsDisplay(Observable observable){ observable.addObserver(this); } @Override public void update(Observable observable, Object arg) { if(observable instanceof WeatherData){ WeatherData weatherData = (WeatherData) observable; float temperature = weatherData.getTemperature(); tempSum += temperature; countReadings++; if(temperature < minTemp){ minTemp = temperature; }else if(temperature > maxTemp){ maxTemp = temperature; } display(); } } @Override public void display() { System.out.println("Avg/Max/Min temperature = " + (tempSum / countReadings) + "/" + maxTemp + "/" + minTemp); } }
package cn.partner4java.weather2.test; import cn.partner4java.weather2.CurrentConditionsDisplay; import cn.partner4java.weather2.StatisticsDisplay; import cn.partner4java.weather2.WeatherData; /** * 气象站测试(启动气象站) * @author partner4java * */ public class WeatherStation { /** * @param args */ public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); weatherData.setMeasurements(80, 234, 20.4f); weatherData.setMeasurements(56, 232, 20.4f); weatherData.setMeasurements(34, 123, 20.4f); } }