Design Pattern Examples
Overview of object-oriented design patterns
Command_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9
11{
24 internal class Command_Exercise
25 {
26
30 Stack<Command> _commandUndoList = new Stack<Command>();
31
39 {
40 _commandUndoList.Push(command);
41 command.Execute();
42 }
43
50 void Command_Operation_Replace(Command_TextObject source, string searchPattern, string replaceText)
51 {
52 source.Text = source.Text.Replace(searchPattern, replaceText);
53 }
54
60 {
61 StringBuilder output = new StringBuilder();
62 string text = source.Text;
63 for (int index = 0; index < text.Length; ++index)
64 {
65 output.Append(text[text.Length - 1 - index]);
66 }
67 source.Text = output.ToString();
68 }
69
76 {
77 if (_commandUndoList.Count > 0)
78 {
79 // Reset the text to the starting point.
80 text.Reset();
81
82 // Get rid of the last command applied and remember it.
83 Command lastCommand = _commandUndoList.Pop();
84
85 // Now apply all remaining commands to the text in order.
86 foreach (Command command in _commandUndoList)
87 {
88 command.Execute();
89 }
90
91 // Show off what we (un)did.
92 Console.WriteLine(" undoing command {0,-31}: \"{1}\"", lastCommand, text);
93 }
94 }
95
104 void Command_ApplyReplaceCommand(Command_TextObject text, string searchPattern, string replaceText)
105 {
106 Command command = new Command(text, "Replace", Command_Operation_Replace, searchPattern, replaceText);
107 Command_Save_And_Execute(text, command);
108 Console.WriteLine(" command {0,-31}: \"{1}\"", command, text);
109 }
110
111
120 {
121 Command command = new Command(text, "Reverse", Command_Operation_Reverse);
122 Command_Save_And_Execute(text, command);
123 Console.WriteLine(" command {0,-31}: \"{1}\"", command, text);
124 }
125
126
130 // ! [Using Command in C#]
131 public void Run()
132 {
133 Console.WriteLine();
134 Console.WriteLine("Command Exercise");
135
136 // Start with a fresh undo list.
137 _commandUndoList.Clear();
138
139 // The base text object to work from.
140 Command_TextObject text = new Command_TextObject("This is a line of text on which to experiment.");
141
142 Console.WriteLine(" Starting text: \"{0}\"", text);
143
144 // Apply four operations to the text.
145 Command_ApplyReplaceCommand(text, "text", "painting");
146 Command_ApplyReplaceCommand(text, "on", "off");
148 Command_ApplyReplaceCommand(text, "i", "!");
149
150 Console.WriteLine(" Now perform undo until back to original");
151
152 // Now undo the four operations.
153 Command_Undo(text);
154 Command_Undo(text);
155 Command_Undo(text);
156 Command_Undo(text);
157
158 Console.WriteLine(" Final text : \"{0}\"", text);
159
160 Console.WriteLine(" Done.");
161 }
162 // ! [Using Command in C#]
163 }
164}
Example of using the Command Pattern in C#.
void Command_Operation_Replace(Command_TextObject source, string searchPattern, string replaceText)
An operation to search and replace text in a Command_TextObject.
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,...
void Command_ApplyReverseCommand(Command_TextObject text)
Helper function to create a Command object that reverses the order of the characters in the given Com...
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_Reverse(Command_TextObject source)
An operation to reverse the characters in the given Command_TextObject.
void Run()
Executes the example for the Command Pattern in C#.
void Command_Undo(Command_TextObject text)
Perform an undo on the given Command_TextObject, using the commands in the "global" undo list....
Stack< Command > _commandUndoList
The list of commands applied.
Container for a string. Need to use a class that allows the text to be changed while the container (t...
Definition: Command.cs:19
void Reset()
Resets the TextObject to the starting string.
Definition: Command.cs:54
string Text
Gets or sets the text in this TextObject.
Definition: Command.cs:40
Represents an operation that can be applied to a TextObject. This class can handle two kinds of opera...
Definition: Command.cs:112
void Execute()
Execute the command on the TextObject.
Definition: Command.cs:170
The namespace containing all Design Pattern Examples implemented in C#.