avatar

刘刚刚的blog

采菊东篱下,悠然见南山🦥

  • 首页
  • 大模型应用
  • 常用软件/工具
  • Halo
  • 关于
Home HeadFirst 设计模式--命令模式
文章

HeadFirst 设计模式--命令模式

Posted 2022-12-29 Updated 2024-09- 27
By Administrator
7~9 min read

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


命令模式

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

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()

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

Head First设计模式
设计模式
License:  CC BY 4.0
Share

Further Reading

Dec 29, 2022

HeadFirst 设计模式--命令模式

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

Dec 18, 2022

HeadFirst 设计模式--外观模式(Facade Pattern)

外观模式将一个或者数个类复杂的一切都隐藏在背后,只露出一个干净的外观

Dec 14, 2022

HeadFirst 设计模式--适配器模式(Adapter Design Pattern)

适配器模式将一个接口转化为另外一个接口。

OLDER

HeadFirst 设计模式--模板方法模式(Template Method)

NEWER

HeadFirst 设计模式--迭代器与组合模式

Recently Updated

  • 文本切分-语义分割(Semantic Chunking)
  • dify 并发配置优化
  • Typing
  • 大模型返回中json_schema与json_mode的区别
  • Async

Trending Tags

Halo 运维 postgresql 设计模式 linux就该这么学 nas rag odoo python 文本切分

Contents

©2025 刘刚刚的blog. Some rights reserved.

Using the Halo theme Chirpy