Mediator pattern is used to loosely couple object interaction. If objects are tightly coupled, resuability disappears and maintenance becomes harder. A real life scenario will explain the pattern the best. Think about an airport control tower. It's task is to moderate the flight landings. If many aircrafts arrive simultaneously, only one can land at a time and the rest have to stay out from landing until signaled by the tower. This behavior exactly matches that of the mediator pattern. The aircrafts don't communicate with each other but yet they communicate through the control tower so that they can avoid disaster. The C++ code simulating the landing of planes, helicopters and private jets at an airport is simulated in this code. Assume only one airstrip.
The output:
Mediator Will Take Over Landing
Plane Landing 1
Heli Out of the way... 2
Jet Out of the way... 3
Jet Out of the way... 4
Mediator Will Take Over Landing
Heli Out of the way... 2
Jet Landing 3
Jet Out of the way... 4
Mediator Will Take Over Landing
Heli Out of the way... 2
Jet Landing 4
Mediator Will Take Over Landing
Heli Landing 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "AirCraft.h" | |
#include "Mediator.h" | |
AirCraft::AirCraft(Mediator* med) { | |
med_ = med; | |
} | |
AirCraft::AirCraft(const AirCraft& orig) { | |
} | |
AirCraft::~AirCraft() { | |
} | |
void AirCraft::Land() { | |
med_->Land(this); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef AIRCRAFT_H | |
#define AIRCRAFT_H | |
#include <iostream> | |
using namespace std; | |
class Mediator; | |
class AirCraft { | |
public: | |
AirCraft(Mediator* med); | |
AirCraft(const AirCraft& orig); | |
virtual ~AirCraft(); | |
virtual void Landing() = 0; | |
virtual void StayOut() = 0; | |
virtual void Land(); | |
void SetID(int id_) { | |
id = id_; | |
} | |
int GetID() { | |
return id; | |
} | |
private: | |
int id; | |
Mediator* med_; | |
}; | |
class Plane : public AirCraft { | |
public: | |
Plane(int id_, Mediator* med) : AirCraft(med) { | |
SetID(id_); | |
} | |
virtual ~Plane() { | |
} | |
void Landing() { | |
std::cout << "Plane Landing " << GetID() << std::endl; | |
} | |
void StayOut() { | |
std::cout << "Plane Out of the way... " << GetID() << std::endl; | |
} | |
}; | |
class Heli : public AirCraft { | |
public: | |
Heli(int id_, Mediator* med) : AirCraft(med) { | |
SetID(id_); | |
} | |
virtual ~Heli() { | |
} | |
void Landing() { | |
std::cout << "Heli Landing " << GetID() << std::endl; | |
} | |
void StayOut() { | |
std::cout << "Heli Out of the way... " << GetID() << std::endl; | |
} | |
}; | |
class Jet : public AirCraft { | |
public: | |
Jet(int id_, Mediator* med) : AirCraft(med) { | |
SetID(id_); | |
} | |
virtual ~Jet() { | |
} | |
void Landing() { | |
std::cout << "Jet Landing " << GetID() << std::endl; | |
} | |
void StayOut() { | |
std::cout << "Jet Out of the way... " << GetID() << std::endl; | |
} | |
}; | |
#endif /* AIRCRAFT_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "AirCraft.h" | |
#include "Mediator.h" | |
int main(int argc, char** argv) { | |
Mediator *med = new Mediator(); | |
Plane *p = new Plane(1, med); | |
Heli *h = new Heli(2, med); | |
Jet *j = new Jet(3, med); | |
Jet *j1 = new Jet(4, med); | |
med->RegisterAirCraft(p); | |
med->RegisterAirCraft(h); | |
med->RegisterAirCraft(j); | |
med->RegisterAirCraft(j1); | |
p->Land(); | |
j->Land(); | |
j1->Land(); | |
h->Land(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Mediator.h" | |
Mediator::Mediator() { | |
} | |
Mediator::Mediator(const Mediator& orig) { | |
} | |
Mediator::~Mediator() { | |
} | |
void Mediator::Land(AirCraft *craft) { | |
cout << "Mediator Will Take Over Landing" << endl; | |
list<AirCraft*>::iterator it; | |
for (it = list_ac.begin(); it != list_ac.end(); it++) { | |
if (*it == craft) { | |
(*it)->Landing(); | |
} else { | |
(*it)->StayOut(); | |
} | |
} | |
for (it = list_ac.begin(); it != list_ac.end(); it++) { | |
if (*it == craft) { | |
list_ac.erase(it); | |
break; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MEDIATOR_H | |
#define MEDIATOR_H | |
#include <list> | |
#include <iostream> | |
#include "AirCraft.h" | |
using namespace std; | |
class Mediator { | |
public: | |
Mediator(); | |
Mediator(const Mediator& orig); | |
virtual ~Mediator(); | |
void RegisterAirCraft(AirCraft *craft) { | |
list_ac.push_back(craft); | |
} | |
void Land(AirCraft *craft); | |
private: | |
list <AirCraft*> list_ac; | |
}; | |
#endif /* MEDIATOR_H */ |
Mediator Will Take Over Landing
Plane Landing 1
Heli Out of the way... 2
Jet Out of the way... 3
Jet Out of the way... 4
Mediator Will Take Over Landing
Heli Out of the way... 2
Jet Landing 3
Jet Out of the way... 4
Mediator Will Take Over Landing
Heli Out of the way... 2
Jet Landing 4
Mediator Will Take Over Landing
Heli Landing 2
Comments
Post a Comment