浏览代码

Simplified example

(Caution: UNTESTED!)
The example made little sense. 

The Undo stack was used to run the commands, not just undo them. Renamed.

The commands had names, for no reason. They are now added directly to the stack.
The stack was edited (popped) while it was being iterated. The cmd variable was unused. Replaced with simple iterator.

The stack should be undone in reverse order. reversed() added.

The comments had grammatical errors. Corrected.
Julian 12 年之前
父节点
当前提交
846937331a
共有 1 个文件被更改,包括 10 次插入12 次删除
  1. 10 12
      command.py

+ 10 - 12
command.py

@@ -19,19 +19,17 @@ class MoveFileCommand(object):
 
 
 if __name__ == "__main__":
-    undo_stack = []
-    ren1 = MoveFileCommand('foo.txt', 'bar.txt')
-    ren2 = MoveFileCommand('bar.txt', 'baz.txt')
+    command_stack = []
 
     # commands are just pushed into the command stack
-    for cmd in ren1, ren2:
-        undo_stack.append(cmd)
+    command_stack.append(MoveFileCommand('foo.txt', 'bar.txt'))
+    command_stack.append(MoveFileCommand('bar.txt', 'baz.txt'))
 
-    # they can be executed later on will
-    for cmd in undo_stack:
-        cmd.execute()     # foo.txt is now renamed to baz.txt
 
-    # and can also be undone on will
-    for cmd in undo_stack:
-        undo_stack.pop().undo()  # Now it's bar.txt
-        undo_stack.pop().undo()  # and back to foo.txt
+    # they can be executed later on
+    for cmd in command_stack:
+        cmd.execute()
+
+    # and can also be undone at will
+    for cmd in reversed(command_stack):
+        cmd.undo()