Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Class.h
Go to the documentation of this file.
1
6
7#pragma once
8#ifndef __MEDIATOR_CLASS_H__
9#define __MEDIATOR_CLASS_H__
10
11#include <exception>
12
14#include "helpers/formatstring.h"
15
18
20{
21
30 {
31 public:
34 };
35
36
37 //########################################################################
38 //########################################################################
39
40
46 {
47 private:
48 // Normally this would be held somewhere else but for this example,
49 // the mediator will be the owner.
51
52 public:
59 void AddUser(std::string name)
60 {
62 }
63
64
70 void RemoveUser(std::string name)
71 {
74 }
75
76
83 void AddGroup(std::string name)
84 {
86 }
87
88
94 void RemoveGroup(std::string name)
95 {
97 }
98
99
107 void AddUserToGroup(std::string userName, std::string groupName)
108 {
109 // As mediator, we must verify the user exists because the group
110 // has no way to do this (groups have no knowledge of how users
111 // are stored, by design).
112 if (_userGroupsContainer.Users.FindUser(userName) == nullptr)
113 {
114 std::string message = Helpers::formatstring("User '%s' does not exist. Cannot add to group '%s'!",
115 userName.c_str(), groupName.c_str());
116 throw Helpers::argumentinvalid_error("userName", message.c_str());
117 }
118
119 Group* foundGroup = _userGroupsContainer.Groups.FindGroup(groupName);
120 if (foundGroup == nullptr)
121 {
122 std::string message = Helpers::formatstring("Cannot add user '%s' to group '%s' as that group does not exist!",
123 userName.c_str(), groupName.c_str());
124 throw Helpers::argumentinvalid_error("groupName", message.c_str());
125 }
126
127 foundGroup->AddUser(userName);
128 }
129
130
137 void RemoveUserFromGroup(std::string userName, std::string groupName)
138 {
139 // As mediator, we must verify the user exists because the group
140 // has no way to do this (groups have no knowledge of how users
141 // are stored, by design).
142 if (_userGroupsContainer.Users.FindUser(userName) == nullptr)
143 {
144 std::string message = Helpers::formatstring("User '%s' does not exist. Cannot remove from group '%s'!",
145 userName.c_str(), groupName.c_str());
146 throw Helpers::argumentinvalid_error("userName", message.c_str());
147 }
148
149 Group* foundGroup = _userGroupsContainer.Groups.FindGroup(groupName);
150 if (foundGroup == nullptr)
151 {
152 std::string message = Helpers::formatstring("Cannot remove user '%s' from group '%s' as that group does not exist!",
153 userName.c_str(), groupName.c_str());
154 throw Helpers::argumentinvalid_error("groupName", message.c_str());
155 }
156
157 foundGroup->RemoveUser(userName);
158 }
159
160
166 void RemoveUserFromAllGroups(std::string userName)
167 {
168 // As mediator, we must verify the user exists because the group
169 // has no way to do this (groups have no knowledge of how users
170 // are stored, by design).
171 if (_userGroupsContainer.Users.FindUser(userName) == nullptr)
172 {
173 std::string message = Helpers::formatstring("User '%s' does not exist. Cannot remove from all groups'!",
174 userName.c_str());
175 throw Helpers::argumentinvalid_error("userName", message.c_str());
176 }
177
178 for(std::string groupName : _userGroupsContainer.Groups.GroupNames())
179 {
180 Group* group = _userGroupsContainer.Groups.FindGroup(groupName);
181 if (group != nullptr)
182 {
183 if (group->ContainsUser(userName))
184 {
185 group->RemoveUser(userName);
186 }
187 }
188 }
189 }
190
191
199 bool IsUserInGroup(std::string userName, std::string groupName)
200 {
201 bool userInGroup = false;
202
203 if (_userGroupsContainer.Users.FindUser(userName) == nullptr)
204 {
205 std::string message = Helpers::formatstring("User '%s' does not exist. Cannot determine if user is in group '%s'!",
206 userName.c_str(), groupName.c_str());
207 throw Helpers::argumentinvalid_error("userName", message.c_str());
208 }
209
210 Group* foundGroup = _userGroupsContainer.Groups.FindGroup(groupName);
211 if (foundGroup == nullptr)
212 {
213 std::string message = Helpers::formatstring("Cannot determine if user '%s' is in group '%s' as that group does not exist!",
214 userName.c_str(), groupName.c_str());
215 throw Helpers::argumentinvalid_error("groupName", message.c_str());
216 }
217
218 userInGroup = foundGroup->ContainsUser(userName);
219
220 return userInGroup;
221 }
222
223
231 StringList GetGroupsWithUser(std::string userName)
232 {
233 if (_userGroupsContainer.Users.FindUser(userName) == nullptr)
234 {
235 std::string message = Helpers::formatstring("User '%s' does not exist. Cannot get groups containing user!",
236 userName.c_str());
237 throw Helpers::argumentinvalid_error("userName", message.c_str());
238 }
239
240 StringList groupNames;
241 for(std::string groupName : _userGroupsContainer.Groups.GroupNames())
242 {
243 Group* group = _userGroupsContainer.Groups.FindGroup(groupName);
244 if (group != nullptr)
245 {
246 if (group->ContainsUser(userName))
247 {
248 groupNames.push_back(groupName);
249 }
250 }
251 }
252 return groupNames;
253 }
254
255
263 StringList GetUsersInGroup(std::string groupName)
264 {
265 Group* foundGroup = _userGroupsContainer.Groups.FindGroup(groupName);
266 if (foundGroup == nullptr)
267 {
268 std::string message = Helpers::formatstring("Cannot determine get users in group '%s' as that group does not exist!",
269 groupName.c_str());
270 throw Helpers::argumentinvalid_error("groupName", message.c_str());
271 }
272 return foundGroup->Users();
273 }
274
275
281 {
283 }
284
285
291 {
293 }
294 };
295
296} // end namespace
297
298#endif // __MEDIATOR_CLASS_H__
299
Implementation of the Group and the GroupList classes used in the Mediator Pattern.
Implementation of the User and the UserList classes used in the Mediator Pattern.
Implementation of the argumentinvalid_error exception.
Represents a single group. A group has a name and zero or more users. Users are tracked by name.
void RemoveUser(std::string name)
Remove a user from this group. If the user is not in the group then the operation is ignored.
void AddUser(std::string name)
Add the specified user to this group. If the user is already in the group, the operation is ignored.
bool ContainsUser(std::string name)
Determine if the specified user is in this group. This is a case- sensitive search.
StringList Users() const
The names of users in this group (read-only).
void AddGroup(std::string name)
Add a group to the list using the given group name. Operation ignored if the group is already in the ...
StringList GroupNames()
The names of all groups contained in this list (read-only). The list is always sorted.
void RemoveGroup(std::string name)
Remove the specified group from the list. Operation ignored if the group is not in the list.
Group * FindGroup(std::string name)
Retrieve the Group instance for the specified group name. The found group may be altered so it must p...
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.
void RemoveGroup(std::string name)
Remove the specified group from the list of known groups if the group exists.
StringList GetUsersInGroup(std::string groupName)
Retrieve a list of users in the specified group.
A simple container for the user and group lists. This represents some entity external to the mediator...
void RemoveUser(std::string name)
Remove the specified user name as a user. Operation ignored if user is not in the list.
User * FindUser(std::string name)
Retrieve the User instance for the specified user name. The found user may be altered so it must poin...
void AddUser(std::string name)
Add the specified user name as a user. Operation ignored if user is already in the list.
StringList UserNames()
The user names contained in this list (read-only). The list is always sorted.
Exception for arguments that are invalid.
std::vector< std::string > StringList
Typedef for a vector of std::string.
The namespace containing all Design Pattern Examples implemented in C++.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....