观察者(observer)模式原理
observer:接收输入
观察者模式:对象之间多对一依赖的一种设计方案,被依赖的对象为Subject,依赖的对象为Observer,Subject通知Observer变化 比如一个定奶项目:这里的奶站是Subject 是1这一方,用户Observer是多的一方
一个应用在天气气象通知的观察者demo
//一个观察者
package com.ljb.meteorologicalservicesPro;
/**
* Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
*
* @author Sw_Ljb
* @version 1.0
* @ClassName Observe
* @Date 2022/4/21下午4:43
*/
public interface Observe {
void update(int temperature, int humidity, float Airpressure);
}
//一个订阅者抽象类
package com.ljb.meteorologicalservicesPro;
/**
* Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
*
* @author Sw_Ljb
* @version 1.0
* @ClassName Subject
* @Date 2022/4/21下午4:45
*/
public interface Subject {
void regisiterObject(Observe o);
void removeObject(Observe o);
void notifyObject();
}
//一个气象站
package com.ljb.meteorologicalservicesPro;
import com.ljb.meteorologicalservices.CurrentConditons;
import java.util.ArrayList;
/**
* Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
*
* @author Sw_Ljb
* @version 1.0
* @ClassName WeatherDate
* @Date 2022/4/21下午4:18
*/
public class WeatherDate implements Subject{
private int temperature;
private int humidity;
private float Airpressure;
//集合
private ArrayList<Observe> arrayList;
//private CurrentConditons currentConditons;
//导入第三方
public WeatherDate() {
arrayList = new ArrayList<Observe>();
}
public int getTemperature() {
return temperature;
}
public int getHumidity() {
return humidity;
}
public float getAirpressure() {
return Airpressure;
}
public void dateChange(){
// currentConditons.update(getTemperature(),getHumidity(),getAirpressure());
notifyObject();
}
public void setData(int temperature,int humidity,float Airpressure){
this.temperature = temperature;
this.humidity= humidity;
this.Airpressure = Airpressure;
dateChange();
}
@Override
public void regisiterObject(Observe o) {
arrayList.add(o);
}
@Override
public void removeObject(Observe o) {
//移除前先判断有没有
if (arrayList.contains(o))
arrayList.remove(o);
}
@Override
public void notifyObject() {
for (Observe o:arrayList
) {
o.update(getTemperature(),getHumidity(),getAirpressure());
}
}
}
//一个第三方
package com.ljb.meteorologicalservicesPro;
/**
* Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
*
* @author Sw_Ljb
* @version 1.0
* @ClassName CurrentConditons
* @Date 2022/4/21下午4:28
*/
public class CurrentConditons implements Observe{
private int temperature;
private int humidity;
private float Airpressure;
public void update(int temperature, int humidity, float Airpressure) {
this.Airpressure=Airpressure;
this.humidity=humidity;
this.temperature=temperature;
display();
}
public void display(){
System.out.println("today mTem:"+temperature);
System.out.println("today mHum:"+humidity);
System.out.println("today mAir:"+Airpressure);
}
}
//第二个第三方
package com.ljb.meteorologicalservicesPro;
/**
* Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
*
* @author Sw_Ljb
* @version 1.0
* @ClassName Baidu
* @Date 2022/4/21下午5:14
*/
public class Baidu implements Observe{
private int temperature;
private int humidity;
private float Airpressure;
public void update(int temperature, int humidity, float Airpressure) {
this.Airpressure=Airpressure;
this.humidity=humidity;
this.temperature=temperature;
display();
}
public void display(){
System.out.println("===百度===");
System.out.println("baidu today mTem:"+temperature);
System.out.println("baidu today mHum:"+humidity);
System.out.println("baidu today mAir:"+Airpressure);
}
}
//客户端
package com.ljb.meteorologicalservicesPro;
/**
* Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
*
* @author Sw_Ljb
* @version 1.0
* @ClassName Client
* @Date 2022/4/21下午5:11
*/
public class Client {
public static void main(String[] args) {
WeatherDate weatherDate = new WeatherDate();
CurrentConditons currentConditons = new CurrentConditons();
Baidu baidu = new Baidu();
weatherDate.regisiterObject(currentConditons);
System.out.println("============");
weatherDate.regisiterObject(baidu);
weatherDate.setData(23,43,43.4f);
}
}
好处:
1、观察者模式设计后 会以集合的方式来管理用户(Observer) 包括注册 移动 通知
2、这样我们增加观察者(理解为一个新的公告板) 就不需要去修改核心类weatherData不会修改代码 遵循了OCP原则