当前位置:网站首页>"How to use" observer mode
"How to use" observer mode
2022-07-25 14:53:00 【Senior fishing Engineer】
This paper
Define one to many dependencies between objects , thus , When an object changes state , All objects that depend on it will be notified , And automatically update .
The theme 【 Publisher 】
import java.util.Observable;
@Data
public class SubjectData extends Observable {
/** * Subject data subscribed by observers */
private String lookData;
/** * Call this method when the subject data changes , At the same time, time subscription publishing * @param changeData : Data of subject changes */
public void lookDataChanged(String changeData) {
// Modify subject data
lookData = changeData;
// Set the change state of the subject object data
this.setChanged();
// Notify all observers
this.notifyObservers(lookData);
}
}
subscriber
import java.util.Observable;
import java.util.Observer;
public class ObserverOne implements Observer {
/** * Observers need to hold references to topics , The function of this reference is to enable the observer to actively pull the subject data , Instead of relying solely on the full volume push of the topic */
private Observable observable;
/** * Through a construction method , Implement instantiation of topic object */
public ObserverOne(Observable observable) {
this.observable = observable;
}
/** * The observer passively updates the data * When the subject object data changes, call lookDataChanged() After method , This method serves as the subscriber's method * Will handle update logic * @param o * @param arg */
@Override
public void update(Observable o, Object arg) {
if (o instanceof SubjectData) {
String lookData = (String) arg;
System.out.println("ObserverOne get lookData = " + lookData);
}
}
/** * The observer actively pulls data * Because the observer already contains the reference of the subject object through the construction method , Therefore, it can actively pull data */
public String getData() {
if (observable instanceof SubjectData) {
SubjectData subjectData = ((SubjectData) observable);
return subjectData.getLookData();
}
return "instance error";
}
}
test
public class TestDrive {
public static void main(String[] args) {
// Instantiate the topic object
SubjectData subjectData = new SubjectData();
// Instantiate the observer , And build references to topic objects
ObserverOne observerOne = new ObserverOne(subjectData);
ObserverTwo observerTwo = new ObserverTwo(subjectData);
// Add an observer to the subscription list of the topic
subjectData.addObserver(observerOne);
subjectData.addObserver(observerTwo);
// The topic tries to modify its state
subjectData.lookDataChanged(" Today, Wednesday ");
// Because the observer has the quotation of the topic , Therefore, you can actively pull data by yourself
String observerOneData = observerOne.getData();
System.out.println("observerOneData = " + observerOneData);
}
}
Output :
ObserverTwo get lookData = Today, Wednesday
ObserverOne get lookData = Today, Wednesday
observerOneData = Today, Wednesday
边栏推荐
- Idea error failed to determine a suitable driver class
- Gateway reports an error service_ UNAVAILABLE
- BigDecimal rounds the data
- (原创)自定义一个滚屏的RecyclerView
- Leetcode-198- house raiding
- The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
- SSH服务器拒绝了密码
- Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer
- 41 picture background synthesis - colorful navigation map
- Paddlenlp's UIE relationship extraction model [executive relationship extraction as an example]
猜你喜欢

Development of uni app offline ID card identification plug-in based on paddleocr

基于浏览器的分屏阅读
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index

关于ROS2安装connext RMW的进度条卡在13%问题的解决办法

43 box model

Alibaba cloud installs mysql5.7

37 element mode (inline element, block element, inline block element)
![应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]

Go语言创始人从Google离职

The security market has entered a trillion era, and the security B2B online mall platform has been accurately connected to deepen the enterprise development path
随机推荐
LeetCode-198-打家劫舍
L1和L2正则化
IP地址分类,判断一个网段是子网超网
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]
43 盒子模型
BIO、NIO、AIO示例
Jmeter的随机数函数怎么用
QObject source code analysis -d pointer and Q pointer
06. Neural network like
GameFramework制作游戏(一)
Idea error failed to determine a suitable driver class
MySQL的登陆【数据库系统】
Copy files / folders through Robocopy
gson与fastjson
Kibana operation es
awk从入门到入土(21)awk脚本调试
Sudo rosdep init error ROS installation problem solution
I2C device driver hierarchy
D2. Chopping Carrots (Hard Version) (每日一题)
微信公众号正式环境上线部署,第三方公众平台接入