当前位置:网站首页>Observer mode
Observer mode
2022-06-23 08:09:00 【Study notes of Aries orange】
1、 demand

2、 How observer mode works + The class diagram design
When subject When the data in the , All of them will be informed obervers;
It's actually subject One was maintained oberver Of list,observer Can dynamically join subject Of list, Or remove from it 

3、 The observer pattern implements the above requirements
/** * @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、 Contains the latest weather information * 2、 Have a collection of observers , Use ArrayList Conduct management * 3、 When the data is updated , Notify all access parties of the latest information */
public class WeatherData implements Subject {
private float temperatrue;
private float pressure;
private float humidity;
// Observers gather
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();
}
// Register an observer
@Override
public void registerObserver(Observer o) {
this.observers.add(o);
}
// Remove an observer
@Override
public void removeObserver(Observer o) {
if (this.observers.contains(o)) {
this.observers.remove(o);
}
}
// Traverse all observers , And give notice
@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: Observer interface */
public interface Observer {
public void update(float temperature,float pressure,float humidity);
}
/** * @author houChen * @date 2022/6/19 21:58 * @Description: Concrete observer */
public class CurrentCondition implements Observer {
// temperature , pressure , humidity
private float temperature;
private float pressure;
private float humidity;
// to update Weather conditions , By WeatherData To call , I use push mode
public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
display();
}
// Show
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();
// Create an observer
CurrentCondition currentCondition = new CurrentCondition();
// Registered observer
weatherData.registerObserver(currentCondition);
weatherData.setData(10, 100, 30.3f);
}
}
4、 Observer mode benefits
- After the observer pattern is designed , Will manage users in a collective way (Observer), Including registration removal and notification
- such , When we add observers , There is no need to modify the core class WeatherData,
JDK Source code : Observale Class uses the observer pattern
边栏推荐
- domain controller
- 顺序表课设
- C#重启应用程序
- QT project error: -1: error: cannot run compiler 'clang++' Output:mingw32-make. exe
- Deep learning ----- different methods to realize vgg16
- ArcMap批量删除距离较近的点
- @Controller和@RestController的区别?
- 看了5本书,我总结出财富自由的这些理论
- Distributed ID generation
- 2022 final examination of software project management of School of software, Shandong University (recall version)
猜你喜欢

Matlab random volatility SV, GARCH using MCMC Markov chain Monte Carlo method to analyze exchange rate time series

Does huangrong really exist?

ArcMap batch delete points closer

Active Directory之AD对象

观察者模式

开源软件、自由软件、Copyleft、CC都是啥,傻傻分不清楚?

socket编程(多进程)

PHP file contains -ctf

Imperva- method of finding regular match timeout

imperva-查找正则匹配超时的方法
随机推荐
11 字符串函数
Configuration asmx not accessible
Acwing game 56 [End]
imperva-查找正则匹配超时的方法
Socket programming -- select model
Captain Abu's soul torture
openvino系列 18. 通过OpenVINO和OpenCV实现实时的物体识别(RTSP,USB视频读取以及视频文件读取)
忽略超长参数违规
GTEST死亡测试
flutter 制作TabBar的圆角
google常用语法
正则表达式使用案例
Markdown learning
Gif verification code analysis
Microsoft Exchange – prevent network attacks
图像分割-改进网络结构
5本财富自由好书的精华
Two bug fixes in aquatone tool
Commonly used bypass methods for SQL injection -ctf
Fillet the tabbar with the flutter