当前位置:网站首页>观察者模式
观察者模式
2022-07-31 02:58:00 【wfsm】
观察者(Observer) : 又称 发布-订阅模式(publish-subscribe:Pub/Sub):他是一种通知机制,让发送通知的一方(被观察方) 和接收通知的一方(观察者) 能彼此分离,互不影响
定义对象间的 一种 一对多 的依赖关系,当一个对象的状态发生改变时,所有依赖于他的对象都得到通知并自动更新
假设一个电商网站,有多种Product(商品),同时,Customer(消费者)和Admin(管理员)对商品上架、价格改变都感兴趣,希望能第一时间获得通知。于是,Store(商场)可以这么写:Store
希望发送通知给那些 关心 Product
的人,而不想知道这些人是谁,,观察者模式就是分离被观察者,和观察者之间的耦合关系
将想要观察的人都实现一个接口,,在Store 修改商品信息的时候,并推送给这些观察者
/** * 要观察 这个产品 需要实现的接口 */
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
边栏推荐
- 关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
- print task sorting js od huawei
- AI在医疗影像设备全流程应用
- Office automation case: how to automatically generate period data?
- 你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
- 10 权限介绍
- Mysql 45讲学习笔记(二十四)MYSQL主从一致
- 8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
- 10、Redis实现点赞(Set)和获取总点赞数
- 15. Website Statistics
猜你喜欢
YOLOV5 study notes (2) - environment installation + operation + training
Mysql 45讲学习笔记(二十四)MYSQL主从一致
Office automation case: how to automatically generate period data?
Chapter 9 SVM Practice
Installation of mysql5.7.37 under CentOS7 [perfect solution]
19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing
Discourse Custom Header Links
Local area network computer hardware information collection tool
MPPT solar charge controller data collection - through the gateway acquisition capacity battery SOC battery voltage, wi-fi
4、敏感词过滤(前缀树)
随机推荐
YOLOV5学习笔记(二)——环境安装+运行+训练
Local area network computer hardware information collection tool
Brute Force/Adjacency List Breadth First Directed Weighted Graph Undirected Weighted Graph
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
学习DAVID数据库(1)
经典链表OJ强训题——快慢双指针高效解法
LeetCode 每日一题 2022/7/25-2022/7/31
【HCIP】ISIS
15、网站统计数据
The use of font compression artifact font-spider
你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
医疗影像领域AI软件开发流程
10 Permission introduction
SQL 面试用题(重点)
What is a distributed lock?Three ways of implementing distributed lock
CefSharp入门-winform
跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
BAT can't sell "Medical Cloud": Hospitals flee, mountains stand, and there are rules
CentOS7下mysql5.7.37的卸载【完美方案】
Thesis framework of the opening report