刘刚刚的个人博客

HeadFirst 设计模式--命令模式

创建时间:2022-11-29 00:09:41
更新时间:2022-11-29 00:09:41

命令模式通过封装调用把方法封装起来。

命令模式

命令模式通过封装调用把方法封装起来。

headerfirst中通过餐厅中顾客点餐的流程介绍了命令模式。

顾客(client)在菜单(setcommond)中点好菜(command),交给服务员(receiver),服务员进行下单操作(orderup),厨师根据菜单进行制作(excute)。

使用代码实现了一套遥控器的代码。遥控器可以控制多个设备,每个设备有两个按钮(1个开,1个关),同时额外有一个撤销(undo)按钮.

from abc import ABC, abstractmethod


class Command(ABC):
    @abstractmethod
    def execute(self):
        pass

    @abstractmethod
    def undo(self):
        pass


# 空的命令不会做任何事情
class NoCommand(Command):

    def execute(self):
        pass

    def undo(self):
        pass


class RemoteControl:
    on_commands = []
    off_commands = []
    undo_commands = None

    def __init__(self):
        no_command = NoCommand()

        for i in range(0, 7):
            self.on_commands.append(no_command)
            self.off_commands.append(no_command)

        self.undo_commands = no_command

    def set_commands(self, slot, on_command, off_command):
        '''
        设置每个位置的开关的命令
        :param slot:
        :param on_command:
        :param off_command:
        :return:
        '''
        self.on_commands[slot] = on_command
        self.off_commands[slot] = off_command

    def on_button_was_pushed(self, slot):
        '''
        按钮被按下,执行相应位置的对象的方法
        :param slot:
        :return:
        '''
        self.on_commands[slot].execute()
        self.undo_commands = self.on_commands[slot]

    def off_button_was_pushed(self, slot):
        self.off_commands[slot].execute()
        self.undo_commands = self.off_commands[slot]

    def undo(self):
        self.undo_commands.undo()


class Light:
    name = None

    def __init__(self, name):
        self.name = name

    def on(self):
        print(f"{self.name}打开")

    def off(self):
        print(f"{self.name}关闭")


class LightOnCommand(Command):
    light = None

    def __init__(self, light):
        self.light = light

    def execute(self):
        self.light.on()

    def undo(self):
        self.light.off()


class LightOffCommand(Command):
    light = None

    def __init__(self, light):
        self.light = light

    def execute(self):
        self.light.off()

    def undo(self):
        self.light.on()


class PartyCommand(Command):
    commands = []

    def __init__(self, commands: list):
        self.commands = commands

    def execute(self):
        for i in self.commands:
            i.execute()

    def undo(self):
        for i in reversed(self.commands):
            i.undo()


# 测试开,关,撤销功能
light = Light("客厅的灯")
control = RemoteControl()

LightOnCommand(light)
control.set_commands(0, LightOnCommand(light), LightOffCommand(light))
control.on_button_was_pushed(0)
control.off_button_was_pushed(0)
control.undo()

print("---批量测试---")
# 测试批量执行
light2 = Light("卧室的灯")
party_on_commands = PartyCommand([LightOnCommand(light), LightOnCommand(light2)])
party_off_commands = PartyCommand([LightOffCommand(light), LightOffCommand(light2)])
control.set_commands(1, party_on_commands, party_off_commands)
control.on_button_was_pushed(1)
control.off_button_was_pushed(1)
control.undo()

>>>
客厅的灯打开
客厅的灯关闭
客厅的灯打开
---批量测试---
客厅的灯打开
卧室的灯打开
客厅的灯关闭
卧室的灯关闭
卧室的灯打开
客厅的灯打开
我的名片

昵称:shuta

职业:后台开发(python、php)

邮箱:648949076@qq.com

站点信息

建站时间: 2020/2/19
网站程序: ANTD PRO VUE + TP6.0
晋ICP备18007778号