Wzorzec projektowy mediator

Autor podstrony: Krzysztof Zajączkowski

Stronę tą wyświetlono już: 3914 razy

Opis wzorca projektowego mediator

Wzorzec projektory mediator należy do czynnościowych wzorców projektowych. Jego celem jest udostępnienie interfejsu pośredniczącego w przekazywaniu informacji pomiędzy różnymi nie połączonymi bezpośrednio z sobą obiektami klas, które dziedziczą po wspólnym interfejsie. Klasa pośrednicząca zawiera tablicę interfejsów klas.

Przykład diagramu UML wzorca projektowego mediator

Na poniższym diagramie UML widoczna jest klasa Mediator, która dziedziczy po interfejsie iMediator. Klasa ta zawiera tablicę asocjacyjną umożliwiającą przechowywanie pary danych klucz (std::string) → wartość (iMessage*). Wysłanie wiadomości odbywa się za pomocą metody tej klasy sendMessage zaś dodawanie nowych interfejsów odbywa się za pomocą metody registerObject.

Klasy User i Bot dziedziczą po wspólnym interfejsie iMessage. W moim przypadku dodatkowo zamieściłem wewnątrz interfejsu iMessage pole zawierające wskaźnik na interfejs iMediator. Dzięki temu obiekt klasy sam siebie próbuje zarejestrować oraz (w przypadku obiektów klasy bot) wysyłać automatycznie odpowiedzi.

Przykładowy diagram UML wzorca projektowego mediator
Rys. 1
Przykładowy diagram UML wzorca projektowego mediator

Przykładowa implementacja wzorca projektowego mediator w C++

#include <iostream> #include <string> #include <map> class iMediator; class iMessage{ protected: std::string name; iMediator* mediator; bool registerUser(); public: iMessage(std::string name, iMediator* mediator) : name(name), mediator(mediator){ registerUser(); } virtual void getMessage(std::string from, std::string message) = 0; }; class iMediator{ public: virtual bool registerObject(std::string name, iMessage* msg) = 0; virtual void sendMessage(std::string to, std::string from, std::string message) = 0; }; bool iMessage::registerUser(){ return mediator->registerObject(name, this); } class Mediator : public iMediator{ std::map<std::string, iMessage*> registeredObjects; public: void sendMessage(std::string to, std::string from, std::string message){ std::map<std::string, iMessage*>::iterator iTo= registeredObjects.find(to); if( iTo != registeredObjects.end() ){ iTo->second->getMessage(from, message); }else{ std::cout << to << " not exist!!!" << std::endl; } } bool registerObject(std::string name, iMessage* msg){ if(msg){ std::map<std::string, iMessage*>::iterator iName = registeredObjects.find(name); if(iName == registeredObjects.end()){ std::cout << name << " is registered" << std::endl; registeredObjects[name] = msg; return true; } std::cout << name << " is already registered!!!" << std::endl; return false; } } }; class User : public iMessage{ public: User(std::string name, iMediator* mediator) : iMessage(name, mediator){}; virtual void getMessage(std::string from, std::string message){ std::cout << name << " get some message from "<< from << std::endl; std::cout << message << std::endl << std::endl; } void sendMessage(){ std::string to; std::cout << "Send to who: "; std::cin >> to; std::string message; std::cin.ignore(1); std::cout << "Message: "; std::getline(std::cin, message); mediator->sendMessage(to, name, message); } }; class Bot : public iMessage{ int age; public: Bot(std::string name, int age, iMediator* mediator) : iMessage(name, mediator), age(age){}; bool registerUser(){ return mediator->registerObject(name, this); } virtual void getMessage(std::string from, std::string message){ std::cout << name << " get some message from "<< from << std::endl; std::cout << message << std::endl << std::endl; if(message == "what's your name"){ mediator->sendMessage(from, name, std::string("My name is ") + name); }else if(message == "how old are you"){ char ageBuffor[50]; ltoa(age, ageBuffor, 10); mediator->sendMessage(from, name, std::string("I'm ") + ageBuffor + " years old"); }else{ mediator->sendMessage(from, name, std::string("I don't understand\nI can ansver only on questions bellow:\n\nwhat's your name\nhow old are you")); } } }; int main(){ Mediator mediator; User yury("Yury", &mediator); Bot r2d2("R2D2", 50, &mediator); int whatToDo = 0; do{ std::cout << "What you want to do:"<< std::endl << std::endl; std::cout << "Send a message [0]" << std::endl; std::cout << "Quit program [not 0]" << std::endl; std::cin >> whatToDo; if(!whatToDo){ yury.sendMessage(); } }while(whatToDo == 0); std::cin.ignore(100); std::cin.get(); return 0; }

Przykładowy wynik działania programu:

Yury is registered
R2D2 is registered
What you want to do:

Send a message [0]
Quit program [not 0]
0
Send to who: R2D2
Message: what's your name
R2D2 get some message from Yury
what's your name

Yury get some message from R2D2
My name is R2D2

What you want to do:

Send a message [0]
Quit program [not 0]
0
Send to who: R2D2
Message: how old are you
R2D2 get some message from Yury
how old are you

Yury get some message from R2D2
I'm 50 years old

Send a message [0]
Quit program [not 0]
0
Send to who: R2D2
Message: what you doing
R2D2 get some message from Yury
what you doing

Yury get some message from R2D2
I don't understand
I can ansver only on questions bellow:

what's your name
how old are you

What you want to do:

Send a message [0]
Quit program [not 0]
Strony powiązane
strony powiązane
  1. sourcemaking.com/design_patterns/mediator - strona opisująca wzorzec projektowy mediator [En]
  2. pl.wikipedia.org - opis tego wzorca na stronie Wikipedii
Propozycje książek