forked from FengJungle/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserver.h
More file actions
60 lines (56 loc) · 1020 Bytes
/
Observer.h
File metadata and controls
60 lines (56 loc) · 1020 Bytes
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
#ifndef __OBSERVER_H__
#define __OBSERVER_H__
#include <iostream>
using namespace std;
#include "common.h"
#include "AllyCenter.h"
// 抽象观察者 Observer
class Observer
{
public:
virtual ~Observer(){}
Observer(){}
// 声明抽象方法
virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0;
string getName(){
return name;
}
void setName(string iName){
this->name = iName;
}
private:
string name;
};
// 具体观察者
class Player :public Observer
{
public:
Player(){
setName("none");
}
Player(string iName){
setName(iName);
}
// 实现
void call(INFO_TYPE infoType, AllyCenter* ac){
switch (infoType){
case RESOURCE:
printf("%s :我这里有物资\n", getName().c_str());
break;
case HELP:
printf("%s :救救我\n", getName().c_str());
break;
default:
printf("Nothing\n");
}
ac->notify(infoType, getName());
}
// 实现具体方法
void help(){
printf("%s:坚持住,我来救你!\n", getName().c_str());
}
void come(){
printf("%s:好的,我来取物资\n", getName().c_str());
}
};
#endif