//迪米特法则 又叫最小知识原则
//知道的越少,建立的耦合关系越低
# 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;
}
//知道的越少,建立的耦合关系越低
# 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;
}