当前位置:网站首页>"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
边栏推荐
- IP地址分类,判断一个网段是子网超网
- Ssh server rejected password
- Quickly set up dobbo demo
- Throwing OutOfMemoryError “Could not allocate JNI Env“
- How to make a set of code fit all kinds of screens perfectly?
- 【口才】谈判说服技巧及策略
- LeetCode-198-打家劫舍
- Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
- Educational codeforces round 132 (rated for Div. 2) C, d+ac automata
- Awk from getting started to digging in (21) awk script debugging
猜你喜欢
随机推荐
Idea error failed to determine a suitable driver class
I hope some suggestions on SQL optimization can help you who are tortured by SQL like me
[C题目]力扣876. 链表的中间结点
Gson and fastjson
Practical guide for network security emergency response technology (Qianxin)
43 box model
44 新浪导航 ,小米边栏 练习
Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
D2. picking carrots (hard version) (one question per day)
IP address classification, which determines whether a network segment is a subnet supernetwork
27 选择器的分类
006操作符简介
Realsense ROS installation configuration introduction and problem solving
The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%
IP地址分类,判断一个网段是子网超网
L1和L2正则化
LeetCode_字符串_中等_151.颠倒字符串中的单词
Realsense-Ros安装配置介绍与问题解决
QObject源码剖析-d指针和q指针
冈萨雷斯 数字图像处理 第一章绪论




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



