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

Implementation of the NullObject_Exercise() function as used in the Null Object Pattern. More...

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "NullObject_MoveCommandList.h"
#include "NullObject_Exercise.h"
Include dependency graph for NullObject_Exercise.c:

Go to the source code of this file.

Functions

static void MoveCommand_Show (char commandToken, const char *commandName)
 Show the move command and its name followed by a newline.
 
static void MoveCommandLeft_Execute (void)
 Represents the Move Left command.
 
static void MoveCommandRight_Execute (void)
 Represents the Move Right command.
 
static void MoveCommandUp_Execute (void)
 Represents the Move Up command.
 
static void MoveCommandDown_Execute (void)
 Represents the Move Down command.
 
static void MoveCommandNone_Execute (void)
 Represents the Do Nothing command. This is the Null "Object" for this exercise.
 
static bool _MoveProcessor_ParseMoves (const char *moveList, MoveCommandList *commandList)
 Represents the processor that translates the move list into a list of MoveCommand objects then either displays them or executes them.
 
static void _MoveProcessor_ExecuteMoves (MoveCommandList *commands)
 Helper method to execute all the given commands.
 
static void _MoveProcessor_ShowMoves (MoveCommandList *commands)
 Show the command character and name of the command for all commands in the given list of commands.
 
static void MoveProcessor_ExecuteMoveList (const char *moveList)
 Parse and execute the given list of move commands, where each command is represents by a single character.
 
static void MoveProcessor_ShowMoveList (const char *moveList)
 Parse and display the given list of move commands, where each command is represents by a single character.
 
void NullObject_Exercise (void)
 Example of using the Null Object Pattern.
 

Detailed Description

Implementation of the NullObject_Exercise() function as used in the Null Object Pattern.

Definition in file NullObject_Exercise.c.

Function Documentation

◆ _MoveProcessor_ExecuteMoves()

static void _MoveProcessor_ExecuteMoves ( MoveCommandList commands)
static

Helper method to execute all the given commands.

Parameters
commandsA list of MoveCommand objects to "execute".

In this implementation, the MoveCommand object execute function prints the command as "<move xxx> " on the current line. When all commands have been printed, a new line is printed to move to the next line. The "Do Nothing" command doesn't print anything, leaving only the empty <>.

Definition at line 174 of file NullObject_Exercise.c.

References MoveCommandList::commands, MoveCommandList::commands_count, and MoveCommand::Execute.

Referenced by MoveProcessor_ExecuteMoveList().

◆ _MoveProcessor_ParseMoves()

static bool _MoveProcessor_ParseMoves ( const char *  moveList,
MoveCommandList commandList 
)
static

Represents the processor that translates the move list into a list of MoveCommand objects then either displays them or executes them.

This classes uses a parser to convert the single letter characters in a string into a list of actions (instances of the MoveCommand class). This list of actions is then executed to perform the operations.

This process of executing the list of operations is an example of the Command Pattern. This is also an example of the Interpreter Pattern, where the actions are the tokens to be interpreted.

Helper method to convert a list of single letter commands into a list of MoveCommand objects.

This method recognizes 'L', 'R', 'U', and 'D' (case-insensitive). All other characters default to the "Do Nothing" (Null Object) command.

Parameters
moveListA string containing a list of single letter commands to be parsed.
commandListA MoveCommandList object in which to return the MoveCommand objects representing the move commands parsed from the string.
Returns
Returns true if the string was successfully parsed and the MoveCommand objects were added to the command list; otherwise, returns false, indicating an out of memory condition or a null argument passed in.

Definition at line 104 of file NullObject_Exercise.c.

References MoveCommand_Create(), MoveCommandDown_Execute(), MoveCommandLeft_Execute(), MoveCommandList_Add(), MoveCommandList_Clear(), MoveCommandNone_Execute(), MoveCommandRight_Execute(), and MoveCommandUp_Execute().

Referenced by MoveProcessor_ExecuteMoveList(), and MoveProcessor_ShowMoveList().

◆ _MoveProcessor_ShowMoves()

