Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Exercise.c File Reference

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"
Include dependency graph for Mediator_Exercise.c:

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.
 

Detailed Description

Implementation of the Mediator_Exercise() function as used in the Mediator Pattern.

Definition in file Mediator_Exercise.c.

Function Documentation

◆ _GetMediatorErrorMessage()

static const char * _GetMediatorErrorMessage ( MediatorErrorCode  errorCode)
static

Helper function to convert a MediatorErrorCode to a string.

Parameters
errorCodeA value from the MediatorErrorCode enumeration indicating some kind of error condition from the mediator.
Returns
Returns a pointer to a string representing the error code.

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().

◆ _ListToString()

static bool _ListToString ( StringList items,
DynamicString output 
)
static

Helper function to convert a list of strings to a comma-delimited list in a single string.

Parameters
itemsThe list of strings to convert.
outputA DynamicString object to be filled with the string representation of the list.
Returns
Returns true if the string was created successfully; otherwise, returns false indicating an out of memory condition (or NULL argument).

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().

◆ _Mediator_Example_AddUserToGroup()

static bool _Mediator_Example_AddUserToGroup ( void  )
static

Helper function example of how to add a user to a group.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ _Mediator_Example_RemoveUser()

static bool _Mediator_Example_RemoveUser ( void  )
static

Helper function example of how to remove a user.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ _Mediator_Example_RemoveUserFromAllGroups()

static bool _Mediator_Example_RemoveUserFromAllGroups ( void  )
static

Helper function example of how to remove a user from all groups.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ _Mediator_Example_RemoveUserFromGroup()

static bool _Mediator_Example_RemoveUserFromGroup ( void  )
static

Helper function example of how to remove a user from a group.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ _Mediator_Example_SeeIfUserInGroup()

static bool _Mediator_Example_SeeIfUserInGroup ( void  )
static

Helper function example of how to see if a user is in a group.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

Definition at line 283 of file Mediator_Exercise.c.

References Mediator_IsUserInGroup().

Referenced by Mediator_Exercise().

◆ _Mediator_Example_ShowAllGroups()

static bool _Mediator_Example_ShowAllGroups ( void  )
static

Helper function example of how to show all groups.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ _Mediator_Example_ShowAllGroupsContainingUser()

static bool _Mediator_Example_ShowAllGroupsContainingUser ( void  )
static

Helper function example of how to show all groups containing a user.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ _Mediator_Example_ShowAllUsers()

static bool _Mediator_Example_ShowAllUsers ( void  )
static

Helper function example of how to show all users.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ _Mediator_Example_ShowUsersInGroup()

static bool _Mediator_Example_ShowUsersInGroup ( void  )
static

Helper function example of how to show all users in a group.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ _ReportAnyMediatorError()

static void _ReportAnyMediatorError ( const char *  prompt,
MediatorErrorCode  errorCode 
)
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.

Parameters
promptWhat was happening when the error occurred.
errorCodeA 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().

◆ Mediator_Exercise()

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().

◆ Mediator_SetupGroups()

static bool Mediator_SetupGroups ( void  )
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.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().

◆ Mediator_SetupUsers()

static bool Mediator_SetupUsers ( void  )
static

Helper function to add a number of users to the Users list.

Returns
Returns true if okay to continue running example; otherwise, returns false, an error occurred. The error has already been reported.

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().