C 设计模式-迪米特法则

#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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值