Design Pattern Examples
Overview of object-oriented design patterns
command_exercise.py
Go to the documentation of this file.
5
6from .command_classes import Command_TextObject, Command
7
8
9_commandUndoList = [] # type; [Command]
10
11
19def Command_Save_And_Execute(text : Command_TextObject, command : Command):
20 _commandUndoList.append(command)
21 command.Execute()
22
23
24
34def Command_Operation_Replace(source : Command_TextObject, searchPattern : str, replaceText : str) -> None:
35 source.Text = source.Text.replace(searchPattern, replaceText)
36
37
38
44def Command_Operation_Reverse(source : Command_TextObject):
45 text_in_list = [c for c in source.Text]
46 text_in_list.reverse()
47 output = "".join(text_in_list)
48 source.Text = output
49
50
51
58def Command_Undo(text : Command_TextObject) -> None:
59 if _commandUndoList:
60 # Reset the text to the starting point.
61 text.Reset()
62
63 # Get rid of the last command applied and remember it.
64 lastCommand = _commandUndoList.pop(len(_commandUndoList) - 1)
65
66 # Now apply all remaining commands to the text in order
67 # (oldest to newest).
68 for command in _commandUndoList:
69 command.Execute()
70
71 # Show off what we (un)did.
72 print(" undoing command {0:<31}==> \"{1}\"".format(lastCommand.ToString(), text.ToString()))
73
74
75
89def Command_ApplyReplaceCommand(text : Command_TextObject, searchPattern : str, replaceText : str) -> None:
90 command = Command(text, "Replace", Command_Operation_Replace, searchPattern, replaceText)
91 Command_Save_And_Execute(text, command)
92 print(" command {0:<31}==> \"{1}\"".format(command.ToString(), text.ToString()))
93
94
95
105def Command_ApplyReverseCommand(text : Command_TextObject) -> None:
106 command = Command(text, "Reverse", Command_Operation_Reverse)
107 Command_Save_And_Execute(text, command)
108 print(" command {0:<31}==> \"{1}\"".format(command.ToString(), text.ToString()))
109
110
111
121
122# ! [Using Command in Python]
124 print()
125 print("Command Exercise")
126
127 # The base text object to work from.
128 text = Command_TextObject("This is a line of text on which to experiment.")
129
130 print(" Starting text: \"{0}\"".format(text.ToString()))
131
132 # Apply four operations to the text.
133 Command_ApplyReplaceCommand(text, "text", "painting")
134 Command_ApplyReplaceCommand(text, "on", "off")
136 Command_ApplyReplaceCommand(text, "i", "!")
137
138 print(" Now perform undo until back to original")
139
140 # Now undo the four operations.
141 Command_Undo(text)
142 Command_Undo(text)
143 Command_Undo(text)
144 Command_Undo(text)
145
146 print(" Final text : \"{0}\"".format(text.ToString()))
147
148 print(" Done.")
149# ! [Using Command in Python]
Represents an operation that can be applied to a TextObject.
None Command_Operation_Replace(Command_TextObject source, str searchPattern, str replaceText)
An operation to search and replace text in a Command_TextObject.
def Command_Operation_Reverse(Command_TextObject source)
An operation to reverse the characters in the given Command_TextObject.
def Command_Exercise()
Example of using the Command Pattern.
None Command_ApplyReverseCommand(Command_TextObject text)
Helper function to create a Command object that reverses the order of the characters in the given Com...
None Command_Undo(Command_TextObject text)
Perform an undo on the given Command_TextObject, using the commands in the "global" undo list.
None Command_ApplyReplaceCommand(Command_TextObject text, str searchPattern, str replaceText)
Helper function to create a Command object that replaces text in the given Command_TextObject,...
def 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.