Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Exercise.cpp
Go to the documentation of this file.
1
6
7#include <iostream>
8#include <sstream>
9
10#include "helpers/formatstring.h"
11
12#include "Mediator_Exercise.h"
13#include "Mediator_Class.h"
14
15namespace // Anonymous
16{
17 using namespace DesignPatternExamples_cpp;
18
25 std::string _ListToString(const std::vector<std::string>& items)
26 {
27 std::ostringstream output;
28 for (size_t index = 0; index < items.size(); ++index)
29 {
30 if (index != 0)
31 {
32 output << ", ";
33 }
34 output << items[index];
35 }
36 return output.str();
37 }
38
44 {
45 // Operation 12: Add a user
46 mediator.AddUser("Stephen");
47 mediator.AddUser("Gladys");
48 mediator.AddUser("Marvin");
49 mediator.AddUser("Arthur");
50 }
51
59 {
60 // Operation 10: Add a group
61 mediator.AddGroup("admins");
62 mediator.AddGroup("Users");
63 mediator.AddGroup("Power Users");
64
65 // Operation 7: Add user to a group
66 mediator.AddUserToGroup("Marvin", "admins");
67 mediator.AddUserToGroup("Arthur", "admins");
68 mediator.AddUserToGroup("Stephen", "Users");
69 mediator.AddUserToGroup("Gladys", "Users");
70 mediator.AddUserToGroup("Arthur", "Power Users");
71 mediator.AddUserToGroup("Marvin", "Power Users");
72 }
73
74} // end namespace Anonmyous
75
76
78{
79
92 // ! [Using Mediator in C++]
94 {
95 std::cout << std::endl;
96 std::cout << "Mediator Exercise" << std::endl;
97
98 UserGroupMediator mediator;
99
100 Mediator_SetupUsers(mediator);
101 Mediator_SetupGroups(mediator);
102
103 //-----------------------------------------------------------------
104 // Operation 1: Determine all groups
105 std::cout << " Operation 1: Show all groups" << std::endl;
106 std::cout
107 << Helpers::formatstring(" All groups: %s", _ListToString(mediator.GetAllGroups()).c_str())
108 << std::endl;
109
110 //-----------------------------------------------------------------
111 // Operation 2: Determine all users
112 std::cout << " Operation 2: Show all users" << std::endl;
113 std::cout
114 << Helpers::formatstring(" All users : {0}", _ListToString(mediator.GetAllUsers()).c_str())
115 << std::endl;
116
117 //-----------------------------------------------------------------
118 // Operation 3: Does a user belong to a group
119 std::cout << " Operation 3: Determine if a user is a member of a specific group." << std::endl;
120 std::string userName = "Arthur";
121 std::string groupName = "admins";
122 std::cout << Helpers::formatstring(" Is user '%s' in the '%s' group?",
123 userName.c_str(), groupName.c_str());
124 std::cout
125 << Helpers::formatstring(" %s", mediator.IsUserInGroup(userName, groupName) ? "Yes" : "No")
126 << std::endl;
127
128 //-----------------------------------------------------------------
129 // Operation 4: Show all users in a group
130 std::cout << " Operation 4: Show all users in a specific group." << std::endl;
131 groupName = "Users";
132 StringList userNames = mediator.GetUsersInGroup(groupName);
133 std::cout
134 << Helpers::formatstring(" All users in '%s' group: %s",
135 groupName.c_str(), _ListToString(userNames).c_str())
136 << std::endl;
137
138 //-----------------------------------------------------------------
139 // Operation 5: Show all groups with a user
140 std::cout << " Operation 5: Show all groups containing a specific user." << std::endl;
141 userName = "Marvin";
142 StringList groupNames = mediator.GetGroupsWithUser(userName);
143 std::cout
144 << Helpers::formatstring(" All groups with user '%s': %s",
145 userName.c_str(), _ListToString(groupNames).c_str())
146 << std::endl;
147
148 //-----------------------------------------------------------------
149 // Operation 6: Show Remove a user from a group
150 std::cout << " Operation 6: Remove a user from a group." << std::endl;
151 userName = "Marvin";
152 groupName = "Power Users";
153 mediator.RemoveUserFromGroup(userName, groupName);
154 std::cout << Helpers::formatstring(" Removed user '%s' from group '%s'",
155 userName.c_str(), groupName.c_str()) << std::endl;
156 groupNames = mediator.GetGroupsWithUser(userName);
157 std::cout
158 << Helpers::formatstring(" All groups with user '%s': %s", userName.c_str(),
159 _ListToString(groupNames).c_str())
160 << std::endl;
161
162 //-----------------------------------------------------------------
163 // Operation 7: Add a user to a group
164 std::cout << " Operation 7: Add a user to a group." << std::endl;
165 groupName = "Users";
166 std::cout << Helpers::formatstring(" Adding user '%s' to group '%s'.",
167 userName.c_str(), groupName.c_str())
168 << std::endl;
169 mediator.AddUserToGroup(userName, groupName);
170 groupNames = mediator.GetGroupsWithUser(userName);
171 std::cout
172 << Helpers::formatstring(" All groups with user '%s': %s",
173 userName.c_str(), _ListToString(groupNames).c_str())
174 << std::endl;
175
176 //-----------------------------------------------------------------
177 // Operation 8: Remove a user from all groups
178 std::cout << " Operation 8: Remove a user from all groups." << std::endl;
179 userName = "Arthur";
180 groupNames = mediator.GetGroupsWithUser(userName);
181 std::cout << Helpers::formatstring(" Removing user '%s' from all groups.",
182 userName.c_str())
183 << std::endl;
184 std::cout
185 << Helpers::formatstring(" Start: all groups with user '%s': %s",
186 userName.c_str(), _ListToString(groupNames).c_str())
187 << std::endl;
188 std::cout << " Removing..." << std::endl;
189 mediator.RemoveUserFromAllGroups(userName);
190 groupNames = mediator.GetGroupsWithUser(userName);
191 std::cout
192 << Helpers::formatstring(" End: all groups with user '%s': %s",
193 userName.c_str(), _ListToString(groupNames).c_str())
194 << std::endl;
195
196 //-----------------------------------------------------------------
197 // Operation 9: Remove a user (which also removes user from all groups)
198 std::cout << " Operation 9: Remove a user (also removes the user from all groups)." << std::endl;
199 userName = "Marvin";
200 std::cout << Helpers::formatstring(" Removing user '%s'.", userName.c_str()) << std::endl;
201 mediator.RemoveUser(userName);
202 std::cout
203 << Helpers::formatstring(" All users : %s", _ListToString(mediator.GetAllUsers()).c_str())
204 << std::endl;
205 groupNames = mediator.GetAllGroups();
206 for (std::string name : groupNames)
207 {
208 userNames = mediator.GetUsersInGroup(name);
209 std::cout
210 << Helpers::formatstring(" Users in group '%s': %s", name.c_str(), _ListToString(userNames).c_str())
211 << std::endl;
212 }
213 //-----------------------------------------------------------------
214
215 std::cout << " Done." << std::endl;
216 }
217 // ! [Using Mediator in C++]
218
219} // end namespace
Implementation of the UserGroupsContainer and the UserGroupMediator classes used in the Mediator Patt...
static bool _ListToString(StringList *items, DynamicString *output)
Helper function to convert a list of strings to a comma-delimited list in a single string.
static bool Mediator_SetupUsers(void)
Helper function to add a number of users to the Users list.
static bool Mediator_SetupGroups(void)
Helper function to add a number of groups to the Groups list and then add users to the groups....
Represents the mediator between caller, users, and groups. All users and groups are identified by str...
StringList GetGroupsWithUser(std::string userName)
Retrieve a list of all groups that contain the specified user.
void AddGroup(std::string name)
Add a group to the list of known groups. If the group is already in the list, the request to add is i...
StringList GetAllUsers()
Retrieve a list of all known users.
void RemoveUserFromAllGroups(std::string userName)
Remove the specified user from all existing groups.
void RemoveUser(std::string name)
Removes the specified user from the list of known users, if the user exists. Also removes the user fr...
void AddUserToGroup(std::string userName, std::string groupName)
Add the specified user to the specified group. If the user is already in the group,...
void AddUser(std::string name)
Add a user to the list of known users. If the name is already in the list of users,...
bool IsUserInGroup(std::string userName, std::string groupName)
Determine if the specified user is in the specified group.
StringList GetAllGroups()
Retrieve a list of all known groups.
void RemoveUserFromGroup(std::string userName, std::string groupName)
Remove the specified user from the specified group.
StringList GetUsersInGroup(std::string groupName)
Retrieve a list of users in the specified group.
Declaration of the Mediator_Exercise() function as used in the Mediator Pattern.
std::vector< std::string > StringList
Typedef for a vector of std::string.
The namespace containing all Design Pattern Examples implemented in C++.
void Mediator_Exercise()
Example of using the Mediator design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....