Mediator pattern (Mediator): Use a mediator object to encapsulate a series of object interactions. Mediators eliminate the need for objects to explicitly reference each other, thereby loosening coupling and allowing them to independently change their interactions.
Four roles:
Abstract mediator Mediator
Concrete mediator object ConcreteMediator
Abstract class Abstract
Concrete abstract class ConcreteAbstract
Mediator pattern advantages:
The emergence of Mediator reduces the coupling of each Abstract, making it possible to independently change and reuse each Abstract class and Mediator.
Because we abstract how objects collaborate, we treat mediation as an independent concept and encapsulate it in an object. In this way, the focus of attention shifts from the behavior of the objects themselves to the interaction between them. , that is, looking at the system from a more objective perspective.
Disadvantages of the mediator model:
Since ConcreteMediator controls centralization, the complexity of the interaction becomes the complexity of the mediator, which makes the mediator become more complex than any ConcreteAbstract All responsible.
Pattern implementation:
[code]//前向声明 class Abstract; //中介者抽象类 class Mediator{ public: virtual void Send(std::string message, Abstract *abstract){} }; //抽象类 class Abstract{ protected: Mediator *mediator; public: //构造中介者 Abstract(Mediator *mediator){ this->mediator = mediator; } }; //具体抽象类1 class ConcreteAbstract1: public Abstract{ public: ConcreteAbstract1(Mediator *me):Abstract(me){} void Send(std::string message){ //中介者送出去 mediator->Send(message, this); } void Notify(std::string message){ std::cout << "ConcreteAbstract1 receive message: " << message << " ---(From ConcreteAbstract2)" << std::endl; } }; //具体抽象类2 class ConcreteAbstract2: public Abstract{ public: ConcreteAbstract2(Mediator *me):Abstract(me){} void Send(std::string message){ //中介者送出去 mediator->Send(message, this); } void Notify(std::string message){ std::cout << "ConcreteAbstract2 receive message: " << message << " ---(From ConcreteAbstract1)" << std::endl; } }; //具体中介者 class ConcreteMediator: public Mediator{ private: //需了解所存的具体抽象类对象 ConcreteAbstract1 *CA1; ConcreteAbstract2 *CA2; public: ConcreteMediator(){} void setConcreteMediator(ConcreteAbstract1 *CA1, ConcreteAbstract2 *CA2){ this->CA1 = CA1; this->CA2 = CA2; } virtual void Send(std::string message, Abstract *abstract){ if(abstract == CA1) CA2->Notify(message); else CA1->Notify(message); } };
Client:
[code]//Client int main(){ ConcreteMediator *m = new ConcreteMediator; //具体的抽象这认识具体的中介者 ConcreteAbstract1 *ca1 = new ConcreteAbstract1(m); ConcreteAbstract2 *ca2 = new ConcreteAbstract2(m); //让中介者认识两个具体的抽象类 m->setConcreteMediator(ca1, ca2); ca1->Send("Hello"); //Output: ConcreteAbstract2 receive message: Hello ---(From ConcreteAbstract1) ca2->Send("Hi"); //Output: ConcreteAbstract1 receive message: Hi ---(From ConcreteAbstract2) if(m != NULL){ delete m; m = NULL; } if(ca1 != NULL){ delete ca1; ca1 = NULL; } if(ca2 != NULL){ delete ca2; ca2 = NULL; } return 0; }
The above is the content of the C++ design pattern brief understanding of the intermediary pattern. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!