Example of using the Command Pattern in C#. More...
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. | |
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.
|
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.
text | The Command_TextObject to affect. |
searchPattern | What to look for in the Command_TextObject. |
replaceText | What 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().
|
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.
text | The 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().
|
inlineprivate |
An operation to search and replace text in a Command_TextObject.
source | The Command_TextObject to affect. |
searchPattern | What to look for in the Command_TextObject. |
replaceText | What to replace the searchPattern with. |
Definition at line 50 of file Command_Exercise.cs.
References Command_TextObject.Text.
Referenced by Command_Exercise.Command_ApplyReplaceCommand().
|
inlineprivate |
An operation to reverse the characters in the given Command_TextObject.
source | The Command_TextObject to affect. |
Definition at line 59 of file Command_Exercise.cs.
References Command_TextObject.Text.
Referenced by Command_Exercise.Command_ApplyReverseCommand().
|
inlineprivate |
Save the given command on the undo list then execute the command on the given text object.
text | The Command_TextObject on which to apply the command. |
command | The 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().
|
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.
text | The 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().
|
inline |
Executes the example for the Command Pattern in C#.
Definition at line 131 of file Command_Exercise.cs.
References Command_Exercise._commandUndoList, Command_Exercise.Command_ApplyReplaceCommand(), Command_Exercise.Command_ApplyReverseCommand(), and Command_Exercise.Command_Undo().
Referenced by Program.Run().
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().