Design Pattern Examples
Overview of object-oriented design patterns
null_object.py
Go to the documentation of this file.
9
10from abc import ABC, abstractmethod
11
12
21class MoveCommand(ABC):
22
23
29 def __init__(self, command : str, commandName : str) -> None:
30 self._name = commandName
31 self._command = command
32
33
37
38
39
41 def Show(self) -> None:
42 print(" '{0}' -> {1}".format(self._command, self._name))
43
44
45
46 @abstractmethod
47 def Execute(self) -> None:
48 pass
49
50
51
53
54
55
56class MoveCommandLeft(MoveCommand):
57
61 def __init__(self, command : str) -> None:
62 super().__init__(command, "Left")
63
64
65
66 def Execute(self) -> None:
67 print("move left", end="")
68
69
70
72
73
74
76
80 def __init__(self, command : str) -> None:
81 super().__init__(command, "Right")
82
83
84
85 def Execute(self):
86 print("move right", end="")
87
88
89
91
92
93
95
99 def __init__(self, command : str) -> None :
100 super().__init__(command, "Up")
101
102
103
104 def Execute(self) -> None:
105 print("move up", end="")
106
107
108
110
111
112
114
118 def __init__(self, command : str) -> None:
119 super().__init__(command, "Down")
120
121
122
123 def Execute(self) -> None:
124 print("move down", end="")
125
126
127
129
130
131
134
138 def __init__(self, command: str) -> None:
139 super().__init__(command, "None")
140
141
142
143 def Execute(self) -> str:
144 pass
145
146
147
149
150
151
163class MoveProcessor:
164
165 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
166 # Private methods.
167
168
180 def _ParseMoves(self, moveList : str) -> list[MoveCommand]:
181 commands = [] # type: list[MoveCommand]
182 for index in range(0, len(moveList)):
183 commandChar = moveList[index].upper()
184 moveCommand = None
185
186 match (commandChar):
187 case 'U':
188 moveCommand = MoveCommandUp(commandChar)
189
190 case 'D':
191 moveCommand = MoveCommandDown(commandChar)
192
193 case 'L':
194 moveCommand = MoveCommandLeft(commandChar)
195
196 case 'R':
197 moveCommand = MoveCommandRight(commandChar)
198
199 case _:
200 # Everything else is a "do nothing" command.
201 moveCommand = MoveCommandNone(commandChar)
202
203 commands.append(moveCommand);
204
205 return commands;
206
207
208
221 def _ExecuteMoves(self, commands : list[MoveCommand]) -> None:
222 for command in commands:
223 print("<", end="")
224 command.Execute()
225 print("> ", end="")
226 print()
227
228
229
234 def _ShowMoves(self, commands : list[MoveCommand]) -> None:
235 for command in commands:
236 command.Show()
237
238
239 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
240 # Public methods.
241
242
250 def ExecuteMoveList(self, moveList: str) -> None:
251 commands = self._ParseMoves(moveList)
252 self._ExecuteMoves(commands)
253
254
255
263 def ShowMoveList(self, moveList : str) -> None:
264 commands = self._ParseMoves(moveList)
265 self._ShowMoves(commands)
Base class that represents a move command.
Definition: null_object.py:21
None Show(self)
Display the move command and its name followed by a newline.
Definition: null_object.py:41
None __init__(self, str command, str commandName)
Constructor.
Definition: null_object.py:29
str Execute(self)
Does nothing when executed (this is the Null Object, after all).
Definition: null_object.py:143
None ShowMoveList(self, str moveList)
Parse and display the given list of move commands, where each command is represents by a single chara...
Definition: null_object.py:263
list[MoveCommand] _ParseMoves(self, str moveList)
Helper method to convert a list of single letter commands into a list of MoveCommand objects.
Definition: null_object.py:180
None _ShowMoves(self, list[MoveCommand] commands)
Display the command character and name of the command for each command in the given list of commands.
Definition: null_object.py:234
None ExecuteMoveList(self, str moveList)
Parse and execute the given list of move commands, where each command is represents by a single chara...
Definition: null_object.py:250
None _ExecuteMoves(self, list[MoveCommand] commands)
Helper method to execute all the given commands.
Definition: null_object.py:221
Represents a move command. A move command has a name and the command character that represents the co...