迪米特法则

//迪米特法则 又叫最小知识原则
//知道的越少,建立的耦合关系越低
# include <iostream>
# include <string>
# include <vector>


using namespace std;


class AbstractBuliding
{
public:
virtual void sale() = 0;
virtual string getQuality() = 0;
};


//楼盘A
class BulidingA : public AbstractBuliding
{
public:
BulidingA()
{
this->mQulity = "高品质";
}
virtual void sale()
{
cout << "楼盘A" << this->mQulity << "被售卖" << endl;
}
virtual string getQuality()
{
return mQulity;
}
public:
string mQulity;
};


//楼盘B
class BulidingB : public AbstractBuliding
{
public:
BulidingB()
{
this->mQulity = "低品质";
}
virtual void sale()
{
cout << "楼盘B" << this->mQulity << "被售卖" << endl;
}
virtual string getQuality()
{
return mQulity;
}
public:
string mQulity;
};


//客户端
void test01()
{
BulidingA *ba = new BulidingA;
if(ba->mQulity == "高品质")
{
ba->sale();
}


BulidingB *bb = new BulidingB;
if(bb->mQulity == "低品质")
{
bb->sale();
}
}


//中介类
class Mediator
{
public:
Mediator()
{
AbstractBuliding* buliding = new BulidingA;
vBuliding.push_back(buliding);

   buliding = new BulidingB;
vBuliding.push_back(buliding);
}


//对外提供接口
AbstractBuliding* findMyBuliding(string quality)
{
for(vector<AbstractBuliding*>::iterator it = vBuliding.begin(); it != vBuliding.end(); it++)
{
if((*it)->getQuality() == quality)
{
return *it;
}
}
return NULL;
}


~Mediator()
{
for(vector<AbstractBuliding*>::iterator it = vBuliding.begin(); it != vBuliding.end(); it++)
{
if(*it != NULL)
{
delete *it;
}
}
}
public:
vector<AbstractBuliding *> vBuliding;
};


void test02()
{
Mediator* mediator = new Mediator;


AbstractBuliding* buliding = mediator->findMyBuliding("sss");
if(buliding != NULL)
{
buliding->sale();
}
else
{
cout << "没有符合您条件的楼盘!" << endl;
}
}


int main(int argc, char *argv[])
{
// test01(); 
test02();
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值