-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver mode.cpp
97 lines (88 loc) · 1.83 KB
/
observer mode.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <string>
#include <iostream>
#include <vector>//容器向量
using namespace std;
class SecretaryBase;
//抽象观察者
class CObserverBase
{
protected:
string name;
SecretaryBase* sub;
public:
CObserverBase(string strname,SecretaryBase* strsub)
{
name=strname;
sub=strsub;
}
virtual void Update()=0;
};
//具体的观察者,看股票的
class StockObserver : public CObserverBase
{
public://构造函数继承自基类的构造函数
StockObserver(string strname,SecretaryBase* strsub) : CObserverBase(strname,strsub)
{
}
virtual void Update();
};
//具体观察者,看NBA的
class NBAObserver : public CObserverBase
{
public:
NBAObserver(string strname,SecretaryBase* strsub) : CObserverBase(strname,strsub){}
virtual void Update();
};
//抽象通知者
class SecretaryBase
{
public:
string action;
vector<CObserverBase*> observers;//聚合关系 ,两个抽象类耦合应尽量少
public:
virtual void Attach(CObserverBase* observer)=0;
virtual void Notify()=0;
};
//具体通知者
class Secretary :public SecretaryBase
{
public:
void Attach(CObserverBase* ob)
{
observers.push_back(ob);
}
void Notify()
{
vector<CObserverBase*>::iterator p = observers.begin();
while (p!=observers.end())
{
(*p)->Update();
p++;
}
}
};
void StockObserver::Update()
{
cout<<name<<":"<<sub->action<<",不要玩股票了,要开始工作了"<<endl;
}
void NBAObserver::Update()
{
cout<<name<<":"<<sub->action<<",不要看NBA了,老板来了"<<endl;
}
//客户端:
int main()
{
SecretaryBase *p=new Secretary(); //创建观察者
//被观察的对象,构造函数有两个参数
CObserverBase *s1= new NBAObserver("小李",p);
CObserverBase *s2 = new StockObserver("小赵",p);
//加入观察队列
p->Attach(s1);
p->Attach(s2);
//事件
p->action="老板来了";
//通知
p->Notify();
system("pause");
return 0;
}