当前位置:网站首页>观察者模式
观察者模式
2022-06-23 07:42:00 【白羊座橙子的学习笔记】
1、需求

2、观察者模式工作原理 + 类图设计
当subject中的数据发生改变时,会通知所有的obervers;
实际上就是subject 维护了一个oberver的list,observer可以动态的加入subject的list,或者是从中移除

3、观察者模式实现上述需求
/** * @author houChen * @date 2022/6/19 21:51 * @Description: */
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
/** * @Description: * 1、包含最新的天气情况信息 * 2、 拥有观察者集合,使用ArrayList 进行管理 * 3、当数据有更新时,就通知所有的接入方最新的消息 */
public class WeatherData implements Subject {
private float temperatrue;
private float pressure;
private float humidity;
//观察者集合
private ArrayList<Observer> observers;
public WeatherData() {
this.observers = new ArrayList<Observer>();
}
public float getTemperature() {
return temperatrue;
}
public float getPressure() {
return pressure;
}
public float getHumidity() {
return humidity;
}
public void setData(float temperature, float pressure, float humidity) {
this.temperatrue = temperature;
this.pressure = pressure;
this.humidity = humidity;
this.notifyObservers();
}
//注册一个观察者
@Override
public void registerObserver(Observer o) {
this.observers.add(o);
}
//移除一个观察者
@Override
public void removeObserver(Observer o) {
if (this.observers.contains(o)) {
this.observers.remove(o);
}
}
//遍历所有的观察者,并进行通知
@Override
public void notifyObservers() {
for (int i = 0; i < this.observers.size(); i++) {
this.observers.get(i).update(this.temperatrue, this.pressure, this.humidity);
}
}
}
/** * @author houChen * @date 2022/6/19 21:55 * @Description: 观察者接口 */
public interface Observer {
public void update(float temperature,float pressure,float humidity);
}
/** * @author houChen * @date 2022/6/19 21:58 * @Description: 具体观察者 */
public class CurrentCondition implements Observer {
// 温度,气压,湿度
private float temperature;
private float pressure;
private float humidity;
//更新 天气情况,是由 WeatherData 来调用,我使用推送模式
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("***Today mTemperature: " + temperature + "***");
System.out.println("***Today mPressure: " + pressure + "***");
System.out.println("***Today mHumidity: " + humidity + "***");
}
}
/** * @author houChen * @date 2022/6/19 22:14 * @Description: */
public class Client {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
//创建一个观察者
CurrentCondition currentCondition = new CurrentCondition();
//注册观察者
weatherData.registerObserver(currentCondition);
weatherData.setData(10, 100, 30.3f);
}
}
4、观察者模式好处
- 观察者模式设计后,会以集合的方式来管理用户(Observer),包括注册移除和通知
- 这样,我们在增加观察者时,就不需要修改核心类WeatherData,
JDK源码: Observale 类就运用了观察者模式
边栏推荐
- aquatone工具 中的2個bug修複
- 1278_ FreeRTOS_ Understand the delayed task with the prvaddcurrenttasktodelayedlist interface
- AVL树的实现
- js中的同步和异步
- Google common syntax
- 船长阿布的灵魂拷问
- The sandbox has reached a cooperation with football player to bring popular football cartoons and animation into the metauniverse
- Tri rapide + Tri par bulle + Tri par insertion + Tri par sélection
- QT reading XML files using qdomdocument
- GTEST death test
猜你喜欢

1278_ FreeRTOS_ Understand the delayed task with the prvaddcurrenttasktodelayedlist interface

Acwing第 56 场周赛【完结】

值得反复回味的81句高人高语

YGG Spain subdao Ola GG officially established

Acwing第 56 場周賽【完結】

81 sentences worth repeating

The sandbox has reached a cooperation with football player to bring popular football cartoons and animation into the metauniverse

图像分割-改进网络结构

vtk.js鼠標左鍵滑動改變窗比特和窗寬

PHP 文件包含 -ctf
随机推荐
Imperva- method of finding regular match timeout
INT 104_ LEC 06
图像分割-改进网络结构
Take you to tiktok. That's it
Implementation principle and source code analysis of ThreadPoolExecutor thread pool
深度学习------不同方法实现lenet-5模型
The essence of five good books on wealth and freedom
GTEST死亡测试
transform的结构及用法
qt 不规则图形 消除锯齿
如何在conda虚拟环境开启jupyter-notebook
Create an orderly sequence table and perform the following operations: 1 Insert element x into the table and keep it in order; 2. find the element with the value of X, and delete it if found; 3. outpu
Location of firewalld configuration file
vtk. JS left mouse button sliding to change window level and window width
MIT CMS.300 Session 12 – IDENTITY CONSTRUCTION 虚拟世界中身份认同的建立 part 2
Kwai 350014
Interview questions of a company in a certain month of a certain year (1)
Commonly used bypass methods for SQL injection -ctf
5本财富自由好书的精华
Playwirght深度入门