czy dobrze zrozumiałem wzorzec projektowy mediator?
mam na myśli
- wykonanie
- czy czegoś nie brakuje?
- a może dałoby się to zrobić lepiej? np co?
#include <iostream>
#include <string>
using namespace std;
class Widget
{
public:
Widget(){cout << "Create new widget" << endl;}
virtual void setName(const string &name)=0;
virtual string name()=0;
virtual void setMessage(const string &message)=0;
virtual string message()=0;
protected:
string widgetName="no name";
string widgetMessage="no message";
};
class Button : public Widget
{
public:
Button(){setName();}
virtual void setName(const string &name="Button") override{
this->objName = name;
cout << objName << " is created!!!" << endl;
}
virtual string name() override {return this->objName;}
virtual void setMessage(const string &message) override{
msg=message;
}
virtual string message(){
return msg;
};
private:
string objName, msg;
};
class CheckBox : public Widget
{
public:
CheckBox(){setName();}
virtual void setName(const string &name="Checkbox") override{
this->objName = name;
cout << objName << " is created!!!" << endl;
}
virtual string name() override {return this->objName;}
virtual void setMessage(const string &message) override{
msg=message;
}
virtual string message(){
return msg;
};
private:
string objName, msg;
};
class Mediator
{
public:
Mediator(){cout << "create mediator";}
Mediator(Widget *sender, Widget *received){
this->sender=sender;
this->received=received;
}
void setMessage(const string &message){
this->sender->setMessage(message);
this->received->setMessage(sender->message());
}
private:
Widget *sender, *received;
};
int main()
{
Button *btt = new Button;
CheckBox *chb = new CheckBox;
Mediator *med = new Mediator(btt,chb);
med->setMessage("clicked");
cout << chb->message();
}