当前位置:网站首页>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
边栏推荐
猜你喜欢

【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

【编译原理】词法分析程序设计原理与实现

【Android】Room —— SQLite的替代品

Addition and Subtraction of Scores in LeetCode Medium Questions

【编译原理】递归下降语法分析设计原理与实现

软件积累 -- 截图软件ScreenToGif

字体压缩神器font-spider的使用

6. Display comments and replies

Mathematics to solve the problem - circular linked list

Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
随机推荐
Clustering index, and what is the difference between a clustering index
SQL注入 Less54(限制次数的SQL注入+union注入)
10、Redis实现点赞(Set)和获取总点赞数
加密公司向盗窃的黑客提供报价:保留一点,把剩下的归还
【C语言】三子棋(经典解法+一览图)
原子操作 CAS
接口测试关键技术
6. Display comments and replies
Addition and Subtraction of Scores in LeetCode Medium Questions
【shell基础】判断目录是否为空
多线程下类对象的服务承诺探讨
局域网电脑硬件信息收集工具
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
IIR滤波器和FIR滤波器
12 Disk related commands
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
JetPack component Databinding
【编译原理】递归下降语法分析设计原理与实现
JetPack组件Databinding