-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediator.cpp
More file actions
87 lines (80 loc) · 2.32 KB
/
Copy pathmediator.cpp
File metadata and controls
87 lines (80 loc) · 2.32 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
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
/*中介对象来封装一系列对象的交互,使得对象可以不用显式的引用,可以独立的改变对象的交互
联合国的出现减少了各国之间的有何,使得可以独立的更改和复用各个国家和联合国
一半用于一组对象定义良好,但是复杂的方式进行通信的场合*/
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
class country;
class uniteNation
{
public:
virtual void declare(string message,country *colleague) = 0;
virtual ~uniteNation(){};
};
class country
{
protected:
uniteNation *mediator;
public:
country(uniteNation *med):mediator(med){};
virtual ~country() = 0;//这个需要给出定义的
};
country::~country(){}
class USA : public country
{
public:
USA(uniteNation *med):country(med){};
virtual void declare(string message)
{
mediator->declare(message,this);
}
void getMessage(string message)
{
cout<<"美国获得对方信息:"<<message<<endl;
}
virtual ~USA(){};
};
class iraq : public country
{
public:
iraq(uniteNation *med):country(med){};
virtual void declare(string message)
{
mediator->declare(message,this);
}
void getMessage(string message)
{
cout<<"伊拉克获得对方信息:"<<message<<endl;
}
virtual ~iraq(){};
};
class uniteNationsSecurityCouncil : public uniteNation
{
public://好设置 一般需要是pravite
USA *colleague1;
iraq *colleague2;
public:
virtual void declare(string message,country *colleague)
{
if(colleague1 == colleague)
colleague2->getMessage(message);
else
colleague1->getMessage(message);
}
};
int main()
{
uniteNationsSecurityCouncil *UNSC = new uniteNationsSecurityCouncil();
USA *c1 = new USA(UNSC);
iraq *c2 = new iraq(UNSC);
UNSC->colleague1 = c1;
UNSC->colleague2 = c2;
c1->declare("不准研制核武器,否则发动战争");
c2->declare("我们没有核武器,也不怕侵略");
delete UNSC;
delete c1;
delete c2;
return 0;
}