当前位置:网站首页>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
边栏推荐
- Graphical lower_bound & upper_bound
- JetPack组件Databinding
- IDEA 注释报红解决
- TCP/IP four-layer model
- 【编译原理】词法分析程序设计原理与实现
- 公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
- MultipartFile file upload
- Installation of mysql5.7.37 under CentOS7 [perfect solution]
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
- 11. Redis implements follow, unfollow, and follow and follower lists
猜你喜欢

Local area network computer hardware information collection tool

Chapter 9 SVM Practice

刚出道“一战成名”,安全、舒适一个不落

12 Disk related commands

【C语言】进制转换一般方法

Office automation case: how to automatically generate period data?

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

关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法

6. Display comments and replies

字体压缩神器font-spider的使用
随机推荐
Why is String immutable?
Software accumulation -- Screenshot software ScreenToGif
你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
15、网站统计数据
SQALE 是什么
下载jar包的好地方
Discourse Custom Header Links
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
How to build a private yum source
execsnoop tool
JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply
The use of font compression artifact font-spider
4. Sensitive word filtering (prefix tree)
7. List of private messages
7年经验,功能测试工程师该如何一步步提升自己的能力呢?
CloudCompare & PCL calculate the degree of overlap between two point clouds
YOLOV5学习笔记(二)——环境安装+运行+训练
原子操作 CAS
Unity3D Button mouse hover enter and mouse hover exit button events
Chapter 9 SVM实践