Design Pattern Examples
Overview of object-oriented design patterns
command_classes.py
Go to the documentation of this file.
7
8
14
15
17
18
19 @property
20 def Text(self) -> str:
21 return self._text
22
23
24 @Text.setter
25 def Text(self, value : str):
26 self._text = value
27
28
29
30
31
35 def __init__(self, text : str):
36 self._text = text
37 self._startingText = text
38
39
43
44
45 def Reset(self):
46 self._text = self._startingText
47
48
52 def ToString(self) -> str:
53 return self._text
54
55
56
75class Command:
76
77
88 def __init__(self, source : Command_TextObject, commandName : str, operation, *arguments) -> None:
89 self._receiver = source
90 self._commandName = commandName
91 self._argument1 = None
92 self._argument2 = None
95 if len(arguments) == 2:
96 self._argument1 = arguments[0]
97 self._argument2 = arguments[1]
98 self._two_parameter_operation = operation
99 else:
100 self._no_parameter_operation = operation
101
102
103
115
116
117
118 def Execute(self):
121 else:
123
124
128 def ToString(self) -> str:
129 output = "<NO COMMAND>"
131 output = "{0} \"{1}\" with \"{2}\"".format(self._commandName, self._argument1, self._argument2)
132 elif self._no_parameter_operation:
133 output = self._commandName
134 return output
_startingText
Starting string text so we can reset the text to a known point.
def Reset(self)
Resets the TextObject to the starting string.
def __init__(self, str text)
Constructs a text object with an initial string.
Represents an operation that can be applied to a TextObject.
str ToString(self)
Return this command as a string representation.
def Execute(self)
Execute the command on the TextObject.
None __init__(self, Command_TextObject source, str commandName, operation, *arguments)
Constructor for a command that applies an operation to a TextObject, where the operation takes zero o...
_argument2
The second argument to the two-parameter operation.
_argument1
The first argument to the two-parameter operation.
_two_parameter_operation
Two parameter operation to apply to the receiver.
_no_parameter_operation
No parameter operation to apply to the receiver.