Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7using System.Text;
8
10{
23 internal class Mediator_Exercise
24 {
31 string _ListToString(string[] items)
32 {
33 StringBuilder output = new StringBuilder();
34 for (int index = 0; index < items.Length; ++index)
35 {
36 if (index != 0)
37 {
38 output.Append(", ");
39 }
40 output.Append(items[index]);
41 }
42 return output.ToString();
43 }
44
50 {
51 // Operation 12: Add a user
52 mediator.AddUser("Stephen");
53 mediator.AddUser("Gladys");
54 mediator.AddUser("Marvin");
55 mediator.AddUser("Arthur");
56 }
57
65 {
66 // Operation 10: Add a group
67 mediator.AddGroup("admins");
68 mediator.AddGroup("Users");
69 mediator.AddGroup("Power Users");
70
71 // Operation 7: Add user to a group
72 mediator.AddUserToGroup("Marvin", "admins");
73 mediator.AddUserToGroup("Arthur", "admins");
74 mediator.AddUserToGroup("Stephen", "Users");
75 mediator.AddUserToGroup("Gladys", "Users");
76 mediator.AddUserToGroup("Arthur", "Power Users");
77 mediator.AddUserToGroup("Marvin", "Power Users");
78 }
79
83 // ! [Using Mediator in C#]
84 public void Run()
85 {
86 Console.WriteLine();
87 Console.WriteLine("Mediator Exercise");
88 UserGroupMediator mediator = new UserGroupMediator();
89
90 Mediator_SetupUsers(mediator);
91 Mediator_SetupGroups(mediator);
92
93 //-----------------------------------------------------------------
94 // Operation 1: Determine all groups
95 Console.WriteLine(" Operation 1: Show all groups");
96 Console.WriteLine(" All groups: {0}", _ListToString(mediator.GetAllGroups()));
97
98 //-----------------------------------------------------------------
99 // Operation 2: Determine all users
100 Console.WriteLine(" Operation 2: Show all users");
101 Console.WriteLine(" All users : {0}", _ListToString(mediator.GetAllUsers()));
102
103 //-----------------------------------------------------------------
104 // Operation 3: Does a user belong to a group
105 Console.WriteLine(" Operation 3: Determine if a user is a member of a specific group.");
106 string userName = "Arthur";
107 string groupName = "admins";
108 Console.Write(" Is user '{0}' in the '{1}' group?", userName, groupName);
109 Console.WriteLine(" {0}", mediator.IsUserInGroup(userName, groupName) ? "Yes" : "No");
110
111 //-----------------------------------------------------------------
112 // Operation 4: Show all users in a group
113 Console.WriteLine(" Operation 4: Show all users in a specific group.");
114 groupName = "Users";
115 string[] userNames = mediator.GetUsersInGroup(groupName);
116 Console.WriteLine(" All users in '{0}' group: {1}", groupName, _ListToString(userNames));
117
118 //-----------------------------------------------------------------
119 // Operation 5: Show all groups with a user
120 Console.WriteLine(" Operation 5: Show all groups containing a specific user.");
121 userName = "Marvin";
122 string[] groupNames = mediator.GetGroupsWithUser(userName);
123 Console.WriteLine(" All groups with user '{0}': {1}", userName, _ListToString(groupNames));
124
125 //-----------------------------------------------------------------
126 // Operation 6: Show Remove a user from a group
127 Console.WriteLine(" Operation 6: Remove a user from a group.");
128 userName = "Marvin";
129 groupName = "Power Users";
130 mediator.RemoveUserFromGroup(userName, groupName);
131 Console.WriteLine(" Removed user '{0}' from group '{1}'", userName, groupName);
132 groupNames = mediator.GetGroupsWithUser(userName);
133 Console.WriteLine(" All groups with user '{0}': {1}", userName, _ListToString(groupNames));
134
135 //-----------------------------------------------------------------
136 // Operation 7: Add a user to a group
137 Console.WriteLine(" Operation 7: Add a user to a group.");
138 groupName = "Users";
139 Console.WriteLine(" Adding user '{0}' to group '{1}'.", userName, groupName);
140 mediator.AddUserToGroup(userName, groupName);
141 groupNames = mediator.GetGroupsWithUser(userName);
142 Console.WriteLine(" All groups with user '{0}': {1}", userName, _ListToString(groupNames));
143
144 //-----------------------------------------------------------------
145 // Operation 8: Remove a user from all groups
146 Console.WriteLine(" Operation 8: Remove a user from all groups.");
147 userName = "Arthur";
148 groupNames = mediator.GetGroupsWithUser(userName);
149 Console.WriteLine(" Removing user '{0}' from all groups.", userName);
150 Console.WriteLine(" Start: all groups with user '{0}': {1}", userName, _ListToString(groupNames));
151 Console.WriteLine(" Removing...");
152 mediator.RemoveUserFromAllGroups(userName);
153 groupNames = mediator.GetGroupsWithUser(userName);
154 Console.WriteLine(" End: all groups with user '{0}': {1}", userName, _ListToString(groupNames));
155
156 //-----------------------------------------------------------------
157 // Operation 9: Remove a user (which also removes user from all groups)
158 Console.WriteLine(" Operation 9: Remove a user (also removes the user from all groups).");
159 userName = "Marvin";
160 Console.WriteLine(" Removing user '{0}'.", userName);
161 mediator.RemoveUser(userName);
162 Console.WriteLine(" All users : {0}", _ListToString(mediator.GetAllUsers()));
163 groupNames = mediator.GetAllGroups();
164 foreach (string name in groupNames)
165 {
166 userNames = mediator.GetUsersInGroup(name);
167 Console.WriteLine(" Users in group '{0}': {1}", name, _ListToString(userNames));
168 }
169 //-----------------------------------------------------------------
170
171 Console.WriteLine(" Done.");
172 }
173 // ! [Using Mediator in C#]
174 }
175}
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....
Example of using the Mediator Pattern in C#.
void Mediator_SetupGroups(UserGroupMediator mediator)
Helper method to add a number of groups to the Groups list and then add users to the groups....
string _ListToString(string[] items)
Helper method to convert a list of strings to a comma-delimited list in a single string.
void Run()
Executes the example for the Mediator Pattern in C#.
void Mediator_SetupUsers(UserGroupMediator mediator)
Helper method to add a number of users to the Users list.
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 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...
The namespace containing all Design Pattern Examples implemented in C#.