-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.observer.js
More file actions
40 lines (36 loc) · 1.15 KB
/
6.observer.js
File metadata and controls
40 lines (36 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 观察者的设计模式 基于发布订阅的设计模式的
// 发布订阅 发布者 订阅者 两者之间是没有关系的
// 观察者模式 观察者 和 被观察者是有关系的(你中有我,我中有你)
// 被观察者 保存了 所有的观察者
// 被观察者状态一旦发生改变 就会通知所有的观察者去update
// 被观察者 (宝宝)
class Subject {
constructor(){
this.state = "开心"; //被观察者自身的一个初始化的状态
this.arrs = []; //专门用来存储观察者的
}
attach(observer){
this.arrs.push(observer);
}
setState(state){
this.state = state;
this.arrs.forEach(observer=>observer.update(state));
}
}
// 观察者 (我、我媳妇)
class Observer {
constructor(name){
this.name = name;
}
update(state){
console.log(`${this.name}知道了: 宝宝 ${state}`);
}
}
let subject = new Subject();
let me = new Observer("我");
let wife = new Observer("我媳妇");
//把观察者放到被观察者身上去
subject.attach(me);
subject.attach(wife);
//宝宝状态发生改变
subject.setState("不开心了");