Mediator Mode (c + + implementation)

Source: Internet
Author: User
Tags getmessage

The main reference is "Big talk design mode" and "design pattern: The basis of reusable object-oriented software" two books. This article describes the implementation of the mediator pattern.

Broker mode: What it is:define an object, that encapsulates how a set of objects interact. Promotes loose coupling by keeping objects from referring to all other explicitly and it lets you vary their interactions independently.

Encapsulates a series of object interactions with a mediation object. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them. There are many examples of intermediaries, from the United Nations Security Council to housing intermediaries, who have played a central role in coordinating the interests of all parties.

The broker pattern is easy to apply in the system and is easily misused in the system. When there are many pairs of interactive complex object groups, do not rush to use the mediator mode, but first to reflect on the design of your system is not reasonable.    

Advantages:The advent of mediator reduces the coupling of each colleague, allowing the individual colleague classes and mediator to be independently changed and reused, and the abstraction of how objects work together, The mediation acts as a separate concept and encapsulates it in an object so that the object of interest shifts from the behavior of the objects themselves to the interaction between them, that is, to look at the system in a more macroscopic perspective.

Disadvantage: Due to the Concretemediator control of centralization. So the complexity of the interaction becomes the complexity of the mediator, which makes the intermediary more complex than any one concretecolleague.

This article to rent for example, if there is no housing intermediary, then tenants to find their own landlord, and the landlord also to find tenants, very inconvenient. With the housing agency is convenient, the landlord can be rented housing information to the intermediary agencies, and tenants can go to the intermediary agency consultation. in software, there is a need for communication between multiple objects, if there is no intermediary, the object needs to know other objects, in the worst case, you may need to know all the other objects, and with the mediation object is more convenient, the object only need to communicate with the mediation object, not to know other objects. This is the intermediary mode, the following take the rental as an example, to give a UML diagram of the intermediary mode.

C + + code implementation Mediator mode:

1#include <iostream>2#include <string>3 using namespacestd;4 5 /*declaring abstract mediation classes*/6 classMediator;7 8 /*Defining abstract Humans*/9 class PersonTen { One  Public: A     Virtual voidSetmediator (Mediator *mediator) {}  -     Virtual voidSendMessage (stringmessage) {} -     Virtual voidGetMessage (stringmessage) {} the protected: -Mediator *_mediator;//Intermediary - }; -  + /*defining abstract Mediation Classes*/ - classMediator + { A  Public: at     Virtual voidSend (stringMessage, Person *Person ) {} -     Virtual voidSetrenter (Person *renter) {} -     Virtual voidSetlandlord (Person *landlord) {} -      - }; -  in /*Define Tenant class*/ - classRenter: Public Person to { +  Public: -     voidSetmediator (Mediator *mediator) {_mediator =Mediator;} the     voidSendMessage (stringMessage) {_mediator->send (message), This);} *     voidGetMessage (stringMessage) {cout<<"the tenant received a message from the landlord:"<<message;} $ };Panax Notoginseng  - /*defining the Landlord class*/ the classLandlord: Public Person + { A  Public: the     voidSetmediator (Mediator *mediator) {_mediator =Mediator;} +     voidSendMessage (stringMessage) {_mediator->send (message), This);} -     voidGetMessage (stringMessage) {cout<<"the landlord received a message from the tenant:"<<message;} $ }; $  - /*defining a specific mediation class*/ - classHousemediator: PublicMediator the { -  Public:Wuyi housemediator (): _renter (null), _landlord (null) {} the     voidSetrenter (person *renter) {_renter =renter;} -     voidSetlandlord (person *landlord) {_landlord =landlord;} Wu     voidSend (stringMessage, Person *Person ) -{//The object that receives the message is the corresponding object of the object About         if(Person = =_renter) $_landlord->GetMessage (message); -         Else -_renter->GetMessage (message); -     } A Private: +Person *_renter; thePerson *_landlord; - }; $ intMainintargcChar*argv[]) the {   theMediator *mediator =Newhousemediator (); thePerson *person1 =Newrenter (); thePerson *person2 =Newlandlord (); -Mediator->Setrenter (person1); inMediator->Setlandlord (person2); thePerson1->Setmediator (mediator); thePerson2->Setmediator (mediator); AboutPerson1->sendmessage ("I want to rent a house near Shenzhen North Station, a room and a hall \ n"); thePerson2->sendmessage ("I rent a house, a room and a hall, near Shenzhen North station \ n"); the     DeletePerson1; the     DeletePerson2; +     DeleteMediator; -     return 0;  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.