Design Pattern Examples
Overview of object-oriented design patterns
Command_Exercise Class Reference

Example of using the Command Pattern in C#. More...

Collaboration diagram for Command_Exercise:
Collaboration graph

Public Member Functions

void Run ()
 Executes the example for the Command Pattern in C#.
 

Private Member Functions

void Command_Save_And_Execute (Command_TextObject text, Command command)
 Save the given command on the undo list then execute the command on the given text object.
 
void Command_Operation_Replace (Command_TextObject source, string searchPattern, string replaceText)
 An operation to search and replace text in a Command_TextObject.
 
void Command_Operation_Reverse (Command_TextObject source)
 An operation to reverse the characters in the given Command_TextObject.
 
void Command_Undo (Command_TextObject text)
 Perform an undo on the given Command_TextObject, using the commands in the "global" undo list. If the undo list is empty, nothing happens.
 
void Command_ApplyReplaceCommand (Command_TextObject text, string searchPattern, string replaceText)
 Helper function to create a Command object that replaces text in the given Command_TextObject, adds the command to the undo list and then applies the command to the Command_TextObject. Finally, it shows off what was done.
 
void Command_ApplyReverseCommand (Command_TextObject text)
 Helper function to create a Command object that reverses the order of the characters in the given Command_TextObject, adds the command to the undo list and then applies the command to the Command_TextObject. Finally, it shows what was done.
 

Private Attributes

Stack< Command_commandUndoList = new Stack<Command>()
 The list of commands applied.
 

Detailed Description

Example of using the Command Pattern in C#.

The Command pattern is used to encapsulate an operation or command associated with an object so that the command can be applied to the object at a later time.

In this exercise, an undo list is implemented using Commands that associate commands defined in this file with a text object. The commands are applied to the text object in succession then effectively undone.

Definition at line 24 of file Command_Exercise.cs.

Member Function Documentation

◆ Command_ApplyReplaceCommand()

void Command_ApplyReplaceCommand ( Command_TextObject  text,
string  searchPattern,
string  replaceText 
)
inlineprivate

Helper function to create a Command object that replaces text in the given Command_TextObject, adds the command to the undo list and then applies the command to the Command_TextObject. Finally, it shows off what was done.

Parameters
textThe Command_TextObject to affect.
searchPatternWhat to look for in the Command_TextObject.
replaceTextWhat to replace the searchPattern with.

Definition at line 104 of file Command_Exercise.cs.

References Command_Exercise.Command_Operation_Replace(), and Command_Exercise.Command_Save_And_Execute().

Referenced by Command_Exercise.Run().

◆ Command_ApplyReverseCommand()

void Command_ApplyReverseCommand ( Command_TextObject  text)
inlineprivate

Helper function to create a Command object that reverses the order of the characters in the given Command_TextObject, adds the command to the undo list and then applies the command to the Command_TextObject. Finally, it shows what was done.

Parameters
textThe Command_TextObject to affect.

Definition at line 119 of file Command_Exercise.cs.

References Command_Exercise.Command_Operation_Reverse(), and Command_Exercise.Command_Save_And_Execute().

Referenced by Command_Exercise.Run().

◆ Command_Operation_Replace()

void Command_Operation_Replace ( Command_TextObject  source,
string  searchPattern,
string  replaceText 
)
inlineprivate

An operation to search and replace text in a Command_TextObject.

Parameters
sourceThe Command_TextObject to affect.
searchPatternWhat to look for in the Command_TextObject.
replaceTextWhat to replace the searchPattern with.

Definition at line 50 of file Command_Exercise.cs.

References Command_TextObject.Text.

Referenced by Command_Exercise.Command_ApplyReplaceCommand().

◆ Command_Operation_Reverse()

void Command_Operation_Reverse ( Command_TextObject  source)
inlineprivate

An operation to reverse the characters in the given Command_TextObject.

Parameters
sourceThe Command_TextObject to affect.

Definition at line 59 of file Command_Exercise.cs.

References Command_TextObject.Text.

Referenced by Command_Exercise.Command_ApplyReverseCommand().

◆ Command_Save_And_Execute()

void Command_Save_And_Execute ( Command_TextObject  text,
Command  command 
)
inlineprivate

Save the given command on the undo list then execute the command on the given text object.

Parameters
textThe Command_TextObject on which to apply the command.
commandThe Command object to apply to the text object.

Definition at line 38 of file Command_Exercise.cs.

References Command_Exercise._commandUndoList, and Command.Execute().

Referenced by Command_Exercise.Command_ApplyReplaceCommand(), and Command_Exercise.Command_ApplyReverseCommand().

◆ Command_Undo()

void Command_Undo ( Command_TextObject  text)
inlineprivate

Perform an undo on the given Command_TextObject, using the commands in the "global" undo list. If the undo list is empty, nothing happens.

Parameters
textThe Command_TextObject to affect.

Definition at line 75 of file Command_Exercise.cs.

References Command_Exercise._commandUndoList, Command.Execute(), and Command_TextObject.Reset().

Referenced by Command_Exercise.Run().

◆ Run()

Member Data Documentation

◆ _commandUndoList

Stack<Command> _commandUndoList = new Stack<Command>()
private

The list of commands applied.

Definition at line 30 of file Command_Exercise.cs.

Referenced by Command_Exercise.Command_Save_And_Execute(), Command_Exercise.Command_Undo(), and Command_Exercise.Run().


The documentation for this class was generated from the following file: