当前位置:网站首页>How to implement observer mode
How to implement observer mode
2022-06-29 09:22:00 【amateur12】
Observer mode : When an object state sends a change , It will automatically notify the objects that depend on it . Observer model belongs to behavioral model .
Realization : Instantiate the topic object , The observer then registers with the subject object , Traverse the registration , Let the subject object implement the operations provided by its interface , Tell the observer to do something .
Subject interface , The theme , Observer interface , The observer
An observer can have multiple observers , The subject is the observed
Topics provide interfaces for registration and notification , The observer provides an interface for his own operation .( These observers have the same interface .) The observer uses the topic interface to register... With the topic , The theme uses the observer interface to inform the observer . The coupling is pretty low .
The advantages of the observer model :
Subject and observer are loosely coupled . A topic only knows that its observer is an instance of a class that implements the observer , But I don't know which class it is , Among observers , Only know that the subject it depends on is an instance of a class that implements the subject , But you don't need to know which class it is .
The observer mode satisfies the open close principle , For extension development , Turn off for changes . When the requirements of the application change , Without modifying the source code or binary code of the software entity , It can extend the function of the module , Make it meet new needs .
The disadvantages of the observer model :
If an observed object has many direct and indirect observers , It will take a lot of time to inform all the observers
If there is circular dependence between the observed , The observed will trigger a circular call between them , Causing the system to crash
Although the observer pattern can let the observer know that the observed object has changed at any time , But the observer mode has no corresponding mechanism to make the observer know how the observed object changes :
Observer mode uses scenarios :
When the data of an object is updated, other objects need to be notified , But this object doesn't want to be tightly coupled to other objects .
When an object's data is updated , This object needs to let other objects update their own data , But this object doesn't know how many objects update data .
For example, microblog subscription , When we subscribe to someone's microblog account , This man has released new information , Will inform us , But he doesn't know or need to know how many people to notify .
边栏推荐
- The difference between cokkie and session
- ThinkPHP 6 uses mongodb
- ActiveMQ message component publish subscribe redelivery message redelivery
- Verilog reduction operator
- Pointnet/pointnet++ training and testing
- Wechat applet opens file stream
- SSD改进CFENet
- 调试H5页面-vConsole
- H5软键盘问题
- Share code snippets of wechat applet
猜你喜欢
随机推荐
[target detection] | indicator a probabilistic challenge for object detection
train_on_batch保存一下loss函数变化的图像
手机开户一般哪个证券公司好?究竟网上开户是否安全么?
Debug H5 page -vconsole
微信小程序wx.navigateBack返回上一页携带参数
Detecting and counting tiny faces
jar包和war包
General multiplier design, verilog code
pytorch总结学习系列-操作
H5 soft keyboard problem
UE4 材质UV纹理不随模型缩放拉伸
Jar package and war package
笔试题“将版本号从大到小排列”
Wechat applet wx Navigateback returns the parameters carried on the previous page
UE4 去掉材质中Mask透明白边
记微信小程序setData动态修改字段名
What exactly does Devops mean?
UE4 installs the datasmith plug-in in version 4.20-23
【To .NET】C#数据模型,从Entity Framework Core到LINQ
Pat (basic level) practice (Chinese) 1003 I want to pass! (20 points) C language implementation









