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);