GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||
GeezerVison is showing www.runoob.com — Python 命令模式 | 菜鸟教程
Python 设计模式
Python 命令模式命令模式是一种行为设计模式,它将一个请求封装成一个对象,从而使你可以用不同的请求对客户进行参数化。简单来说,命令模式把操作(比如打开文件、保存数据)包装成独立的对象,让你能够像处理数据一样处理操作。 生活中的类比想象一下餐厅的点餐流程:
这种分工明确的模式就是命令模式在现实生活中的体现。 为什么需要命令模式传统方式的局限性在传统编程中,我们通常直接调用对象的方法: 实例
class Light:
def turn_on(self): print("灯亮了") def turn_off(self): print("灯灭了") # 直接调用 light = Light() light.turn_on() light.turn_off() 这种方式存在几个问题:
命令模式的优势
命令模式通过引入中间层(命令对象)解决了上述问题,提供了:
命令模式的核心组件基本结构命令模式包含四个核心角色:
完整代码示例基础实现让我们通过一个智能家居的灯光控制例子来理解命令模式: 实例
from abc import ABC, abstractmethod
from typing import List # 1. 命令接口 class Command(ABC): @abstractmethod def execute(self): pass @abstractmethod def undo(self): pass # 2. 接收者 - 灯光设备 class Light: def turn_on(self): print("💡 灯光已打开") def turn_off(self): print("💡 灯光已关闭") # 3. 具体命令 - 开灯命令 class LightOnCommand(Command): def __init__(self, light: Light): self.light = light def execute(self): self.light.turn_on() def undo(self): self.light.turn_off() # 4. 具体命令 - 关灯命令 class LightOffCommand(Command): def __init__(self, light: Light): self.light = light def execute(self): self.light.turn_off() def undo(self): self.light.turn_on() # 5. 调用者 - 遥控器 class RemoteControl: def __init__(self): self.command = None self.history: List[Command] = [] def set_command(self, command: Command): self.command = command def press_button(self): if self.command: self.command.execute() self.history.append(self.command) def press_undo(self): if self.history: last_command = self.history.pop() last_command.undo() # 客户端代码 if __name__ == "__main__": # 创建设备 living_room_light = Light() # 创建命令 light_on = LightOnCommand(living_room_light) light_off = LightOffCommand(living_room_light) # 创建遥控器 remote = RemoteControl() # 测试开灯 print("=== 测试开灯 ===") remote.set_command(light_on) remote.press_button() # 测试关灯 print("\n=== 测试关灯 ===") remote.set_command(light_off) remote.press_button() # 测试撤销 print("\n=== 测试撤销操作 ===") remote.press_undo() # 撤销关灯,应该开灯 remote.press_undo() # 撤销开灯,应该关灯 运行结果:
高级应用示例支持多设备的万能遥控器让我们扩展之前的例子,创建一个支持多种设备的万能遥控器: 实例
# 新增设备 - 电视
class TV: def __init__(self, location: str): self.location = location self.is_on = False self.volume = 50 def turn_on(self): self.is_on = True print(f"📺 {self.location}电视已打开") def turn_off(self): self.is_on = False print(f"📺 {self.location}电视已关闭") def set_volume(self, volume: int): self.volume = volume print(f"📺 {self.location}电视音量设置为 {volume}") # 电视相关命令 class TVOnCommand(Command): def __init__(self, tv: TV): self.tv = tv self.previous_volume = 50 def execute(self): self.previous_volume = self.tv.volume self.tv.turn_on() def undo(self): self.tv.turn_off() self.tv.volume = self.previous_volume class TVVolumeUpCommand(Command): def __init__(self, tv: TV): self.tv = tv self.previous_volume = 50 def execute(self): self.previous_volume = self.tv.volume self.tv.set_volume(min(100, self.tv.volume + 10)) def undo(self): self.tv.set_volume(self.previous_volume) # 宏命令 - 一键执行多个命令 class MacroCommand(Command): def __init__(self, commands: List[Command]): self.commands = commands def execute(self): for command in self.commands: command.execute() def undo(self): # 按相反顺序撤销 for command in reversed(self.commands): command.undo() # 改进的遥控器 class AdvancedRemoteControl: def __init__(self, slot_count: int = 4): self.on_commands: List[Command] = [None] * slot_count self.off_commands: List[Command] = [None] * slot_count self.history: List[Command] = [] def set_command(self, slot: int, on_command: Command, off_command: Command): self.on_commands[slot] = on_command self.off_commands[slot] = off_command def press_on_button(self, slot: int): if self.on_commands[slot]: self.on_commands[slot].execute() self.history.append(self.on_commands[slot]) def press_off_button(self, slot: int): if self.off_commands[slot]: self.off_commands[slot].execute() self.history.append(self.off_commands[slot]) def press_undo(self): if self.history: last_command = self.history.pop() last_command.undo() # 测试高级遥控器 def test_advanced_remote(): print("=== 测试万能遥控器 ===") # 创建设备 living_room_light = Light() bedroom_tv = TV("卧室") # 创建命令 light_on = LightOnCommand(living_room_light) light_off = LightOffCommand(living_room_light) tv_on = TVOnCommand(bedroom_tv) tv_volume_up = TVVolumeUpCommand(bedroom_tv) # 创建宏命令 - 影院模式 cinema_mode = MacroCommand([light_off, tv_on, tv_volume_up]) # 设置遥控器 remote = AdvancedRemoteControl() remote.set_command(0, light_on, light_off) # 槽位0:灯光控制 remote.set_command(1, tv_on, TVOnCommand(bedroom_tv)) # 槽位1:电视控制 remote.set_command(2, cinema_mode, light_on) # 槽位2:影院模式 # 测试各种功能 print("\n1. 打开灯光:") remote.press_on_button(0) print("\n2. 开启影院模式:") remote.press_on_button(2) print("\n3. 撤销操作:") remote.press_undo() if __name__ == "__main__": test_advanced_remote() 命令模式的参数和选项命令接口的扩展在实际应用中,命令接口可以根据需要扩展更多功能: 实例
class AdvancedCommand(ABC):
@abstractmethod def execute(self): """执行命令""" pass @abstractmethod def undo(self): """撤销命令""" pass @abstractmethod def redo(self): """重做命令""" pass @abstractmethod def can_execute(self) -> bool: """检查命令是否可以执行""" pass @abstractmethod def get_description(self) -> str: """获取命令描述""" pass 命令队列和延迟执行命令模式天然支持命令队列,这在很多场景下非常有用: 实例
class CommandQueue:
def __init__(self): self.queue: List[Command] = [] def add_command(self, command: Command): self.queue.append(command) def process_commands(self): while self.queue: command = self.queue.pop(0) if command.can_execute(): command.execute() def clear(self): self.queue.clear() 实践练习练习 1:实现空调控制系统尝试实现一个空调控制系统的命令模式: 实例
# 你的代码在这里
class AirConditioner: def __init__(self): self.temperature = 26 self.is_on = False def turn_on(self): # 实现打开空调 pass def turn_off(self): # 实现关闭空调 pass def set_temperature(self, temp: int): # 实现设置温度 pass # 实现空调相关的命令类 # AirConditionerOnCommand # AirConditionerOffCommand # TemperatureUpCommand # TemperatureDownCommand 练习 2:实现文本编辑器的撤销功能创建一个简单的文本编辑器,支持输入文本和撤销操作: 实例
class TextEditor:
def __init__(self): self.content = "" def add_text(self, text: str): # 实现添加文本 pass def delete_text(self, length: int): # 实现删除文本 pass # 实现文本编辑命令 # AddTextCommand # DeleteTextCommand 常见问题解答Q: 命令模式适用于什么场景?A: 命令模式特别适合以下场景:
Q: 命令模式的缺点是什么?A: 主要缺点包括:
Q: 如何选择是否使用命令模式?A: 考虑以下几点:
总结命令模式通过将操作封装成对象,实现了调用者与接收者的解耦,提供了强大的扩展能力。虽然会增加一些代码复杂度,但在需要撤销/重做、操作队列、日志记录等场景下,命令模式是不可或缺的设计模式。 关键要点:
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
| |||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |