当前位置:网站首页>Observer pattern
Observer pattern
2022-07-31 03:03:00 【wfsm】
观察者(Observer) : 又称 发布-订阅模式(publish-subscribe:Pub/Sub):He is a notification mechanism,让发送通知的一方(被观察方) 和接收通知的一方(观察者) 能彼此分离,互不影响
定义对象间的 一种 一对多 的依赖关系,当一个对象的状态发生改变时,所有依赖于他的对象都得到通知并自动更新
假设一个电商网站,有多种Product(商品),同时,Customer(消费者)和Admin(管理员)对商品上架、价格改变都感兴趣,希望能第一时间获得通知.于是,Store(商场)可以这么写:StoreHope to send notifications to those 关心 Product的人,And don't want to know who these people are,,Observer pattern is to separate the observed,和观察者之间的耦合关系
Implement an interface for everyone you want to observe,,在Store When modifying product information,and push to these observers
/** * 要观察 这个产品 需要实现的接口 */
public interface ProductObserver {
void onPublished(Product product);
void onPriceChange(Product product);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private String name;
private double price;
}
public class Store {
private List<ProductObserver> observers = new ArrayList<>();
private Map<String,Product> map = new HashMap<>();
// 注册观察者
public void addObserver(ProductObserver productObserver){
this.observers.add(productObserver);
}
// 取消注册
public void removeObserver(ProductObserver productObserver){
this.observers.remove(productObserver);
}
public void addNewProduct(String name,double price){
Product product = new Product(name, price);
map.put(name,product);
// 通知观察者
observers.forEach(observer->{
observer.onPublished(product);});
}
public void setProductPrice(String name,double price){
Product product = map.get(name);
product.setPrice(price);
// 通知观察者
observers.forEach(observer->{
observer.onPriceChange(product);});
}
}
测试:
public class Main {
public static void main(String[] args) {
Store store = new Store();
store.addObserver(new Observer01());
store.addObserver(new Observer02());
store.addNewProduct("gift",2d);
store.setProductPrice("gift",3d);
}
static class Observer01 implements ProductObserver{
@Override
public void onPublished(Product product) {
System.out.println("observer01 publish product "+product.getName());
}
@Override
public void onPriceChange(Product product) {
System.out.println("observer01 price change : "+product.getPrice());
}
}
static class Observer02 implements ProductObserver{
@Override
public void onPublished(Product product) {
System.out.println("observer02 publish product");
}
@Override
public void onPriceChange(Product product) {
System.out.println("observer02 price change");
}
}
}
引用:https://blog.csdn.net/u013087513/article/details/51839986
https://www.jianshu.com/p/2bb48cde23c9
https://www.liaoxuefeng.com/wiki/1252599548343744/1281319577321505
边栏推荐
- 7年经验,功能测试工程师该如何一步步提升自己的能力呢?
- CloudCompare & PCL calculate the degree of overlap between two point clouds
- 5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
- Modbus on AT32 MCUs
- 8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
- Chapter 9 SVM Practice
- Moxa NPort device flaw could expose critical infrastructure to devastating attack
- 品牌广告投放平台的中台化应用与实践
- SQL 面试用题(重点)
- 【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
猜你喜欢

C# remote debugging

分布式与集群是什么 ? 区别是什么?

Pythagorean tuple od js

共模电感的仿真应用来了,满满的干货送给大家

想从手工测试转岗自动化测试,需要学习哪些技能?

Getting Started with CefSharp - winform

JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply

4、敏感词过滤(前缀树)

【C语言】表达式求值的一般方法

Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
随机推荐
False positives and false negatives in testing are equally worthy of repeated corrections
Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
冒泡排序、选择排序、直接插入排序、二分法查找
4. Sensitive word filtering (prefix tree)
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
工程(五)——小目标检测tph-yolov5
刚出道“一战成名”,安全、舒适一个不落
学习DAVID数据库(1)
Project (5) - Small target detection tph-yolov5
The whole process scheduling, MySQL and Sqoop
分布式系统架构需要解决的问题
16、热帖排行
【shell基础】判断目录是否为空
遗留系统的自动化策略
原子操作 CAS
什么是分布式锁?实现分布式锁的三种方式
【HCIP】ISIS
Compile Hudi
Discourse 自定义头部链接(Custom Header Links)
C# remote debugging