当前位置:网站首页>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
边栏推荐
- 【C语言基础】解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token
- PMP微信群日常习题
- 【HCIP】ISIS
- Basic learning about Redis related content
- 8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)
- CorelDRAW2022 streamlined Asia Pacific new features in detail
- 4、敏感词过滤(前缀树)
- False positives and false negatives in testing are equally worthy of repeated corrections
- Mysql 45讲学习笔记(二十五)MYSQL保证高可用
- 递归查询单表-单表树结构-(自用)
猜你喜欢

8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)

SQL注入 Less54(限制次数的SQL注入+union注入)

分布式系统架构需要解决的问题

IDEA 注释报红解决

TCP详解(二)

Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击

Software accumulation -- Screenshot software ScreenToGif

SQL注入 Less46(order by后的注入+rand()布尔盲注)

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

编译Hudi
随机推荐
【HCIP】ISIS
TCP详解(一)
Mathematics to solve the problem - circular linked list
Clustering index, and what is the difference between a clustering index
什么是分布式锁?实现分布式锁的三种方式
什么是系统?
The difference between link and @import
IDEA 注释报红解决
一份高质量的测试用例如何养成?
[Godot][GDScript] 二维洞穴地图随机生成
What is SQALE
分布式系统架构需要解决的问题
[Compilation principle] Lexical analysis program design principle and implementation
Multilingual settings of php website (IP address distinguishes domestic and foreign)
return in try-catch
f.grid_sample
MultipartFile文件上传
8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
10 权限介绍
SQL注入 Less47(报错注入) 和Less49(时间盲注)