-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediatorPattern.cpp
More file actions
138 lines (124 loc) · 4.28 KB
/
MediatorPattern.cpp
File metadata and controls
138 lines (124 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// main.cpp
// mediator-pattern
//
//
#include <iostream>
class Mediator {
public:
virtual void mediate(const std::string & event) = 0;
};
class InterfaceElement {
protected:
std::string name;
bool isVisible = true;
public:
InterfaceElement(const std::string & name, bool isVisible) : name(name), isVisible(isVisible) {};
void setVisibility(bool isVisible) { this->isVisible = isVisible; };
std::string getDescription() {
return isVisible
? name + " is visible"
: name + " is NOT visible";
}
};
class ButtonElement : public InterfaceElement {
public:
ButtonElement(const std::string & name, bool isVisible) : InterfaceElement(name, isVisible) {};
virtual ~ButtonElement() {};
virtual void click() = 0;
};
class TextBox : public InterfaceElement {
std::string textValue = "";
public:
TextBox(const std::string & name, bool isVisible) : InterfaceElement(name, isVisible) {};
virtual ~TextBox() {};
virtual void changeText(const std::string & newValue) { textValue = newValue; };
};
class CheckBox : public InterfaceElement {
bool isChecked = false;
public:
CheckBox(const std::string & name, bool isVisible) : InterfaceElement(name, isVisible) {};
virtual ~CheckBox() {};
virtual void setIsChecked(bool isChecked) { this->isChecked = isChecked; };
};
class SubmitButton : public ButtonElement {
public:
SubmitButton() : ButtonElement("Submit button", false) {};
void click() override {
std::cout << "Submitted!";
}
};
class NameTextBox : public TextBox {
SubmitButton *submitButton;
public:
NameTextBox(SubmitButton *submitButton) : TextBox("Name textbox", true), submitButton(submitButton) {};
void changeText(const std::string & newValue) override {
if (newValue.empty()) {
submitButton->setVisibility(false);
} else {
submitButton->setVisibility(true);
}
TextBox::changeText(newValue);
}
};
class SpousesNameTextBox : public TextBox {
public:
SpousesNameTextBox() : TextBox("Spouse's name textbox", false) {};
};
class IsMarriedCheckbox : public CheckBox {
SpousesNameTextBox *spousesNameTextBox;
public:
IsMarriedCheckbox(SpousesNameTextBox *spousesNameTextBox) : CheckBox("Is married checkbox", true), spousesNameTextBox(spousesNameTextBox) {};
void setIsChecked(bool isChecked) override {
if (isChecked) {
spousesNameTextBox->setVisibility(true);
} else {
spousesNameTextBox->setVisibility(false);
}
CheckBox::setIsChecked(isChecked);
}
};
class UserInterface : public Mediator {
TextBox *nameTextBox;
CheckBox *isMarriedCheckbox;
TextBox *spousesNameTextBox;
ButtonElement *submitButton;
public:
UserInterface() {
nameTextBox = new TextBox("Name textbox", true);
isMarriedCheckbox = new CheckBox("Is married checkbox", true);
spousesNameTextBox = new TextBox("Spouse's name textbox", false);
submitButton = new ButtonElement("Submit button", false);
}
~UserInterface() {
delete nameTextBox;
delete isMarriedCheckbox;
delete spousesNameTextBox;
delete submitButton;
}
void mediate(const std::string & event) override {
std::cout << "Mediating event: " << event << "...\n";
if (event == "Name textbox is empty") {
submitButton->setVisibility(false);
} else if (event == "Name textbox is not empty") {
submitButton->setVisibility(true);
} else if (event == "Is married checkbox is checked") {
spousesNameTextBox->setVisibility(true);
} else if (event == "Is married checkbox is unchecked") {
spousesNameTextBox->setVisibility(false);
} else if (event == "Submit button clicked") {
std::cout << "Submitted!\n";
} else {
std::cout << "Unrecognized event!";
}
}
TextBox *getNameTextBox() { return nameTextBox; };
CheckBox *getIsMarriedCheckbox() { return isMarriedCheckbox; };
TextBox *getSpousesNameTextBox() { return spousesNameTextBox; };
ButtonElement *getSubmitButton() { return submitButton; };
};
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}