【设计模式-命令】

定义

命令模式(Command Pattern)是一种行为设计模式,它将请求封装为一个对象,从而使您能够使用不同的请求、排队请求或记录请求,并支持可撤销的操作。该模式通过将请求与其执行分离,使得请求者和接收者之间的耦合度降低。

UML图

  • Command(命令接口):定义一个接口,通常包含一个 execute() 方法。
  • ConcreteCommand(具体命令):实现命令接口,定义了与接收者的绑定,调用接收者的方法以实现请求。
  • Receiver(接收者):具体执行请求的类,包含执行相关操作的具体方法。
  • Invoker(调用者):持有命令对象并在需要时调用它。它可以调度请求并可以支持多种命令。
  • Client(客户端):创建具体命令对象并将其与接收者绑定,同时将命令对象传递给调用者。

代码

java 复制代码
// Command interface
interface Command {
    void execute();
}

// Receiver
class Light {
    public void turnOn() {
        System.out.println("Light is ON");
    }

    public void turnOff() {
        System.out.println("Light is OFF");
    }
}

// ConcreteCommand for turning on the light
class TurnOnLightCommand implements Command {
    private Light light;

    public TurnOnLightCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.turnOn();
    }
}

// ConcreteCommand for turning off the light
class TurnOffLightCommand implements Command {
    private Light light;

    public TurnOffLightCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.turnOff();
    }
}

// Invoker
class RemoteControl {
    private Command command;

    public void setCommand(Command command) {
        this.command = command;
    }

    public void pressButton() {
        command.execute();
    }
}

// Client code
public class CommandPatternDemo {
    public static void main(String[] args) {
        Light light = new Light();
        
        Command turnOn = new TurnOnLightCommand(light);
        Command turnOff = new TurnOffLightCommand(light);
        
        RemoteControl remote = new RemoteControl();
        
        remote.setCommand(turnOn);
        remote.pressButton(); // Output: Light is ON
        
        remote.setCommand(turnOff);
        remote.pressButton(); // Output: Light is OFF
    }
}

优点

  • 解耦:请求者与接收者之间的解耦,使得系统更加灵活。
  • 可扩展性:可以轻松添加新的命令而不修改现有代码。
  • 支持撤销:可以实现命令的撤销和重做功能。

缺点

  • 命令对象数量多:如果命令数量较多,可能会增加系统的复杂性。
  • 实现复杂性:对于复杂的命令和接收者,可能需要更多的设计工作。

使用场景

  • 当需要将请求调用的对象和请求的执行对象解耦时。
  • 当需要支持撤销、重做操作时。
  • 当需要对请求进行日志记录或排队时。
  • 当需要实现事务处理时。
相关推荐
啦啦啦啦啦zzzz17 小时前
设计模式:桥接模式和组合模式
c++·设计模式·组合模式·桥接模式
莫得感情 o20 小时前
设计模式 03 · 工厂方法模式
设计模式·工厂方法模式
cyforkk21 小时前
高并发核心设计模式:Single-Flight 原理与实战
设计模式
饼干哥哥2 天前
字节Seedance2.5终于上线,这次在收割谁?
人工智能·设计模式·前端框架
董员外2 天前
RAG 系统进化论(六):GraphRAG(基于知识图谱的 RAG),从相似文本走向实体关系
人工智能·后端·设计模式
鬼鬼鬼2 天前
从 Prompt 到 Harness:企业级 Agent 工程的完整演进之路
设计模式·架构·ai编程
啦啦啦啦啦zzzz2 天前
设计模式:原型模式
c++·设计模式·原型模式
董员外2 天前
RAG 系统进化论(五):Corrective RAG 与 Self-RAG,让系统发现并纠正错误
人工智能·后端·设计模式
workflower2 天前
高质量数据集的类型
人工智能·机器学习·设计模式·自然语言处理·机器人
啦啦啦啦啦zzzz2 天前
工具:动态类工厂和用配置文件存储属性
c++·设计模式·工具·动态工厂