-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mediator.cpp
80 lines (67 loc) · 1.55 KB
/
Mediator.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
#include <string>
#include <iostream>
using namespace std;
class Mediator;
class Colleague
{
private:
Mediator* m;
public:
void setMediator(Mediator* m){
this->m = m;
}
void send(string msg);
virtual void getMessage(string msg){}
};
class Mediator{
public:
virtual void setColleague(Colleague* c1, Colleague* c2){}
virtual void send(string message, Colleague* colleague){}
};
class ConcreteMediator : public Mediator
{
private:
Colleague* c1;
Colleague* c2;
public:
void setColleague(Colleague* c1, Colleague* c2){
this->c1 = c1;
this->c2 = c2;
}
void send(string message, Colleague* colleague){
if(c1!=nullptr && c1 == colleague){
c2->getMessage(message);
}else if(c2 != nullptr){
c1->getMessage(message);
}else{
cout << "你要发送给谁?" << endl;
}
}
};
void Colleague::send(string msg){
m->send(msg, this);
}
class Manager : public Colleague
{
public:
void getMessage(string msg){
cout << "经理接受到:" << msg << endl;
}
};
class Guard : public Colleague
{
public:
void getMessage(string msg){
cout << "保安接受到:" << msg << endl;
}
};
int main(){
Colleague* c1 = new Manager();
Colleague* c2 = new Guard();
Mediator* m = new ConcreteMediator();
m->setColleague(c1, c2);
c1->setMediator(m);
c2->setMediator(m);
c1->send("吃了吗");
c2->send("没呢");
}