设计模式:命令模式(Command pattern)

发布于 2025-01-31 18:01:03 字数 4525 浏览 7 评论 0

命令模式是一种非常有意思的设计模式,我们可以根据该模式轻松编写动态命令链,并且可以在不修改源代码的情况下,把不同的操作组合在一起。因为在设计之初,我们就把每个命令都定义为一个独立对象,所以我们也可以很容易地实现各个对象的撤销操作。

优化前的代码

以下是一个简易的计算器程序:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

A君

暂无简介

文章
评论
28 人气
更多

推荐作者

冰魂雪魄

文章 0 评论 0

qq_Wl4Sbi

文章 0 评论 0

柳家齐

文章 0 评论 0

无法言说的痛

文章 0 评论 0

魄砕の薆

文章 0 评论 0

盗琴音

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文