Design Pattern Examples
Overview of object-oriented design patterns
Null_Object.cs
Go to the documentation of this file.
1
7
8using System;
9using System.Collections.Generic;
10
12{
24 public abstract class MoveCommand
25 {
29 public string Name { get; private set; }
30
36 public string Command { get; private set; }
37
38
44 public MoveCommand(string command, string commandName)
45 {
46 Command = command;
47 Name = commandName;
48 }
49
50
54 public void Show()
55 {
56 Console.WriteLine(" '{0}' -> {1}", Command, Name);
57 }
58
59
63 public abstract void Execute();
64 }
65
66
67 //########################################################################
68 //########################################################################
69
70
75 {
81 public MoveCommandLeft(string command) : base(command, "Left")
82 {
83 }
84
85
89 public override void Execute()
90 {
91 Console.Write("move left");
92 }
93 }
94
95
96 //########################################################################
97 //########################################################################
98
99
104 {
110 public MoveCommandRight(string command) : base(command, "Right")
111 {
112 }
113
114
118 public override void Execute()
119 {
120 Console.Write("move right");
121 }
122 }
123
124
125 //########################################################################
126 //########################################################################
127
128
133 {
139 public MoveCommandUp(string command) : base(command, "Up")
140 {
141 }
142
143
147 public override void Execute()
148 {
149 Console.Write("move up");
150 }
151 }
152
153
154 //########################################################################
155 //########################################################################
156
157
162 {
168 public MoveCommandDown(string command) : base(command, "Down")
169 {
170 }
171
172
176 public override void Execute()
177 {
178 Console.Write("move down");
179 }
180 }
181
182
183 //########################################################################
184 //########################################################################
185
186
192 {
198 public MoveCommandNone(string command) : base(command, "None")
199 {
200 }
201
202
206 public override void Execute()
207 {
208 }
209 }
210
211
212 //########################################################################
213 //########################################################################
214
215
231 public class MoveProcessor
232 {
233 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
234 // Private methods.
235
249 private List<MoveCommand> _ParseMoves(string moveList)
250 {
251 List<MoveCommand> commands = new List<MoveCommand>();
252 foreach (char command in moveList)
253 {
254 string testCommand = command.ToString().ToUpper();
255 MoveCommand moveCommand;
256
257 switch (testCommand)
258 {
259 case "U":
260 moveCommand = new MoveCommandUp(command.ToString());
261 break;
262
263 case "D":
264 moveCommand = new MoveCommandDown(command.ToString());
265 break;
266
267 case "L":
268 moveCommand = new MoveCommandLeft(command.ToString());
269 break;
270
271 case "R":
272 moveCommand = new MoveCommandRight(command.ToString());
273 break;
274
275 default:
276 // Everything else is a "do nothing" command.
277 moveCommand = new MoveCommandNone(command.ToString());
278 break;
279 }
280 commands.Add(moveCommand);
281 }
282
283 return commands;
284 }
285
286
301 private void _ExecuteMoves(List<MoveCommand> commands)
302 {
303 foreach(MoveCommand command in commands)
304 {
305 Console.Write("<");
306 command.Execute();
307 Console.Write("> ");
308 }
309 Console.WriteLine();
310 }
311
312
320 private void _ShowMoves(List<MoveCommand> commands)
321 {
322 foreach(MoveCommand command in commands)
323 {
324 command.Show();
325 }
326 }
327
328 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
329 // Public methods.
330
339 public void ExecuteMoveList(string moveList)
340 {
341 List<MoveCommand> commands = _ParseMoves(moveList);
342 _ExecuteMoves(commands);
343 }
344
345
354 public void ShowMoveList(string moveList)
355 {
356 List<MoveCommand> commands = _ParseMoves(moveList);
357 _ShowMoves(commands);
358 }
359 }
360}
Represents an operation that can be applied to a TextObject. This class can handle two kinds of opera...
Definition: Command.cs:112
Represents the Move Down command.
Definition: Null_Object.cs:162
MoveCommandDown(string command)
Constructor.
Definition: Null_Object.cs:168
override void Execute()
Executes the move down command.
Definition: Null_Object.cs:176
Base class that represents a move command. A move command has a name and the command character that r...
Definition: Null_Object.cs:25
MoveCommand(string command, string commandName)
Constructor.
Definition: Null_Object.cs:44
string Name
Name of the command (assigned in the class constructor).
Definition: Null_Object.cs:29
abstract void Execute()
Execute the command. Derived classes must implement this.
void Show()
Display the move command and its name followed by a newline.
Definition: Null_Object.cs:54
Represents the Move Left command.
Definition: Null_Object.cs:75
override void Execute()
Executes the move left command.
Definition: Null_Object.cs:89
MoveCommandLeft(string command)
Constructor.
Definition: Null_Object.cs:81
Represents the Do Nothing command. This is the Null Object for this exercise.
Definition: Null_Object.cs:192
MoveCommandNone(string command)
Constructor.
Definition: Null_Object.cs:198
override void Execute()
Does nothing when executed (this is the Null Object, after all).
Definition: Null_Object.cs:206
Represents the Move Right command.
Definition: Null_Object.cs:104
MoveCommandRight(string command)
Constructor.
Definition: Null_Object.cs:110
override void Execute()
Executes the move right command.
Definition: Null_Object.cs:118
Represents the Move Up command.
Definition: Null_Object.cs:133
MoveCommandUp(string command)
Constructor.
Definition: Null_Object.cs:139
override void Execute()
Executes the move up command.
Definition: Null_Object.cs:147
Represents the processor that translates the move list into a list of MoveCommand objects then either...
Definition: Null_Object.cs:232
void _ShowMoves(List< MoveCommand > commands)
Display the command character and name of the command for each command in the given list of commands.
Definition: Null_Object.cs:320
void ShowMoveList(string moveList)
Parse and display the given list of move commands, where each command is represents by a single chara...
Definition: Null_Object.cs:354
List< MoveCommand > _ParseMoves(string moveList)
Helper method to convert a list of single letter commands into a list of MoveCommand objects.
Definition: Null_Object.cs:249
void _ExecuteMoves(List< MoveCommand > commands)
Helper method to execute all the given commands.
Definition: Null_Object.cs:301
void ExecuteMoveList(string moveList)
Parse and execute the given list of move commands, where each command is represents by a single chara...
Definition: Null_Object.cs:339
The namespace containing all Design Pattern Examples implemented in C#.