命令模式(命令)

命令模式

什么时命令模式

命令模式(Command),将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可撤销的操作。

通过示例了解命令模式

命令模式(Command Pattern)是一种行为设计模式,它将请求封装为一个对象,从而使你可用不同的请求参数化其他对象。命令模式也支持撤销操作。下面是一个简单的Java命令模式示例,以控制家电设备为例:

接受者接口:家电设备接口

public interface Device {
    void on();
    void off();
}

具体接收者:电视

public class Television implements Device {
    @Override
    public void on() {
        System.out.println("Television is turned on.");
    }

    @Override
    public void off() {
        System.out.println("Television is turned off.");
    }
}

具体接收者:空调

public class AirConditioner implements Device {
    @Override
    public void on() {
        System.out.println("Air conditioner is turned on.");
    }

    @Override
    public void off() {
        System.out.println("Air conditioner is turned off.");
    }
}

命令接口:设备控制接口

public interface Command {
    void execute();
}

具体命令:打开设备的命令

public class TurnOnCommand implements Command {
    private Device device;

    public TurnOnCommand(Device device) {
        this.device = device;
    }

    @Override
    public void execute() {
        device.on();
    }
}

具体命令:关闭设备的命令

public class TurnOffCommand implements Command {
    private Device device;

    public TurnOffCommand(Device device) {
        this.device = device;
    }

    @Override
    public void execute() {
        device.off();
    }
}

请求者:遥控器

public class RemoteControl {
    private Command command;

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

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

主程序

public class CommandPatternDemo {
    public static void main(String[] args) {
        RemoteControl remote = new RemoteControl();

        Device television = new Television();
        Command turnOnTelevision = new TurnOnCommand(television);
        Command turnOffTelevision = new TurnOffCommand(television);
        remote.setCommand(turnOnTelevision);
        remote.pressButton(); // 输出:Television is turned on.

        remote.setCommand(turnOffTelevision);
        remote.pressButton(); // 输出:Television is turned off.

        Device airConditioner = new AirConditioner();
        Command turnOnAirConditioner = new TurnOnCommand(airConditioner);
        Command turnOffAirConditioner = new TurnOffCommand(airConditioner);
        remote.setCommand(turnOnAirConditioner);
        remote.pressButton(); // 输出:Air conditioner is turned on.

        remote.setCommand(turnOffAirConditioner);
        remote.pressButton(); // 输出:Air conditioner is turned off.
    }
}
  1. Device接口是接受者,定义了设备的基本操作。
  2. Television和AirConditioner是具体接收者,实现了Device接口。
  3. Command接口是命令接口,定义了执行命令的方法
  4. TurnOnCommand和TurnOffCommand是具体命令,它们持有设备对象并实现了execute方法。
  5. RemoteControl是请求者,它持有一个命令对象,并调用execute方法来执行命令。
  6. main方法展示了如何使用遥控器控制不同设备的开关操作。

通过命令模式,我们可以轻松地添加新的设备和控制操作,同时保持遥控器类的简洁。

相关推荐

  1. 命令模式(命令)

    2024-05-16 07:38:10       10 阅读
  2. ·命令模式

    2024-05-16 07:38:10       27 阅读
  3. 【设计模式命令模式

    2024-05-16 07:38:10       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-16 07:38:10       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-16 07:38:10       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-16 07:38:10       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-16 07:38:10       18 阅读

热门阅读

  1. 关于数据结构的整理

    2024-05-16 07:38:10       10 阅读
  2. android之instrumentation的简单记录

    2024-05-16 07:38:10       10 阅读
  3. OpenAI 推出 GPT-4o,慢其实是快

    2024-05-16 07:38:10       11 阅读
  4. 如何使用Python进行网页爬取

    2024-05-16 07:38:10       15 阅读
  5. 自强不息(好习惯与自律相互促进)

    2024-05-16 07:38:10       7 阅读
  6. Spring MVC(七) 异常处理

    2024-05-16 07:38:10       10 阅读
  7. Oracle数据块之数据行中的SCN

    2024-05-16 07:38:10       11 阅读
  8. 【专用】C# ArrayList的用法总结

    2024-05-16 07:38:10       9 阅读
  9. 【FFmpeg】编码链路上主要函数的简单分析

    2024-05-16 07:38:10       15 阅读
  10. Linux文件处理知识点

    2024-05-16 07:38:10       8 阅读
  11. 24.HashMap的扩容机制

    2024-05-16 07:38:10       12 阅读