基本概念
具体案例
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include <crtdbg.h>
#include<unordered_map>
using namespace std;
class Country;
//联合国机构
class UniteNations
{
public:
UniteNations();
~UniteNations();
virtual void Declare(string message, Country* colleague)=0;
private:
};
//国家
class Country
{
public:
Country();
~Country();
Country(UniteNations* mediator) {
this->mediator = mediator;
}
protected:
UniteNations* mediator;
private:
};
//米国
class USA:public Country
{
public:
USA();
~USA();
USA(UniteNations* mediator) {
this->mediator = mediator;
}
void Declare(string message) {
mediator->Declare(message, this);
}
void GetMessage(string message) {
cout << "米国获得消息,即将攻打日本:" << message << endl;
}
private:
};
class Iraq:public Country
{
public:
Iraq();
~Iraq();
Iraq(UniteNations* mediator) {
this->mediator = mediator;
}
void Declare(string message) {
mediator->Declare(message, this);
}
void GetMessage(string message) {
cout << "伊拉克获得对方消息,米国要打他:" << message << endl;
}
private:
};
// 联合国安全理事会
class UnitedNationSecurityCouncil:public UniteNations
{
public:
UnitedNationSecurityCouncil();
~UnitedNationSecurityCouncil();
//米国
void setColleague1(USA* coll) {
colleague1 = coll;
}
void setColleague2(Iraq* coll) {
colleague2 = coll;
}
USA* getColleague1(){
return colleague1;
}
Iraq* getColleague2(){
return colleague2;
}
void Declare(string message, Country* colleague) {
if (colleague == colleague1) {
colleague2->GetMessage(message);
}
else {
colleague1->GetMessage(message);
}
}
private:
USA* colleague1;
Iraq* colleague2;
};
UnitedNationSecurityCouncil::UnitedNationSecurityCouncil()
{
}
UnitedNationSecurityCouncil::~UnitedNationSecurityCouncil()
{
}
Iraq::Iraq()
{
}
Iraq::~Iraq()
{
}
USA::USA()
{
}
USA::~USA()
{
}
Country::Country()
{
}
Country::~Country()
{
}
UniteNations::UniteNations()
{
}
UniteNations::~UniteNations()
{
}
int main(int argc, char** argv)
{
UnitedNationSecurityCouncil* UNSC = new UnitedNationSecurityCouncil();
USA* c1 = new USA(UNSC);
Iraq* c2 = new Iraq(UNSC);
UNSC->setColleague1(c1);
UNSC->setColleague2(c2);
c1->Declare("打的好,打的棒棒哦!");
c2->Declare("弄两颗原子弹过来岛国");
return 0;
}