设计模式:命令模式(Command pattern)
命令模式是一种非常有意思的设计模式,我们可以根据该模式轻松编写动态命令链,并且可以在不修改源代码的情况下,把不同的操作组合在一起。因为在设计之初,我们就把每个命令都定义为一个独立对象,所以我们也可以很容易地实现各个对象的撤销操作。
优化前的代码
以下是一个简易的计算器程序:
class Calculator: def __init__(self): self.value = 0 def add(self, value): self.value += value def subtract(self, value): self.value -= value def multiply(self, value): self.value *= value def divide(self, value): self.value /= value calculator = Calculator() calculator.add(10) print(calculator.value) calculator.divide(2) print(calculator.value)
优化后的代码
下面我们采用命令模式,对上面代码进行重构:
class Calculator: def __init__(self): self.value = 0 self.histories = [] def executeCommand(self, command): self.value = command.execute(self.value) self.histories.append(command) def undo(self): command = self.histories.pop() self.value = command.undo(self.value) class AddCommand: def __init__(self, value): self.value = value def execute(self, currentValue): return currentValue + self.value def undo(self, currentValue): return currentValue - self.value class SubtractCommand: def __init__(self, value): self.value = value def execute(self, currentValue): return currentValue - self.value def undo(self, currentValue): return currentValue + self.value class MultiplyCommand: def __init__(self, value): self.value = value def execute(self, currentValue): return currentValue * self.value def undo(self, currentValue): return currentValue / self.value class DivideCommand: def __init__(self, value): self.value = value def execute(self, currentValue): return currentValue / self.value def undo(self, currentValue): return currentValue * self.value class AddThenDivideCommand: def __init__(self, valueToAdd, valueToDivide): self.addCommand = AddCommand(valueToAdd) self.divideCommnad = DivideCommand(valueToDivide) def execute(self, currentValue): new_value = self.addCommand.execute(currentValue) return self.divideCommnad.execute(new_value) def undo(self, currentValue): new_value = self.divideCommnad.undo(currentValue) return self.addCommand.undo(new_value) calculator = Calculator() # calculator.executeCommand(AddCommand(10)) # print(calculator.value) # calculator.executeCommand(DivideCommand(2)) # print(calculator.value) # calculator.undo() # print(calculator.value) # calculator.undo() # print(calculator.value) calculator.executeCommand(AddThenDivideCommand(10, 2)) print(calculator.value) calculator.undo() print(calculator.value)
在采用命令模式后,我们可以在不修改原来加减乘除各命令的源代码情况下,轻松组合实现各类混合四则运算和撤销操作。
我们还可以微调上面的代码,实现链式调用:
class Calculator: def __init__(self): self.value = 0 self.histories = [] def executeCommand(self, command): self.value = command.execute(self.value) self.histories.append(command) return self def undo(self): command = self.histories.pop() self.value = command.undo(self.value) return self class AddCommand: def __init__(self, value): self.value = value def execute(self, currentValue): return currentValue + self.value def undo(self, currentValue): return currentValue - self.value class SubtractCommand: def __init__(self, value): self.value = value def execute(self, currentValue): return currentValue - self.value def undo(self, currentValue): return currentValue + self.value class MultiplyCommand: def __init__(self, value): self.value = value def execute(self, currentValue): return currentValue * self.value def undo(self, currentValue): return currentValue / self.value class DivideCommand: def __init__(self, value): self.value = value def execute(self, currentValue): return currentValue / self.value def undo(self, currentValue): return currentValue * self.value calculator = Calculator() calculator.executeCommand(AddCommand(10)).executeCommand(DivideCommand(2)) print(calculator.value) calculator.undo().undo() print(calculator.value)
参考链接
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论