命令设计模式帮助我们将一个操作(撤销、重做、复制、粘贴等)封装成一个对象,通常是创建一个包含Operation所有逻辑和方法的类。
当我们去餐馆吃饭时,会叫服务员来点单。他们用来做记录的账单(通常是纸质的)就是命令模式的一个例子。在记录好订单后,服务员将其放入账单队列,厨师会照着单子去做。每个账单都是独立的,并且可用来执行许多不同命令,例如,一个命令对应一个将要烹饪的菜品。
通过命令模式可以控制命令的执行时间和过程,还可以用来组织事务。
这里用一些文件操作类来说明命令模式的使用:
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()