设计模式实现
狐七 3/14/2022 programming
# 发布订阅模式
Class Notify {
constructor() {
this.subscribers = []
}
add(handler) {
this.subscribers.push(handler)
}
emit() {
this.subscribers.forEach(subscriber => subscriber())
}
}
let notify = new Notify()
// 收集依赖
notify.add(() => {
console.log('emit here');
})
// 执行任务
notify.emit()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20