Design Pattern Examples
Overview of object-oriented design patterns
mediator_exercise.py
Go to the documentation of this file.
6
7from .mediator_class import UserGroupMediator
8
9
16def _ListToString(items : list[str]) -> str:
17 return ", ".join(items)
18
19
23def Mediator_SetupUsers(mediator : UserGroupMediator) -> None:
24 # Operation 12: Add a user
25 mediator.AddUser("Stephen");
26 mediator.AddUser("Gladys");
27 mediator.AddUser("Marvin");
28 mediator.AddUser("Arthur");
29
30
31
37def Mediator_SetupGroups(mediator : UserGroupMediator) -> None:
38 # Operation 10: Add a group
39 mediator.AddGroup("admins");
40 mediator.AddGroup("Users");
41 mediator.AddGroup("Power Users");
42
43 # Operation 7: Add user to a group
44 mediator.AddUserToGroup("Marvin", "admins");
45 mediator.AddUserToGroup("Arthur", "admins");
46 mediator.AddUserToGroup("Stephen", "Users");
47 mediator.AddUserToGroup("Gladys", "Users");
48 mediator.AddUserToGroup("Arthur", "Power Users");
49 mediator.AddUserToGroup("Marvin", "Power Users");
50
51
52
62
63# ! [Using Mediator in Python]
65 print()
66 print("Mediator Exercise")
67
68 mediator = UserGroupMediator()
69
70 Mediator_SetupUsers(mediator)
71 Mediator_SetupGroups(mediator)
72
73 #-----------------------------------------------------------------
74 # Operation 1: Determine all groups
75 print(" Operation 1: Show all groups")
76 print(" All groups: {0}".format(_ListToString(mediator.GetAllGroups())))
77
78 #-----------------------------------------------------------------
79 # Operation 2: Determine all users
80 print(" Operation 2: Show all users");
81 print(" All users : {0}".format(_ListToString(mediator.GetAllUsers())))
82
83 #-----------------------------------------------------------------
84 # Operation 3: Does a user belong to a group
85 print(" Operation 3: Determine if a user is a member of a specific group.");
86 userName = "Arthur";
87 groupName = "admins";
88 print(" Is user '{0}' in the '{1}' group?".format(userName, groupName), end="")
89 print(" {0}".format("Yes" if mediator.IsUserInGroup(userName, groupName) else "No"))
90
91 #-----------------------------------------------------------------
92 # Operation 4: Show all users in a group
93 print(" Operation 4: Show all users in a specific group.");
94 groupName = "Users";
95 userNames = mediator.GetUsersInGroup(groupName);
96 print(" All users in '{0}' group: {1}".format(groupName, _ListToString(userNames)))
97
98 #-----------------------------------------------------------------
99 # Operation 5: Show all groups with a user
100 print(" Operation 5: Show all groups containing a specific user.");
101 userName = "Marvin";
102 groupNames = mediator.GetGroupsWithUser(userName);
103 print(" All groups with user '{0}': {1}".format(userName, _ListToString(groupNames)))
104
105 #-----------------------------------------------------------------
106 # Operation 6: Show Remove a user from a group
107 print(" Operation 6: Remove a user from a group.");
108 userName = "Marvin";
109 groupName = "Power Users";
110 mediator.RemoveUserFromGroup(userName, groupName);
111 print(" Removed user '{0}' from group '{1}'".format(userName, groupName))
112 groupNames = mediator.GetGroupsWithUser(userName);
113 print(" All groups with user '{0}': {1}".format(userName, _ListToString(groupNames)))
114
115 #-----------------------------------------------------------------
116 # Operation 7: Add a user to a group
117 print(" Operation 7: Add a user to a group.");
118 groupName = "Users";
119 print(" Adding user '{0}' to group '{1}'.".format(userName, groupName))
120 mediator.AddUserToGroup(userName, groupName);
121 groupNames = mediator.GetGroupsWithUser(userName);
122 print(" All groups with user '{0}': {1}".format(userName, _ListToString(groupNames)))
123
124 #-----------------------------------------------------------------
125 # Operation 8: Remove a user from all groups
126 print(" Operation 8: Remove a user from all groups.");
127 userName = "Arthur";
128 groupNames = mediator.GetGroupsWithUser(userName);
129 print(" Removing user '{0}' from all groups.".format(userName))
130 print(" Start: all groups with user '{0}': {1}".format(userName, _ListToString(groupNames)))
131 print(" Removing...");
132 mediator.RemoveUserFromAllGroups(userName);
133 groupNames = mediator.GetGroupsWithUser(userName);
134 print(" End: all groups with user '{0}': {1}".format(userName, _ListToString(groupNames)))
135
136 #-----------------------------------------------------------------
137 # Operation 9: Remove a user (which also removes user from all groups)
138 print(" Operation 9: Remove a user (also removes the user from all groups).");
139 userName = "Marvin";
140 print(" Removing user '{0}'.".format(userName))
141 mediator.RemoveUser(userName);
142 print(" All users : {0}".format(_ListToString(mediator.GetAllUsers())))
143 groupNames = mediator.GetAllGroups();
144 for name in groupNames:
145 userNames = mediator.GetUsersInGroup(name);
146 print(" Users in group '{0}': {1}".format(name, _ListToString(userNames)))
147
148 #-----------------------------------------------------------------
149
150 print(" Done.")
151# ! [Using Mediator in Python]
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.
def Mediator_Exercise()
Example of using the Mediator Pattern.
str _ListToString(list[str] items)
Helper method to convert a list of strings to a comma-delimited list in a single string.