From a4f2da4a131a94fe914dfb3bcc56d92a8d96371f Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Thu, 21 May 2020 22:19:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E4=BB=8B=E8=80=85=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=20=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- general/src/pattern/adapter.hpp | 32 ++++++++++--------- test/src/patterntest/pattern_test.cpp | 45 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/general/src/pattern/adapter.hpp b/general/src/pattern/adapter.hpp index 46a43aa..7223bc9 100644 --- a/general/src/pattern/adapter.hpp +++ b/general/src/pattern/adapter.hpp @@ -39,36 +39,36 @@ private: ResponsabilityChan *mHead; ResponsabilityChan *mTail; }; +template class Mediator; - template class MediatorColleague{ public: - MediatorColleague(std::string name,Mediator *p){ - mName = name; - mMediater = p; - } std::string GetName(){ return mName; }; - int SetMediater(Mediator* p){ + int SetMediater(Mediator* p){ mMediater = p; } - virtual int Send(std::string colleage,T* data){ + virtual int Send(std::string colleage,T data)=0; + virtual int Recv(std::string colleage,T data)=0; +protected: + std::string mName; + Mediator *mMediater; + MediatorColleague(std::string name,Mediator *p){ + mName = name; + mMediater = p; } private: MediatorColleague(){ - } - std::string mName; - Mediator *mMediater; }; - +template class Mediator{ public: - virtual int Register(MediatorColleague *p){ // 注册 + int Register(MediatorColleague *p){ // 注册 if (nullptr == p){ return -1; } @@ -79,11 +79,15 @@ public: p->SetMediater(this); return -1; } - virtual int Relay(MediatorColleague *p){ //转发 + int Relay(std::string colleage,T data){ //转发 + if(mColleague.find(colleage) != mColleague.end()){ + MediatorColleague *recvcolleage = mColleague.find(colleage)->second; + recvcolleage->Recv("",data); + } return -1; } private: - std::map mColleague; + std::map*> mColleague; }; #endif //GENERAL_ADAPTER_H diff --git a/test/src/patterntest/pattern_test.cpp b/test/src/patterntest/pattern_test.cpp index cac3609..79920be 100644 --- a/test/src/patterntest/pattern_test.cpp +++ b/test/src/patterntest/pattern_test.cpp @@ -36,6 +36,9 @@ private: } }; +/* + 显示调用 + */ using namespace std; class Product :public CloneAble{ public: @@ -62,7 +65,49 @@ private: int mAttribute1; char mAttribute2; }; +/* + * mediater test +*/ +class HandleData{ +public: + std::string data; +}; +class Motherboard :public Mediator{ +}; +class RAM :public MediatorColleague{ +public: + RAM(std::string name,Mediator *p) : MediatorColleague(name,p){ + } + int Send(std::string colleage,HandleData data){ + this->mMediater->Relay(colleage,data); + } + int Recv(std::string colleage,HandleData data){ + std::cout<mName<<" receive data "<{ +public: + SoundCard(std::string name,Mediator *p) : MediatorColleague(name,p){ + } + int Send(std::string colleage,HandleData data){ + this->mMediater->Relay(colleage,data); + } + int Recv(std::string colleage,HandleData data){ + std::cout<mName<<" receive data "<*) &soundcard); + mon.Register((MediatorColleague*) &ram); + soundcard.Send("ram",HandleData{"ss"}); + BridgeTest br(new MP3Player); BridgeTest br1(new MP4Player);