diff --git a/18.MediatorPattern/1.Picture/中介者举例图.png b/18.MediatorPattern/1.Picture/中介者举例图.png new file mode 100644 index 0000000..ebfebe4 Binary files /dev/null and b/18.MediatorPattern/1.Picture/中介者举例图.png differ diff --git a/18.MediatorPattern/1.Picture/中介者模式.jpg b/18.MediatorPattern/1.Picture/中介者模式.jpg new file mode 100644 index 0000000..8308b6b Binary files /dev/null and b/18.MediatorPattern/1.Picture/中介者模式.jpg differ diff --git a/18.MediatorPattern/1.Picture/中介者模式UML图.png b/18.MediatorPattern/1.Picture/中介者模式UML图.png new file mode 100644 index 0000000..3681213 Binary files /dev/null and b/18.MediatorPattern/1.Picture/中介者模式UML图.png differ diff --git a/18.MediatorPattern/1.Picture/中介者模式实例UML图.png b/18.MediatorPattern/1.Picture/中介者模式实例UML图.png new file mode 100644 index 0000000..6786f16 Binary files /dev/null and b/18.MediatorPattern/1.Picture/中介者模式实例UML图.png differ diff --git a/18.MediatorPattern/2.Code/Colleague.h b/18.MediatorPattern/2.Code/Colleague.h new file mode 100644 index 0000000..cfe231e --- /dev/null +++ b/18.MediatorPattern/2.Code/Colleague.h @@ -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__ \ No newline at end of file diff --git a/18.MediatorPattern/2.Code/Landlord.cpp b/18.MediatorPattern/2.Code/Landlord.cpp new file mode 100644 index 0000000..dd6f2a6 --- /dev/null +++ b/18.MediatorPattern/2.Code/Landlord.cpp @@ -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); +} \ No newline at end of file diff --git a/18.MediatorPattern/2.Code/Mediator.h b/18.MediatorPattern/2.Code/Mediator.h new file mode 100644 index 0000000..05c1461 --- /dev/null +++ b/18.MediatorPattern/2.Code/Mediator.h @@ -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: + vectorlandlordList; + vectortenantList; +}; + + +#endif //__MEDIATOR_PATTERN_H__ \ No newline at end of file diff --git a/18.MediatorPattern/2.Code/Tenant.cpp b/18.MediatorPattern/2.Code/Tenant.cpp new file mode 100644 index 0000000..c27ca84 --- /dev/null +++ b/18.MediatorPattern/2.Code/Tenant.cpp @@ -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()); +} \ No newline at end of file diff --git a/18.MediatorPattern/2.Code/common.h b/18.MediatorPattern/2.Code/common.h new file mode 100644 index 0000000..0662f31 --- /dev/null +++ b/18.MediatorPattern/2.Code/common.h @@ -0,0 +1,16 @@ +#ifndef __COMMON_H__ +#define __COMMON_H__ + +// ͷļ + +#include +using namespace std; + +enum PERSON_TYPE +{ + NONE_PERSON, + LANDLORD, + TENANT +}; + +#endif //__COMMON_H__ \ No newline at end of file diff --git a/18.MediatorPattern/2.Code/main.cpp b/18.MediatorPattern/2.Code/main.cpp new file mode 100644 index 0000000..702346d --- /dev/null +++ b/18.MediatorPattern/2.Code/main.cpp @@ -0,0 +1,38 @@ +#include +#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; +} \ No newline at end of file diff --git a/README.md b/README.md index 08ce255..fff718c 100644 --- a/README.md +++ b/README.md @@ -81,4 +81,8 @@ Jungle设计模式系列 20.设计模式(二十)——迭代器模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102879383 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102879383 + +21.设计模式(二十一)——中介者模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102885567 \ No newline at end of file