当前位置:网站首页>观察者模式
观察者模式
2022-08-04 00:53:00 【橙子19911016】
1. 观察者模式
观察者模式:一个或几个对象监听着另一个对象,只要被监听对象的状态发生改变,监听者要随之做出相应的反应。
concrete [ˈkɑːnkriːt] 具体的;实在的
以下是相关代码:
abstract class Subject {
private ArrayList<Observer> observers;
public Subject() {
observers = new ArrayList<>();
}
public void addObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObserver(int state) {
for (Observer o : observers) {
o.update(state);
}
}
}
class ConcreteSubject extends Subject {
private int subjectState;
public int getState() {
return subjectState;
}
public void setState(int newState) {
subjectState = newState;
System.out.println("subject's state change to " + newState);
this.notifyObserver(subjectState);
}
}
interface Observer {
void update(int state);
}
class ConcreteObserver1 implements Observer {
@Override
public void update(int state) {
System.out.println("ConcreteObserver1 knows state had changed to " + state);
doSomething();
}
private void doSomething() {
System.out.println("ConcreteObserver1 do something!");
}
}
class ConcreteObserver2 implements Observer {
@Override
public void update(int state) {
System.out.println("ConcreteObserver2 knows state had changed to " + state);
doSomething();
}
private void doSomething() {
System.out.println("ConcreteObserver2 do something!");
}
}
class ConcreteObserver3 implements Observer {
@Override
public void update(int state) {
System.out.println("ConcreteObserver3 knows state had changed to " + state);
doSomething();
}
private void doSomething() {
System.out.println("ConcreteObserver3 do something!");
}
}
class Test {
public static void main(String[] args) {
ConcreteSubject subject = new ConcreteSubject();
Observer o1 = new ConcreteObserver1();
Observer o2 = new ConcreteObserver2();
Observer o3 = new ConcreteObserver3();
subject.addObserver(o1);
subject.addObserver(o2);
subject.addObserver(o3);
subject.setState(2);
subject.removeObserver(o2);
subject.setState(5);
}
}
// subject's state change to 2
// ConcreteObserver1 knows state had changed to 2
// ConcreteObserver1 do something!
// ConcreteObserver2 knows state had changed to 2
// ConcreteObserver2 do something!
// ConcreteObserver3 knows state had changed to 2
// ConcreteObserver3 do something!
// subject's state change to 5
// ConcreteObserver1 knows state had changed to 5
// ConcreteObserver1 do something!
// ConcreteObserver3 knows state had changed to 5
// ConcreteObserver3 do something!
2 发布订阅模式
有很多人认为发布订阅模式就是观察者模式,Publisher == Subject 被观察者
,Subscriber == Observer 观察者
,其实不然。在发布订阅模式中,被观察并不会直接通知订阅者,换句话说,发布者和订阅者并不相干。如果互不相干,它们之间是如何进行交流的?通过第三者——事件总线或者叫做经纪人(Broker
)。
broker [ˈbroʊkər] 经纪人,中间人
如果说观察者模式实现了松耦合(loosely coupled),发布订阅模式就是完全解耦的。
loosely [ˈluːsli] 宽松地 coupled [ˈkʌpld] 耦合的
参考
边栏推荐
- 分子个数 数论(欧拉函数 前缀和
- 2015年开源大事件汇总
- 114. How to find the cause of Fiori Launchpad routing error by single-step debugging
- WMS仓储管理系统能解决电子行业哪些仓库管理问题
- Sqlnet. Ora file with the connection of authentication test
- NLP resources that must be used for projects [Classified Edition]
- Google Earth Engine - Calculates the effective width of rivers using publicly available river data
- 字符串的排列
- The problem of disorganized data output by mnn model
- 微服务的简单介绍
猜你喜欢
手撕Gateway源码,今日撕工作流程、负载均衡源码
电子组装行业对MES管理系统的需求分析
Nanoprobes丨Nanogold-抗体和链霉亲和素偶联物
C语言 函数递归
typescript50-交叉类型和接口之间的类型说明
咱们500万条数据测试一下,如何合理使用索引加速?
Nanoprobes Alexa Fluor 488 FluoroNanogold 偶联物
ASP.NET 获取数据库的数据并写入到excel表格中
Getting started with MATLAB 3D drawing command plot3
Web3 security risks daunting?How should we respond?
随机推荐
c语言分层理解(c语言操作符)
2022年8月份DAMA-CDGA/CDGP数据治理认证招生简章
Eight things to pay attention to in spot silver
"Miscellaneous" barcode by Excel as a string
Analysis: What makes the Nomad Bridge hack unique
字符串变形
一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
typescript50-交叉类型和接口之间的类型说明
C 学生管理系统_添加学生
因为一次bug的教训,我决定手撕Nacos源码(先撕客户端源码)
全面讲解 Handler机制原理解析 (小白必看)
【超详细】手把手教你搭建MongoDB集群搭建
建木DevOps流程的快速运用
DataBinding下的RecycleView适配器Adapter基类
C语言 函数递归
ES6高级-迭代器与生成器的用法
中原银行实时风控体系建设实践
求解同余方程 数论 扩展欧几里得
电子组装行业对MES管理系统的需求分析
How to find the cause of Fiori Launchpad routing errors by single-step debugging