命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。
现在多数应用都有撤销操作。虽然难以想象,但在很多年里,任何软件中确实都不存在撤销
操作。撤销操作是在1974年引入的(请参考网页[t.cn/Rqr3N22]),但Fortran和Lisp分别早在1957
年和1958年就已创建了撤销操作(请参考网页[t.cn/Rqr3067]),这两门语言仍在被人广泛使用。
在那些年里,我真心不想使用应用软件。犯了一个错误,用户也没什么便捷方式能修正它。
import os verbose = True class RenameFile: def __init__(self, path_src, path_dest): self.src, self.dest = path_src, path_dest def execute(self): if verbose: print("[renaming ‘{}‘ to ‘{}‘]".format(self.src, self.dest)) os.rename(self.src, self.dest) def undo(self): if verbose: print("[renaming ‘{}‘ back to ‘{}‘]".format(self.dest, self.src)) os.rename(self.dest, self.src) class CreateFile: def __init__(self, path, txt=‘hello world\n‘): self.path, self.txt = path, txt def execute(self): if verbose: print("[creating file ‘{}‘]".format(self.path)) with open(self.path, mode=‘w‘, encoding=‘utf-8‘) as out_file: out_file.write(self.txt) def undo(self): delete_file(self.path) class ReadFile: def __init__(self, path): self.path = path def execute(self): if verbose: print("[reading file ‘{}‘]".format(self.path)) with open(self.path, mode=‘r‘, encoding=‘utf-8‘) as in_file: print(in_file.read(), end=‘‘) def delete_file(path): if verbose: print("deleting file ‘{}‘".format(path)) os.remove(path) def main(): orig_name, new_name = ‘file1‘, ‘file2‘ commands = [] for cmd in CreateFile(orig_name), ReadFile(orig_name), RenameFile(orig_name, new_name): commands.append(cmd) [c.execute() for c in commands] answer = input(‘reverse the executed commands? [y/n] ‘) if answer not in ‘yY‘: print("the result is {}".format(new_name)) exit() for c in reversed(commands): try: c.undo() except AttributeError as e: pass if __name__ == ‘__main__‘: main()