Design pattern--the Mediator mode C + + implementation

Source: Internet
Author: User

Broker Mode C + + implementation 1 definition

Encapsulates a series of object interactions with a mediation object that the mediator does not need to display for interaction, which makes it loosely coupled and can independently change the interaction between them

2 class Diagram

Composition description

Mediator abstract Mediator role, defining a unified interface for communication between individual colleague roles

Concretemediator the role of a specific mediator, by coordinating each colleague's role to achieve collaborative behavior, relying on each colleague

Colleague colleague role, each colleague role should know the existence of intermediary, and communication between colleagues must be through intermediary collaboration; colleague behavior: Spontaneous behavior, dependent behavior.

3 implementation

Class Mediator

{

Protected

Mediator ();

Public

Virtual ~mediator () = 0;

Protected

ConcreteColleague1 C1;

ConcreteColleague2 C2;

Public

ConcreteColleague1 *GETC1 ()

{

return C1;

}

ConcreteColleague2 *GETC2 ()

{

return C2;

}

virtual void doSomething1 () = 0;

virtual void doSomething2 () = 0;

};

Intermediary person

Class Concretemediator:public Mediator

{

Public

void DoSomething1 ()

{}

void DoSomething2 ()

{}

};

Class colleague

{

Public

Colleague (mediator me)

: Mediator (Me)

{}

Public

Virtual ~colleague () = 0;

Protected

Mediator mediator;

};

Class Concretecolleague1:public Colleague

{

Public

ConcreteColleague1 (Mediator me)

: Colleague (Me)

{}

void Self_method1 ()

{}

Dependency method

void DepMethod1 ()

{

Deal with your business

The business you can't handle is handed over to the intermediary for communication collaboration

Mediator.dosomething2 ();

}

};

Class Concretecolleague2:public Colleague

{

Public

ConcreteColleague2 (Mediator me)

: Colleague (Me)

{}

void Self_method2 ()

{}

Dependency method

void DepMethod2 ()

{

Deal with your business

The business you can't handle is handed over to the intermediary for communication collaboration

Mediator.dosomething2 ();

}

};

Note: Colleague classes use constructors to inject intermediaries, and intermediaries use Setter/getter to inject colleague classes, because peer classes must have intermediaries, and intermediaries have only part of their colleagues class

4 applications

① Advantages

Reduce inter-class dependencies and turn one-to-many dependencies into one-to-one. Colleague classes rely only on mediations, reducing inter-class coupling

② shortcomings, intermediaries will increase with the number of colleagues, logic more and more complex

5 Usage Scenarios

Simple but not easy to use a pattern. Grasp a guideline: When a spider web appears in a class diagram, try to use a mediator

Practical application: Airport dispatch Center, C (Controller) in MVC framework, Media Gateway, intermediary service.

6 Tips-Best Use

N dependencies between objects (N>2)

Multiple objects have dependencies, but are uncertain or may change, using intermediaries to mitigate risk spread

Product development: EG MVC framework. Applying the intermediary model to the product can improve product performance and scalability, but not necessarily for the project. The project is to pay for production as the goal, and products are stable, efficient expansion for the purpose.

Design pattern--the Mediator mode C + + implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.