Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Group_Classes.cs
Go to the documentation of this file.
1
6
7// The Mediator pattern acts as a go-between for multiple entities that want
8// no knowledge of any of the other entities except at the most abstract
9// level.
10//
11// The classes defined in this module are for handling groups. These classes
12// have no knowledge of the User class or the mediator class itself.
13//
14// This is a dead simple implementation of the concept of a group. There is
15// only a name of the group and the list of users (identified by name) being
16// tracked here.
17using System;
18using System.Collections.Generic;
19
21{
26 public class Group
27 {
31 private List<string> _users = new List<string>();
32
37 internal Group(string name)
38 {
39 Name = name;
40 }
41
45 public string Name { get; }
46
50 public string[] Users
51 {
52 get
53 {
54 return _users.ToArray();
55 }
56 }
57
64 public bool ContainsUser(string name)
65 {
66 return _users.Contains(name);
67 }
68
75 public void AddUser(string name)
76 {
77 if (String.IsNullOrEmpty(name))
78 {
79 throw new ArgumentNullException("name", "Must specify a user name to add it to the group.");
80 }
81
82 if (!ContainsUser(name))
83 {
84 _users.Add(name);
85 }
86 }
87
88
94 public void RemoveUser(string name)
95 {
96 if (_users.Contains(name))
97 {
98 _users.Remove(name);
99 }
100 }
101
107 public override bool Equals(object? obj)
108 {
109 if (obj != null)
110 {
111 if (obj is Group)
112 {
113 return ((Group)obj).Name == Name;
114 }
115 if (obj is string)
116 {
117 return (string)obj == Name;
118 }
119 }
120 return false;
121 }
122
130 public override int GetHashCode()
131 {
132 return Name.GetHashCode();
133 }
134 }
135
136
137 //########################################################################
138 //########################################################################
139
140
146 public class GroupList
147 {
151 private List<Group> _groups = new List<Group>();
152
153
158 public string[] GroupNames
159 {
160 get
161 {
162 List<string> groupNames = new List<string>();
163 foreach (Group group in _groups)
164 {
165 groupNames.Add(group.Name);
166 }
167 groupNames.Sort();
168 return groupNames.ToArray();
169 }
170 }
171
172
178 public Group? FindGroup(string name)
179 {
180 return _groups.Find(x => x.Equals(name));
181 }
182
183
190 public void AddGroup(string name)
191 {
192 if (String.IsNullOrEmpty(name))
193 {
194 throw new ArgumentNullException("name", "Must specify a group name to add it to the group list.");
195 }
196
197 Group newGroup = new Group(name);
198 if (!_groups.Contains(newGroup))
199 {
200 _groups.Add(newGroup);
201 }
202 }
203
204
210 public void RemoveGroup(string name)
211 {
212 Group existingGroup = new Group(name);
213 if (_groups.Contains(existingGroup))
214 {
215 _groups.Remove(existingGroup);
216 }
217 }
218 }
219}
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.
override int GetHashCode()
Generate a hash code for this instance.
void AddUser(string name)
Add the specified user to this group. If the user is already in the group, the operation is ignored.
string Name
The name of the group (read-only).
List< string > _users
The list of users in this group.
override bool Equals(object? obj)
Override to compare a Group or string to this Group.
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.
List< Group > _groups
The list of groups.
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.
The namespace containing all Design Pattern Examples implemented in C#.