Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Class.cs
Go to the documentation of this file.
1
6
7using System;
8using System.Collections.Generic;
9
10
12{
21 {
22 public UserList Users = new UserList();
23 public GroupList Groups = new GroupList();
24 }
25
26
27 //########################################################################
28 //########################################################################
29
30
35 public class UserGroupMediator
36 {
37 // Normally this would be held somewhere else but for this example,
38 // the mediator will be the owner.
40
41
48 public void AddUser(string name)
49 {
51 }
52
53
59 public void RemoveUser(string name)
60 {
63 }
64
65
72 public void AddGroup(string name)
73 {
75 }
76
77
83 public void RemoveGroup(string name)
84 {
86 }
87
88
96 public void AddUserToGroup(string userName, string groupName)
97 {
98 // As mediator, we must verify the user exists because the group
99 // has no way to do this (groups have no knowledge of how users
100 // are stored, by design).
101 User? foundUser = _userGroupsContainer.Users.FindUser(userName);
102 if (foundUser == null)
103 {
104 string message = String.Format("User '{0}' does not exist. Cannot add to group '{1}'!", userName, groupName);
105 throw new ArgumentException(message);
106 }
107
108 Group? foundGroup = _userGroupsContainer.Groups.FindGroup(groupName);
109 if (foundGroup == null)
110 {
111 string message = String.Format("Cannot add user '{0}' to group '{1}' as that group does not exist!", userName, groupName);
112 throw new ArgumentException(message);
113 }
114
115 foundGroup.AddUser(userName);
116 }
117
118
125 public void RemoveUserFromGroup(string userName, string groupName)
126 {
127 // As mediator, we must verify the user exists because the group
128 // has no way to do this (groups have no knowledge of how users
129 // are stored, by design).
130 User? foundUser = _userGroupsContainer.Users.FindUser(userName);
131 if (foundUser == null)
132 {
133 string message = String.Format("User '{0}' does not exist. Cannot remove from group '{1}'!", userName, groupName);
134 throw new ArgumentException(message);
135 }
136
137 Group? foundGroup = _userGroupsContainer.Groups.FindGroup(groupName);
138 if (foundGroup == null)
139 {
140 string message = String.Format("Cannot remove user '{0}' from group '{1}' as that group does not exist!", userName, groupName);
141 throw new ArgumentException(message);
142 }
143
144 foundGroup.RemoveUser(userName);
145 }
146
147
153 public void RemoveUserFromAllGroups(string userName)
154 {
155 // As mediator, we must verify the user exists because the group
156 // has no way to do this (groups have no knowledge of how users
157 // are stored, by design).
158 User? foundUser = _userGroupsContainer.Users.FindUser(userName);
159 if (foundUser == null)
160 {
161 string message = String.Format("User '{0}' does not exist. Cannot remove from all groups'!", userName);
162 throw new ArgumentException(message);
163 }
164
165 foreach (string groupName in _userGroupsContainer.Groups.GroupNames)
166 {
167 Group? group = _userGroupsContainer.Groups.FindGroup(groupName);
168 if (group != null)
169 {
170 if (group.ContainsUser(userName))
171 {
172 group.RemoveUser(userName);
173 }
174 }
175 }
176 }
177
178
186 public bool IsUserInGroup(string userName, string groupName)
187 {
188 bool userInGroup = false;
189
190 User? foundUser = _userGroupsContainer.Users.FindUser(userName);
191 if (foundUser == null)
192 {
193 string message = String.Format("User '{0}' does not exist. Cannot determine if user is in group '{1}'!", userName, groupName);
194 throw new ArgumentException(message);
195 }
196
197 Group? foundGroup = _userGroupsContainer.Groups.FindGroup(groupName);
198 if (foundGroup == null)
199 {
200 string message = String.Format("Cannot determine if user '{0}' is in group '{1}' as that group does not exist!", userName, groupName);
201 throw new ArgumentException(message);
202 }
203
204 userInGroup = foundGroup.ContainsUser(userName);
205
206 return userInGroup;
207 }
208
209
217 public string[] GetGroupsWithUser(string userName)
218 {
219 User? foundUser = _userGroupsContainer.Users.FindUser(userName);
220 if (foundUser == null)
221 {
222 string message = String.Format("User '{0}' does not exist. Cannot get groups container user!", userName);
223 throw new ArgumentException(message);
224 }
225
226 List<string> groupNames = new List<string>();
227 foreach(string groupName in _userGroupsContainer.Groups.GroupNames)
228 {
229 Group? group = _userGroupsContainer.Groups.FindGroup(groupName);
230 if (group != null)
231 {
232 if (group.ContainsUser(userName))
233 {
234 groupNames.Add(groupName);
235 }
236 }
237 }
238 return groupNames.ToArray();
239 }
240
241
249 public string[] GetUsersInGroup(string groupName)
250 {
251 Group? foundGroup = _userGroupsContainer.Groups.FindGroup(groupName);
252 if (foundGroup == null)
253 {
254 string message = String.Format("Cannot determine get users in group '{0}' as that group does not exist!", groupName);
255 throw new ArgumentException(message);
256 }
257 return (string[])foundGroup.Users.Clone();
258 }
259
260
265 public string[] GetAllGroups()
266 {
267 return (string[])_userGroupsContainer.Groups.GroupNames.Clone();
268 }
269
270
275 public string[] GetAllUsers()
276 {
277 return (string[])_userGroupsContainer.Users.UserNames.Clone();
278 }
279 }
280}
Represents a single group. A group has a name and zero or more users. Users are tracked by name.
bool ContainsUser(string name)
Determine if the specified user is in this group. This is a case- sensitive search.
void AddUser(string name)
Add the specified user to this group. If the user is already in the group, the operation is ignored.
void RemoveUser(string name)
Remove a user from this group. If the user is not in the group then the operation is ignored.
string[] Users
The names of users in this group (read-only).
Group? FindGroup(string name)
Retrieve the Group instance for the specified group name.
void RemoveGroup(string name)
Remove the specified group from the list. Operation ignored if the group is not in the list.
void AddGroup(string name)
Add a group to the list using the given group name. Operation ignored if the group is already in the ...
string[] GroupNames
The names of all groups contained in this list (read-only). The list is always sorted.
Represents the mediator between caller, users, and groups. All users and groups are identified by str...
string[] GetAllUsers()
Retrieve a list of all known users.
void RemoveUserFromGroup(string userName, string groupName)
Remove the specified user from the specified group.
void RemoveGroup(string name)
Remove the specified group from the list of known groups if the group exists.
void RemoveUserFromAllGroups(string userName)
Remove the specified user from all existing groups.
string[] GetAllGroups()
Retrieve a list of all known groups.
void AddUser(string name)
Add a user to the list of known users. If the name is already in the list of users,...
void AddUserToGroup(string userName, string groupName)
Add the specified user to the specified group. If the user is already in the group,...
bool IsUserInGroup(string userName, string groupName)
Determine if the specified user is in the specified group.
void RemoveUser(string name)
Removes the specified user from the list of known users, if the user exists. Also removes the user fr...
string[] GetUsersInGroup(string groupName)
Retrieve a list of users in the specified group.
string[] GetGroupsWithUser(string userName)
Retrieve a list of all groups that contain the specified user.
void AddGroup(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...
A simple container for the user and group lists. This represents some entity external to the mediator...
Represents a user with a name.
User? FindUser(string name)
Retrieve the User instance for the specified user name.
void AddUser(string name)
Add the specified user name as a user. Operation ignored if user is already in the list.
void RemoveUser(string name)
Remove the specified user name as a user. Operation ignored if user is not in the list.
string[] UserNames
The user names contained in this list (read-only). The list is always sorted.
The namespace containing all Design Pattern Examples implemented in C#.