Design Pattern Examples
Overview of object-oriented design patterns
Command.cs
Go to the documentation of this file.
1
6
7using System;
8
10{
18 public class Command_TextObject
19 {
20 // Starting string text so we can reset the text to a known point.
22
23 // The text that can change.
24 string _text;
25
30 public Command_TextObject(string text)
31 {
32 _text = text;
33 _startingText = text;
34 }
35
39 public string Text
40 {
41 get
42 {
43 return _text;
44 }
45 set
46 {
47 _text = value;
48 }
49 }
50
54 public void Reset()
55 {
57 }
58
59
64 public override string ToString()
65 {
66 return _text;
67 }
68 }
69
70
78 public delegate void two_parameter_operation(Command_TextObject source, string argument1, string argument2);
79
85 public delegate void no_parameter_operation(Command_TextObject source);
86
87
111 public class Command
112 {
113 // The receiver of the command.
115
116 // Easy-to-read command name.
118
119 // Two parameter operation to apply to the receiver.
121
122 // No parameter operation to apply to the receiver.
124
125 // The first argument to the operation.
127
128 // The second argument to the operation.
130
140 public Command(Command_TextObject source, string commandName, two_parameter_operation operation, string argument1, string argument2)
141 {
142 _receiver = source;
143 _commandName = commandName;
144 _two_parameter_operation = operation;
146 _argument1 = argument1;
147 _argument2 = argument2;
148 }
149
157 public Command(Command_TextObject source, string commandName, no_parameter_operation operation)
158 {
159 _receiver = source;
160 _commandName = commandName;
161 _no_parameter_operation = operation;
163 _argument1 = string.Empty;
164 _argument2 = string.Empty;
165 }
166
170 public void Execute()
171 {
172 if (_two_parameter_operation != null)
173 {
175 }
176 else if (_no_parameter_operation != null)
177 {
179 }
180 }
181
186 public override string ToString()
187 {
188 string output = "<NO COMMAND>";
189 if (_two_parameter_operation != null)
190 {
191 output = String.Format("{0} \"{1}\" with \"{2}\"", _commandName, _argument1, _argument2);
192 }
193 else if (_no_parameter_operation != null)
194 {
195 output = String.Format("{0}", _commandName);
196 }
197 return output;
198 }
199 }
200}
void(* no_parameter_operation)(Command_TextObject *source)
Alias for a function type representing an operation applied to a Command_TextObject that uses no addi...
void(* two_parameter_operation)(Command_TextObject *source, const char *argument1, const char *argument2)
Alias for a function type representing an operation applied to a Command_TextObject using two paramet...
Container for a string. Need to use a class that allows the text to be changed while the container (t...
Definition: Command.cs:19
Command_TextObject(string text)
Constructs a text object with an initial string.
Definition: Command.cs:30
void Reset()
Resets the TextObject to the starting string.
Definition: Command.cs:54
override string ToString()
Converts the TextObject to a string.
Definition: Command.cs:64
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
Command(Command_TextObject source, string commandName, no_parameter_operation operation)
Constructor for a command that applies an operation to a TextObject but does not take any additional ...
Definition: Command.cs:157
Command_TextObject _receiver
Definition: Command.cs:114
override string ToString()
Convert this command to a string representation.
Definition: Command.cs:186
Command(Command_TextObject source, string commandName, two_parameter_operation operation, string argument1, string argument2)
Constructor for a command that applies an operation to a TextObject, where the operation takes two pa...
Definition: Command.cs:140
two_parameter_operation? _two_parameter_operation
Definition: Command.cs:120
no_parameter_operation? _no_parameter_operation
Definition: Command.cs:123
The namespace containing all Design Pattern Examples implemented in C#.