当前位置:网站首页>"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
边栏推荐
- SSH服务器拒绝了密码
- How many ways can you assign initial values to a two-dimensional array?
- 32 chrome调试工具的使用
- (原创)自定义一个滚屏的RecyclerView
- 阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
- 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_因式分解_简单_263.丑数
- Browser based split screen reading
- kibana操作es
- MySQL 45讲 | 06 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
猜你喜欢

D2. Chopping Carrots (Hard Version) (每日一题)

【口才】谈判说服技巧及策略
![优质数对的数目[位运算特点+抽象能力考察+分组快速统计]](/img/c9/8f8f0934111f7ae8f8abd656d92f12.png)
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]

Vs2017 large factory ERP management system source code factory general ERP source code

SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
![Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]

I hope some suggestions on SQL optimization can help you who are tortured by SQL like me

43 box model

Gameframework making games (II) making UI interface

Browser based split screen reading
随机推荐
H5页面input输入框弹起数字键盘,需要支持小数点
C language and SQL Server database technology
32 chrome调试工具的使用
LeetCode_因式分解_简单_263.丑数
Content type corresponding to office file
【口才】谈判说服技巧及策略
Software testing -- 1. Outline of software testing knowledge
English语法_不定代词 - other / another
Kibana operation es
Spark 参数配置的几种方法
[Nuxt 3] (十一) 传送 & 模块
sudo rosdep init Error ROS安装问题解决方案
Examples of bio, NiO, AIO
How to make a set of code fit all kinds of screens perfectly?
关于ROS2安装connext RMW的进度条卡在13%问题的解决办法
关于RDBMS和非RDBMS【数据库系统】
Qt connect 中, SIGNAL,SLOT 与 lambda 对比
变分(Calculus of variations)的概念及运算规则
QT connect, signal, slot and lambda comparison
SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你