static void _MoveProcessor_ShowMoves ( MoveCommandList commands)
static

Show the command character and name of the command for all commands in the given list of commands.

Parameters
commandsThe list of MoveCommand objects to display.

Definition at line 198 of file NullObject_Exercise.c.

References MoveCommand::commandName, MoveCommandList::commands, MoveCommandList::commands_count, MoveCommand::commandToken, and MoveCommand_Show().

Referenced by MoveProcessor_ShowMoveList().

◆ MoveCommand_Show()

static void MoveCommand_Show ( char  commandToken,
const char *  commandName 
)
static

Show the move command and its name followed by a newline.

Definition at line 20 of file NullObject_Exercise.c.

Referenced by _MoveProcessor_ShowMoves().

◆ MoveCommandDown_Execute()

static void MoveCommandDown_Execute ( void  )
static

Represents the Move Down command.

Definition at line 59 of file NullObject_Exercise.c.

Referenced by _MoveProcessor_ParseMoves().

◆ MoveCommandLeft_Execute()

static void MoveCommandLeft_Execute ( void  )
static

Represents the Move Left command.

Definition at line 32 of file NullObject_Exercise.c.

Referenced by _MoveProcessor_ParseMoves().

◆ MoveCommandNone_Execute()

static void MoveCommandNone_Execute ( void  )
static

Represents the Do Nothing command. This is the Null "Object" for this exercise.

Definition at line 69 of file NullObject_Exercise.c.

Referenced by _MoveProcessor_ParseMoves().

◆ MoveCommandRight_Execute()

static void MoveCommandRight_Execute ( void  )
static

Represents the Move Right command.

Definition at line 41 of file NullObject_Exercise.c.

Referenced by _MoveProcessor_ParseMoves().

◆ MoveCommandUp_Execute()

static void MoveCommandUp_Execute ( void  )
static

Represents the Move Up command.

Definition at line 50 of file NullObject_Exercise.c.

Referenced by _MoveProcessor_ParseMoves().

◆ MoveProcessor_ExecuteMoveList()

static void MoveProcessor_ExecuteMoveList ( const char *  moveList)
static

Parse and execute the given list of move commands, where each command is represents by a single character.

Recognizes 'U', 'D', 'L', and 'R' (case-insensitive). All other characters are assigned a "Do Nothing" (Null Object) command.

Parameters
moveListA string of characters to parse and execute.

Definition at line 218 of file NullObject_Exercise.c.

References _MoveProcessor_ExecuteMoves(), _MoveProcessor_ParseMoves(), MoveCommandList_Clear(), and MoveCommandList_Initialize().

Referenced by NullObject_Exercise().

◆ MoveProcessor_ShowMoveList()

static void MoveProcessor_ShowMoveList ( const char *  moveList)
static

Parse and display the given list of move commands, where each command is represents by a single character.

Recognizes 'U', 'D', 'L', and 'R' (case-insensitive). All other characters are assigned a "Do Nothing" (Null Object) command.

Parameters
moveListA string of characters to parse and display.

Definition at line 240 of file NullObject_Exercise.c.

References _MoveProcessor_ParseMoves(), _MoveProcessor_ShowMoves(), MoveCommandList_Clear(), and MoveCommandList_Initialize().

Referenced by NullObject_Exercise().

◆ NullObject_Exercise()

void NullObject_Exercise ( void  )

Example of using the Null Object Pattern.

The Null Object pattern is where an object or function acts as a stand-in for real commands but otherwise does nothing.

In this exercise, movement commands are presented as characters in a string, with the characters 'u', 'd', 'l', and 'r' representing the moves "up", "down", "left", and "right", respectively. To keep the processing of this string simple, all other characters in the string are assigned a Null Object ("Do Nothing") version of the move command.

This example displays the commands after parsing and then "executes" commands, which consists of printing the commands out.

This example highlights the Null Object Pattern while also utilizing the Command Pattern and Interpreter Pattern.

Definition at line 275 of file NullObject_Exercise.c.

References MoveProcessor_ExecuteMoveList(), and MoveProcessor_ShowMoveList().