设计模式之观察者模式demo

时间:2023-02-14 15:13:24


设计模式之观察者模式demo

 

设计模式之观察者模式demo

设计模式之观察者模式demo

代码

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) {
//创建接入方CurrentConditions
CurrentConditions currentConditions = new CurrentConditions();
//创建weatherData 并将接入方currentConditions 传递到WeatherData中
WeatherData weatherData = new WeatherData(currentConditions);
//更新天气情况
weatherData.setData(30,150,40);
}
}

设计模式之观察者模式demo

设计模式之观察者模式demo

设计模式之观察者模式demo

设计模式之观察者模式demo

 

 

//观察者接口,有观察者来实现
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);
}

@Override
public 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);
}

@Override
public 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();
}

//新增观察者
@Override
public void registerObserver(Observer observer) {
observerList.add(observer);
}

//删除观察者
@Override
public void removeObserver(Observer observer) {
observerList.remove(observer);
}

//遍历所有的观察者,并通知
@Override
public void notifyObserver() {
observerList.forEach(e->{
e.update(temperature,pressure,humidity);
});
}
}

 

public class Client {
public static void main(String[] args) {
//创建一个WeatherData
WeatherData weatherData = new WeatherData();
//创建一个观察者
CurrentConditions currentConditions = new CurrentConditions();
BaiduSite baiduSite = new BaiduSite();


//注册到weatherData
weatherData.registerObserver(currentConditions);
weatherData.registerObserver(baiduSite);
weatherData.setData(10,100,30);

}
}