Wzorzec projektowy mediator
Stronę tą wyświetlono już: 2852 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ł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]

Tytuł:
React 17. Wzorce projektowe i najlepsze praktyki. Projektowanie i rozwijanie nowoczesnych aplikacji internetowych. Wydanie III
Autor:
Carlos Santana Roldán

Tytuł:
Wzorce projektowe. Rusz głową! Tworzenie rozszerzalnego i łatwego w utrzymaniu oprogramowania obiektowego. Wydanie II
Autor:
Eric Freeman, Elisabeth Robson

Tytuł:
Wzorce projektowe w .NET Core 3. Projektowanie zorientowane obiektowo z wykorzystaniem C# i F#
Autor:
Dmitri Nesteruk

Tytuł:
Wzorce projektowe. Elementy oprogramowania obiektowego wielokrotnego użytku
Autor:
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

Tytuł:
Projektowanie interfejsów., Sprawdzone wzorce projektowe. Wydanie III
Autor:
Jenifer Tidwell, Charles Brewer, Aynne Valencia-Brooks

Tytuł:
Kubernetes. Wzorce projektowe. Komponenty wielokrotnego użycia do projektowania natywnych aplikacji chmurowych
Autor:
Bilgin Ibryam, Roland Huß

Tytuł:
Wzorce projektowe w .NET. Projektowanie zorientowane obiektowo z wykorzystaniem C# i F#
Autor:
Dmitri Nesteruk

Tytuł:
Programowanie zorientowane obiektowo. Wzorce projektowe. Wydanie II
Autor:
Alan Shalloway, James R. Trott

Tytuł:
Java EE 8. Wzorce projektowe i najlepsze praktyki
Autor:
Rhuan Rocha, Joao Purificacao

Tytuł:
Systemy reaktywne. Wzorce projektowe i ich stosowanie
Autor:
Roland Kuhn Dr., Brian Hanafee, Jamie Allen