Implementation of the Mediator_Exercise() function as used in the Mediator Pattern. More...
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "helpers/stringlist.h"
#include "helpers/dynamicstring.h"
#include "Mediator_Functions.h"
#include "Mediator_Exercise.h"
Go to the source code of this file.
Functions | |
static bool | _ListToString (StringList *items, DynamicString *output) |
Helper function to convert a list of strings to a comma-delimited list in a single string. | |
static const char * | _GetMediatorErrorMessage (MediatorErrorCode errorCode) |
Helper function to convert a MediatorErrorCode to a string. | |
static void | _ReportAnyMediatorError (const char *prompt, MediatorErrorCode errorCode) |
Helper function for reporting an error if an error has occurred. Always call this after any mediator call; if there is no error, nothing is reported. | |
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. Note that everything here is done with names. | |
static bool | _Mediator_Example_ShowAllGroups (void) |
Helper function example of how to show all groups. | |
static bool | _Mediator_Example_ShowAllUsers (void) |
Helper function example of how to show all users. | |
static bool | _Mediator_Example_SeeIfUserInGroup (void) |
Helper function example of how to see if a user is in a group. | |
static bool | _Mediator_Example_ShowUsersInGroup (void) |
Helper function example of how to show all users in a group. | |
static bool | _Mediator_Example_ShowAllGroupsContainingUser (void) |
Helper function example of how to show all groups containing a user. | |
static bool | _Mediator_Example_RemoveUserFromGroup (void) |
Helper function example of how to remove a user from a group. | |
static bool | _Mediator_Example_AddUserToGroup (void) |
Helper function example of how to add a user to a group. | |
static bool | _Mediator_Example_RemoveUserFromAllGroups (void) |
Helper function example of how to remove a user from all groups. | |
static bool | _Mediator_Example_RemoveUser (void) |
Helper function example of how to remove a user. | |
void | Mediator_Exercise (void) |
Example of using the Mediator Pattern. | |
Implementation of the Mediator_Exercise() function as used in the Mediator Pattern.
Definition in file Mediator_Exercise.c.
|
static |
Helper function to convert a MediatorErrorCode to a string.
errorCode | A value from the MediatorErrorCode enumeration indicating some kind of error condition from the mediator. |
Definition at line 65 of file Mediator_Exercise.c.
References MediatorErrorCode_Group_Does_Not_Exist, MediatorErrorCode_No_Error, MediatorErrorCode_No_Memory, MediatorErrorCode_Null_Argument, and MediatorErrorCode_User_Does_Not_Exist.
Referenced by _ReportAnyMediatorError().
|
static |
Helper function to convert a list of strings to a comma-delimited list in a single string.
items | The list of strings to convert. |
output | A DynamicString object to be filled with the string representation of the list. |
Definition at line 30 of file Mediator_Exercise.c.
References DynamicString_Append().
Referenced by _Mediator_Example_AddUserToGroup(), _Mediator_Example_RemoveUser(), _Mediator_Example_RemoveUserFromAllGroups(), _Mediator_Example_RemoveUserFromGroup(), _Mediator_Example_ShowAllGroups(), _Mediator_Example_ShowAllGroupsContainingUser(), _Mediator_Example_ShowAllUsers(), _Mediator_Example_ShowUsersInGroup(), and DesignPatternExamples_cpp::Mediator_Exercise().
|
static |
Helper function example of how to add a user to a group.
Definition at line 403 of file Mediator_Exercise.c.
References _ListToString(), _ReportAnyMediatorError(), DynamicString_Clear(), Mediator_AddUserToGroup(), Mediator_GetGroupsWithUser(), MediatorErrorCode_No_Error, DynamicString::string, and StringList_Clear().
Referenced by Mediator_Exercise().
|
static |
Helper function example of how to remove a user.
Definition at line 491 of file Mediator_Exercise.c.
References _ListToString(), _ReportAnyMediatorError(), DynamicString_Clear(), Mediator_GetAllGroups(), Mediator_GetAllUsers(), Mediator_GetUsersInGroup(), Mediator_RemoveUser(), MediatorErrorCode_No_Error, DynamicString::string, and StringList_Clear().
Referenced by Mediator_Exercise().
|
static |
Helper function example of how to remove a user from all groups.
Definition at line 441 of file Mediator_Exercise.c.
References _ListToString(), _ReportAnyMediatorError(), DynamicString_Clear(), Mediator_GetGroupsWithUser(), Mediator_RemoveUserFromAllGroups(), MediatorErrorCode_No_Error, DynamicString::string, and StringList_Clear().
Referenced by Mediator_Exercise().
|
static |
Helper function example of how to remove a user from a group.
Definition at line 365 of file Mediator_Exercise.c.
References _ListToString(), _ReportAnyMediatorError(), DynamicString_Clear(), Mediator_GetGroupsWithUser(), Mediator_RemoveUserFromGroup(), MediatorErrorCode_No_Error, DynamicString::string, and StringList_Clear().
Referenced by Mediator_Exercise().
|
static |
Helper function example of how to see if a user is in a group.
Definition at line 283 of file Mediator_Exercise.c.
References Mediator_IsUserInGroup().
Referenced by Mediator_Exercise().
|
static |
Helper function example of how to show all groups.
Definition at line 221 of file Mediator_Exercise.c.
References _ListToString(), _ReportAnyMediatorError(), DynamicString_Clear(), Mediator_GetAllGroups(), MediatorErrorCode_No_Error, DynamicString::string, and StringList_Clear().
Referenced by Mediator_Exercise().
|
static |
Helper function example of how to show all groups containing a user.
Definition at line 333 of file Mediator_Exercise.c.
References _ListToString(), _ReportAnyMediatorError(), DynamicString_Clear(), Mediator_GetGroupsWithUser(), MediatorErrorCode_No_Error, DynamicString::string, and StringList_Clear().
Referenced by Mediator_Exercise().
|
static |
Helper function example of how to show all users.
Definition at line 252 of file Mediator_Exercise.c.
References _ListToString(), _ReportAnyMediatorError(), DynamicString_Clear(), Mediator_GetAllUsers(), MediatorErrorCode_No_Error, DynamicString::string, and StringList_Clear().
Referenced by Mediator_Exercise().
|
static |
Helper function example of how to show all users in a group.
Definition at line 302 of file Mediator_Exercise.c.
References _ListToString(), _ReportAnyMediatorError(), DynamicString_Clear(), Mediator_GetUsersInGroup(), MediatorErrorCode_No_Error, DynamicString::string, and StringList_Clear().
Referenced by Mediator_Exercise().
|
static |
Helper function for reporting an error if an error has occurred. Always call this after any mediator call; if there is no error, nothing is reported.
This function makes it easier to capture the context in which an error occurred.
prompt | What was happening when the error occurred. |
errorCode | A value from the MediatorErrorCode enumeration indicating what went wrong, if anything. |
Definition at line 110 of file Mediator_Exercise.c.
References _GetMediatorErrorMessage(), and MediatorErrorCode_No_Error.
Referenced by _Mediator_Example_AddUserToGroup(), _Mediator_Example_RemoveUser(), _Mediator_Example_RemoveUserFromAllGroups(), _Mediator_Example_RemoveUserFromGroup(), _Mediator_Example_ShowAllGroups(), _Mediator_Example_ShowAllGroupsContainingUser(), _Mediator_Example_ShowAllUsers(), _Mediator_Example_ShowUsersInGroup(), Mediator_SetupGroups(), and Mediator_SetupUsers().
void Mediator_Exercise | ( | void | ) |
Example of using the Mediator Pattern.
A mediator is instantiated then populated with users and groups. Users are added to some of the groups.
A series of operations are then performed through the Mediator. The output shows the results of each operation. Note that all operations are done using user and group names, with no knowledge of the actual lists of users and groups. The mediator hides all the details.
The odd design of this example is to more effectively accommodate error handling from the mediator. This is what you get if you don't have exceptions, like in C++.
Definition at line 577 of file Mediator_Exercise.c.
References _Mediator_Example_AddUserToGroup(), _Mediator_Example_RemoveUser(), _Mediator_Example_RemoveUserFromAllGroups(), _Mediator_Example_RemoveUserFromGroup(), _Mediator_Example_SeeIfUserInGroup(), _Mediator_Example_ShowAllGroups(), _Mediator_Example_ShowAllGroupsContainingUser(), _Mediator_Example_ShowAllUsers(), _Mediator_Example_ShowUsersInGroup(), Mediator_ClearAll(), Mediator_SetupGroups(), and Mediator_SetupUsers().
|
static |
Helper function to add a number of groups to the Groups list and then add users to the groups. Note that everything here is done with names.
Definition at line 163 of file Mediator_Exercise.c.
References _ReportAnyMediatorError(), Mediator_AddGroup(), Mediator_AddUserToGroup(), and MediatorErrorCode_No_Error.
Referenced by DesignPatternExamples_cpp::Mediator_Exercise(), DesignPatternExamples_python.mediator.mediator_exercise::Mediator_Exercise(), Mediator_Exercise(), and Mediator_Exercise::Run().
|
static |
Helper function to add a number of users to the Users list.
Definition at line 130 of file Mediator_Exercise.c.
References _ReportAnyMediatorError(), Mediator_AddUser(), and MediatorErrorCode_No_Error.
Referenced by DesignPatternExamples_cpp::Mediator_Exercise(), DesignPatternExamples_python.mediator.mediator_exercise::Mediator_Exercise(), Mediator_Exercise(), and Mediator_Exercise::Run().