Design Pattern Learning notes--Mediator mode

Source: Internet
Author: User
Tags ming

I. INTRODUCTION
Learn about the legendary mediator model today. When it comes to intermediaries, the easiest thing to think about is the telephone repeater, and here we look at the evolution of the phone. The earliest phone calls were only connected to two users, so the structure was simple and straightforward, and the operation was most convenient. But with the number of people using the telephone, for each of the two phone set up a line is obviously impossible, so there is a telephone repeater, remember the old movies often see inside, call people to pick up the phone, first call the operator, said, "I received xxx that." Yes, the operator is the equivalent of a mediator. In object-oriented design, we often have many classes of objects, objects need to interact with each other, and if there are too many objects, and each object is likely to interact with each other, the simplest way is to save a reference to every other possible interaction object, but there are a few problems, first, there is too much content to save, One copy of each object is obviously redundant, and the coupling between the objects is too high, reaching, if a new class is added, all previous classes will have to be modified. In order to solve this problem, the intermediary model has emerged.Here's a look at the definition of the broker pattern and the UML class diagram:The Mediator pattern (mediator pattern): Encapsulates a series of object interactions with a mediation object (the mediator), which allows the objects to be loosely coupled without having to explicitly refer to each other, and to independently change the interaction between them. The mediator pattern, also known as the mediator pattern, is an object-behavioral pattern.
A simple explanation of the UML class diagram, our original object in the figure is equivalent to colleague colleague class, if not in the intermediary mode, they each specific collegue need to save all other objects of reference, UML diagram will be gray often messy, and through the intermediary mediator, Only the mediator keeps a reference to all the objects themselves, and each individual colleague class only needs to establish a relationship with the mediator to communicate with all other colleague class objects, which greatly reduces the coupling of the system.
Two. Example of a broker pattern
Let's take a look at the phone case, there are intermediaries and no intermediaries.
1. No use of the intermediary mode
Design Pattern.cpp:Defines The entry point for the console application.//#include "stdafx.h" #include <iostream> #include <string> #include <vector>using namespace std;//abstract phone class basephone{public:virtual void call (int num) = 0;virtual void Answer () = 0;};/ /Mobile phone class mobilephone:public basephone{private:vector<basephone*> m_phonevec;string m_Name;public: Mobilephone (string name): M_name (name) {}//add Address Book, or place phone line more appropriately void add (basephone* phone) {m_phonevec.push_back (phone);} Call on the number virtual void calls (int num) override{cout << m_name << "Call with your phone"; M_phonevec[num]->answer ();} Pick up the phone virtual void Answer () override{cout << m_name << "Phone call" << Endl;}; Class Telephone:public basephone{private:vector<basephone*> m_phonevec;string m_Name;public:TelePhone (string Name): M_name (name) {}//add Address Book, or the phone line is more appropriate void add (basephone* phone) {m_phonevec.push_back (phone);} Call on the number virtual void calls (int num) override{cout << m_name << "Use a landline to hit the electricityWords, "; M_phonevec[num]->answer ();//This is directly based on the base class, and if there is other processing, the coupling is greater}//answer call virtual void Answer () override{cout << m_ Name << "Landline calls" << Endl;}; int _tmain (int argc, _tchar* argv[]) {mobilephone* phonea = new Mobilephone ("Xiaoming"); telephone* Phoneb = new Telephone ("Xiao Gang"); mobilephone* Phonec = new Mobilephone ("Xiao Cai");//Establish a connection between each object and each object Phonea->add (phonea);p honea->add (PHONEB); Phonea->add (Phonec);p honeb->add (phonea);p honeb->add (phoneb);p honeb->add (Phonec);p Honec->add ( Phonea);p Honec->add (phoneb);p honec->add (Phonec);p honea->call (1);p Honeb->call (2);p honec->call (0) ; System ("pause"); return 0;}
Result: Xiao Ming uses cell phone to call, small just use a landline to answer the phone
Little just phone with a landline, Xiao Cai on the phone to answer the phone
Xiao Cai calls with his cell phone, Xiao Ming uses his mobile phone to answer the phone
Please press any key to continue ...

2. Use of the mediator mode
We found that each phone was coupled to two other phones, although here I use a base class that can lower some of the coupling, but still feel the code is not elegant enough, we don't want them to be too close to each other, so we add a mediator.
Design Pattern.cpp:Defines The entry point for the console application.//#include "stdafx.h" #include <iostream> #include <string> #include <vector>using namespace Std;class basephone;//Mediator base class Basemediator{public: virtual void call (int num) = 0;virtual void Add (basephone* phone) = 0;};/ /Abstract Phone class basephone{protected:basemediator* m_mediator;public://Now I just need to add a mediator to the void Setmediator (basemediator* Mediator) {m_mediator = mediator;} Send information to other phones via intermediary virtual void call (int num) {m_mediator->call (num);} virtual void Answer () = 0;};/ /electronic Telephone repeater class Electricmediator:public basemediator{private:vector<basephone*> phonevec;public://registered phone void ADD (basephone* phone) override{phonevec.push_back (phone);} Dial virtual void call (int num) override{phonevec[num]->answer ();}};/ /Mobile phone class Mobilephone:public basephone{private:string m_name;public:mobilephone (string name): M_name (name) {}// Call on the number virtual-void call (int num) override{cout << m_name << "phone on mobile"; BasephOne::call (num);} Pick up the phone virtual void Answer () override{cout << m_name << "Phone call" << Endl;}; Class Telephone:public basephone{private:string M_name;public:telephone (string name): M_name (name) {}// Call from virtual void, call (int num) override{cout << m_name << "phone on a landline"; Basephone::call (num);} Answer phone: virtual void Answer () override{cout << m_name << "Landline calls" << Endl;}; int _tmain (int argc, _tchar* argv[]) {mobilephone* phonea = new Mobilephone ("Xiaoming"); telephone* Phoneb = new Telephone ("Xiao Gang"); mobilephone* Phonec = new Mobilephone ("Xiao Cai"); basemediator* Mediator = new Electricmediator ();//Register the Telephone Mediator->add (Phonea) with the intermediary; Mediator->add (PhoneB); Mediator->add (Phonec);//Phone only save the reference of the intermediary can be phonea->setmediator (mediator);p honeb->setmediator (mediator); Phonec->setmediator (mediator);p Honea->call (1);p Honeb->call (2);p honec->call (0); system ("pause"); return 0;}
Result: Xiao Ming uses cell phone to call, small just use a landline to answer the phone
Little just phone with a landline, Xiao Cai on the phone to answer the phone
Xiao Cai calls with his cell phone, Xiao Ming uses his mobile phone to answer the phone
Please press any key to continue ...

Three. Summary of the broker model
Finally, let's look at the pros, cons, and timing of the broker pattern:
advantages;
1) To remove the original colleague object many-to-many interaction, with the intermediary and the object of a one-to-many relationship instead of the original mesh structure was replaced by the star structure. 2) The mediator mode relieves the coupling between complex objects, and the new object does not need to change the original code to conform to the open-close principle.
Disadvantages:The mediator mode places the interaction details of a large number of objects in the intermediary, which is too heavy and difficult to maintain.
Use time:When the reference relationship between objects in our system is meshed, the objects need to be referenced to each other, and each object needs to refer to other objects, we can consider using the mediator pattern to reconstruct the system.

Design Pattern Learning notes--Mediator mode

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.