代码
public class WeatherData {private float temperature;private float pressure;private float humidity;//聚合private CurrentConditions currentConditions;public WeatherData(CurrentConditions currentConditions) {this.currentConditions = currentConditions;}public float getTemperature() {return temperature;}public float getPressure() {return pressure;}public float getHumidity() {return humidity;}public void dataChange(){currentConditions.update(getTemperature(),getPressure(),getHumidity());}//当数据有更新时候public void setData(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;dataChange();}
}
//显示当前天气情况,可以理解成是气象站自己的网站
public class CurrentConditions {//温度private float temperature;//气压private float pressure;//湿度private float humidity;//更新天气情况,是由WeatherDate来调用的,使用推送模式public void update(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;display();}public void display(){System.out.println("temperature" + temperature);System.out.println("pressure" + pressure);System.out.println("humidity" + humidity);}
}
public class Client {public static void main(String[] args) {//创建接入方CurrentConditionsCurrentConditions currentConditions = new CurrentConditions();//创建weatherData 并将接入方currentConditions 传递到WeatherData中WeatherData weatherData = new WeatherData(currentConditions);//更新天气情况weatherData.setData(30,150,40);}
}
//观察者接口,有观察者来实现
public interface Observer {void update(float temperature,float pressure,float humidity);
}
//观察者
public class CurrentConditions implements Observer{//温度private float temperature;//气压private float pressure;//湿度private float humidity;public void display(){System.out.println("temperature" + temperature);System.out.println("pressure" + pressure);System.out.println("humidity" + humidity);}@Overridepublic void update(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;display();}
}
public class BaiduSite implements Observer{//温度private float temperature;//气压private float pressure;//湿度private float humidity;public void display(){System.out.println("百度temperature" + temperature);System.out.println("百度pressure" + pressure);System.out.println("百度humidity" + humidity);}@Overridepublic void update(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;display();}
}
public interface Subject {void registerObserver(Observer observer);void removeObserver(Observer observer);void notifyObserver();
}
public class WeatherData implements Subject{private float temperature;private float pressure;private float humidity;private List<Observer> observerList;public WeatherData() {observerList = new ArrayList<>();}public float getTemperature() {return temperature;}public float getPressure() {return pressure;}public float getHumidity() {return humidity;}public void dataChange(){notifyObserver();}//当数据有更新时候public void setData(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;dataChange();}//新增观察者@Overridepublic void registerObserver(Observer observer) {observerList.add(observer);}//删除观察者@Overridepublic void removeObserver(Observer observer) {observerList.remove(observer);}//遍历所有的观察者,并通知@Overridepublic void notifyObserver() {observerList.forEach(e->{e.update(temperature,pressure,humidity);});}
}
public class Client {public static void main(String[] args) {//创建一个WeatherDataWeatherData weatherData = new WeatherData();//创建一个观察者CurrentConditions currentConditions = new CurrentConditions();BaiduSite baiduSite = new BaiduSite();//注册到weatherDataweatherData.registerObserver(currentConditions);weatherData.registerObserver(baiduSite);weatherData.setData(10,100,30);}
}