Expand description

The Null Object design pattern example module

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” the commands, which consists of printing the commands out.

Accessed through the nullobject_exercise() function.

Modules

  • Definition of the IMoveCommand trait that all move commands must implement in the “Null Object” design pattern example.
  • Contains the MoveLeft, MoveRight, MoveUp, MoveDown, and MoveNone command structs that represent the specific movement commands.
  • Contains the MoveProcessor struct that parses a string of move commands to create move command objects and then displays or executes those command objects as requested.

Functions