#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//迪米特法则:最少知识原则
class AbstractBuilding
{
public:
virtual void sale() = 0;
virtual string getQuality() = 0;
};
class BuildingA : public AbstractBuilding
{
public:
BuildingA()
{
this->mQulity = "高品质";
}
virtual void sale()
{
cout << "楼盘A" << mQulity<<"被售卖"<<endl;
}
virtual string getQuality()
{
return mQulity;
}
public:
string mQulity;
};
class BuildingB : public AbstractBuilding
{
public:
BuildingB()
{
this->mQulity = "低品质";
}
virtual void sale()
{
cout << "楼盘B" << mQulity << "被售卖" << endl;
}
virtual string getQuality()
{
return mQulity;
}
public:
string mQulity;
};
// 提升 创建中介类
class Mediator
{
public:
Mediator()
{
AbstractBuilding* building = new BuildingA;
vBuild.push_back(building);
building = new BuildingB;
vBuild.push_back(building);
}
~Mediator()
{
for (auto it = vBuild.begin(); it != vBuild.end(); ++it)
{
if (*it != NULL)
delete *it;
}
}
//对外提供一个接口
AbstractBuilding* findMyBuilding(string quality)
{
for (auto it = vBuild.begin(); it != vBuild.end(); ++it)
{
if ((*it)->getQuality() == quality)
return *it;
}
return NULL;
}
public:
vector<AbstractBuilding*> vBuild;
};
void test()
{
BuildingA* ba = new BuildingA();
if (ba->mQulity == "低品质")
{
ba->sale();
}
BuildingB* bb = new BuildingB();
if (bb->mQulity == "低品质")
{
bb->sale();
}
}
void test03()
{
Mediator* mediator = new Mediator;
AbstractBuilding* building = mediator->findMyBuilding("高品质");
if (building != NULL)
building->sale();
else
cout << "没有符合您要求的楼盘" << endl;
}
int main()
{
//test();
test03();
system("pause");
return 0;
}