add Mediator Pattern

master
FengJungle 2019-11-04 07:46:24 +08:00
parent 16faa3a386
commit ea451138ef
11 changed files with 228 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -0,0 +1,62 @@
#ifndef __COLLEAGUE_H__
#define __COLLEAGUE_H__
#include "common.h"
using namespace std;
// 前向声明
class Mediator;
class Agency;
// 抽象同事类
class Colleague
{
public:
Colleague(){}
void setMediator(Mediator* iMediator){
this->mediator = iMediator;
}
Mediator* getMediator(){
return this->mediator;
}
void setPersonType(PERSON_TYPE iPersonType){
this->personType = iPersonType;
}
PERSON_TYPE getPersonType(){
return this->personType;
}
virtual void ask() = 0;
virtual void answer() = 0;
private:
PERSON_TYPE personType;
Mediator* mediator;
};
// 具体同事类:房东
class Landlord :public Colleague
{
public:
Landlord();
Landlord(string iName, int iPrice, string iAddress, string iPhoneNum);
void ask();
void answer();
private:
string name;
int price;
string address;
string phoneNumber;
};
// 具体同事类:租客
class Tenant :public Colleague
{
public:
Tenant();
Tenant(string name);
void ask();
void answer();
private:
string name;
};
#endif //__COLLEAGUE_H__

View File

@ -0,0 +1,29 @@
#include "Colleague.h"
#include "Mediator.h"
Landlord::Landlord(){
name = "none";
price = 0;
address = "none";
phoneNumber = "none";
setPersonType(NONE_PERSON);
}
Landlord::Landlord(string iName, int iPrice,
string iAddress, string iPhoneNum){
name = iName;
price = iPrice;
address = iAddress;
phoneNumber = iPhoneNum;
setPersonType(LANDLORD);
}
void Landlord::answer(){
printf("房东姓名:%s, 房租:%d, 地址:%s, 联系电话:%s\n",
name.c_str(), price, address.c_str(), phoneNumber.c_str());
}
void Landlord::ask(){
printf("房东%s查看租客信息\n",name.c_str());
(this->getMediator())->operation(this);
}

View File

@ -0,0 +1,57 @@
#ifndef __MEDIATOR_PATTERN_H__
#define __MEDIATOR_PATTERN_H__
#include "common.h"
#include "Colleague.h"
// 抽象中介者
class Mediator
{
public:
Mediator(){}
// 声明抽象方法
virtual void operation(Colleague*) = 0;
// 声明注册方法
virtual void registerMethod(Colleague*) = 0;
};
// 具体中介者
class Agency:public Mediator
{
public:
Agency(){}
void registerMethod(Colleague* person){
switch (person->getPersonType()){
case LANDLORD:
landlordList.push_back((Landlord*)person);
break;
case TENANT:
tenantList.push_back((Tenant*)person);
break;
default:
printf("wrong person\n");
}
}
void operation(Colleague* person){
switch (person->getPersonType()){
case LANDLORD:
for (int i = 0; i < tenantList.size(); i++){
tenantList[i]->answer();
}
break;
case TENANT:
for (int i = 0; i < landlordList.size(); i++){
landlordList[i]->answer();
}
break;
default:
break;
}
}
private:
vector<Landlord*>landlordList;
vector<Tenant*>tenantList;
};
#endif //__MEDIATOR_PATTERN_H__

View File

@ -0,0 +1,21 @@
#include "Colleague.h"
#include "Mediator.h"
Tenant::Tenant(){
name = "none";
setPersonType(NONE_PERSON);
}
Tenant::Tenant(string iName){
name = iName;
setPersonType(TENANT);
}
void Tenant::ask(){
printf("租客%s询问房东信息\n", name.c_str());
(this->getMediator())->operation(this);
}
void Tenant::answer(){
printf("租客姓名:%s\n", name.c_str());
}

View File

@ -0,0 +1,16 @@
#ifndef __COMMON_H__
#define __COMMON_H__
// 公共头文件
#include <vector>
using namespace std;
enum PERSON_TYPE
{
NONE_PERSON,
LANDLORD,
TENANT
};
#endif //__COMMON_H__

View File

@ -0,0 +1,38 @@
#include <iostream>
#include "Mediator.h"
#include "Colleague.h"
int main()
{
// 创建租房中介
Agency *mediator = new Agency();
// 创建3位房东
Landlord *fangdong1 = new Landlord("刘备", 1350, "成都市双流区", "1351025");
Landlord *fangdong2 = new Landlord("关羽", 1500, "成都市武侯区", "1378390");
Landlord *fangdong3 = new Landlord("张飞", 1000, "成都市龙泉驿", "1881166");
fangdong1->setMediator(mediator);
fangdong2->setMediator(mediator);
fangdong3->setMediator(mediator);
// 房东在中介处登记注册房源信息
mediator->registerMethod(fangdong1);
mediator->registerMethod(fangdong2);
mediator->registerMethod(fangdong3);
// 创建两位租客Jungle和贱萌兔
Tenant *jungle = new Tenant("Jungle");
Tenant *jianmengtu = new Tenant("贱萌兔");
jungle->setMediator(mediator);
jianmengtu->setMediator(mediator);
// Jungle和贱萌兔在中介处登记求租信息
mediator->registerMethod(jungle);
mediator->registerMethod(jianmengtu);
jungle->ask();
printf("\n\n");
fangdong1->ask();
printf("\n\n");
system("pause");
return 0;
}

View File

@ -81,4 +81,8 @@ Jungle设计模式系列
20.设计模式(二十)——迭代器模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102879383
博客地址https://blog.csdn.net/sinat_21107433/article/details/102879383
21.设计模式(二十一)——中介者模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102885567