Created
May 23, 2014 09:55
-
-
Save mihids/fef5f0a1cb2dcc2aad67 to your computer and use it in GitHub Desktop.
Mediator_Pattern
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 */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment