当前位置:网站首页>Observer model of behavioral model
Observer model of behavioral model
2022-07-25 23:57:00 【w ͏ l ͏ j ͏】
Concept
Observer mode Defines one to many dependencies between objects , thus , When an object changes state , All its dependents are notified and automatically updated . Also known as release order Reading mode .
chart

Role analysis :
- Subject: Observed . Define the responsibilities that must be fulfilled by the observed , Must be able to dynamically increase 、 Cancel the observer . Generally, it is an abstract class or interface , Only fulfill the duties that the observed must fulfill : Manage the observer and inform the observer .
- Observer The observer : After the observer receives the message , Process information .
- ConcreteSubject The specific observed : Define the business logic of the observed , It also defines which events are notified .
- ConCreteObserver: The processing logic reaction of each observer after receiving the message is different , Each observer has its own processing logic .
Code
Observed
package com.wlj.observer;
import java.util.ArrayList;
/** * @author wlj * @Classname AbstractSubject * @Description The observed abstract class * @Date 7/24/2022 6:06 PM */
public abstract class ASubject {
// List of observers
private ArrayList<Observer> observerArrayList = new ArrayList<>();
// Add an observer
public void addObserver(Observer observer){
this.observerArrayList.add(observer);
}
// Delete an observer
public void deleteObserver(Observer observer) {
if(observerArrayList.indexOf(observer) >= 0){
this.observerArrayList.remove(observer);
}
}
// Notify all observers
public void notifyObservers(){
for (Observer observer : observerArrayList) {
observer.update();
}
}
}
package com.wlj.observer;
/** * @author wlj * @Classname ConcreteSubject * @Description The specific observed * @Date 7/24/2022 6:25 PM */
public class ConcreteSubject extends ASubject{
public void notify(){
// You can do something else , Notify observer
super.notifyObservers();
}
}
The observer
package com.wlj.observer;
/** * @author wlj * @Classname Observer * @Description Observer mode * @Date 7/24/2022 6:07 PM */
public interface Observer {
public void update();
}
package com.wlj.observer;
/** * @author wlj * @Classname ConcreteObserver * @Description The observer A * @Date 7/24/2022 6:16 PM */
public class ConcreteObserverA implements Observer{
@Override
public void update() {
System.out.println("A Receive information for processing ");
}
}
package com.wlj.observer;
/** * @author wlj * @Classname ConcreteObserver * @Description The observer B * @Date 7/24/2022 6:16 PM */
public class ConcreteObserverB implements Observer{
@Override
public void update() {
System.out.println("B Receive information for processing ");
}
}
Use scenarios
package com.wlj.observer;
/** * @author wlj * @Classname Client * @Description Specific scenarios * @Date 7/24/2022 6:19 PM */
public class Client {
public static void main(String[] args) {
// Create an observed
ConcreteSubject subject = new ConcreteSubject();
// Create observers
Observer observerA = new ConcreteObserverA();
Observer observerB = new ConcreteObserverB();
// Add observers
subject.addObserver(observerA);
subject.addObserver(observerB);
// Notify all observers
subject.notify();
}
}
As long as it is executed by the observer notify Method , That is, when the observer makes some actions or changes , Will inform the observer .
Pattern analysis
- When the change of an object needs to be notified to multiple objects , We can use the observer mode .
- When we need to publish and subscribe , You can use observer mode .
- MessageQuene and EventBus In fact, it also uses the observer mode ,hook,callback,listener In fact, it can also be generalized as the application of observer pattern .
- The observer model is often used in conjunction with the chain of responsibility model , Observer mode can only be executed along the task chain , Cannot pass in direction .
边栏推荐
猜你喜欢
随机推荐
Leetcode 0136. numbers that appear only once: XOR
你还在用浏览器自带书签?这款书签插件超赞
Programmer interview Golden Classic 4.12 summation path
[Muduo] package EventLoop and thread
Song of statistics lyrics
firewall 命令简单操作
Macro task, micro task and event cycle mechanism
The difference between SFTP and FTP
回溯——17. 电话号码的字母组合
LeetCode 0135. 分发糖果
Key features and application trends of next generation terminal security management
C - readonly and const keywords
[Muduo] thread package
VSCode格式化Json文件
行为型模式之迭代器模式
[learning notes] solid works operation record
二叉树——530.二叉搜索树的最小绝对差
浅识 OWASP
获取马蜂窝酒店数据
redis-扩展数据类型(跳跃表/BitMaps/HyperLogLog/GeoSpatial)








