【设计模式-命令】

定义

命令模式(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
    }
}

优点

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

缺点

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

使用场景

  • 当需要将请求调用的对象和请求的执行对象解耦时。
  • 当需要支持撤销、重做操作时。
  • 当需要对请求进行日志记录或排队时。
  • 当需要实现事务处理时。
相关推荐
晨米酱13 小时前
JavaScript 中"对象即函数"设计模式
前端·设计模式
数据智能老司机18 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机19 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机19 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机19 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
使一颗心免于哀伤19 小时前
《设计模式之禅》笔记摘录 - 21.状态模式
笔记·设计模式
数据智能老司机2 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机2 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
烛阴2 天前
【TS 设计模式完全指南】懒加载、缓存与权限控制:代理模式在 TypeScript 中的三大妙用
javascript·设计模式·typescript
李广坤2 天前
工厂模式
设计模式