java23种设计模式-命令模式

news/2025/2/26 23:14:51

命令模式(Command Pattern)学习笔记

1. 模式定义

行为型设计模式,将请求封装为对象,使请求的发送者与接收者解耦。支持请求的排队、记录、撤销/重做等操作。

2. 适用场景

✅ 需要将操作参数化
✅ 需要支持事务操作(撤销/重做)
✅ 需要实现任务队列/线程池
✅ 需要记录操作历史
✅ 需要支持宏命令(命令组合)

3. 模式结构

knows
executes
creates
creates
«interface»
Command
+execute()
+undo()
Receiver
+action()
ConcreteCommand
-receiver: Receiver
-state
+execute()
+undo()
Invoker
-command: Command
+setCommand()
+executeCommand()
Client
+createCommand()

4. 核心角色

角色说明
Command命令接口,声明执行操作的抽象方法
ConcreteCommand具体命令,绑定接收者与动作,实现execute()方法
Receiver接收者,实际执行操作的对象
Invoker调用者,触发命令执行
Client创建具体命令并设置接收者

5. 代码示例

5.1 智能家居控制示例

// 接收者 - 灯光设备
class Light {
    public void on() {
        System.out.println("打开灯光");
    }
    
    public void off() {
        System.out.println("关闭灯光");
    }
}

// 命令接口
interface Command {
    void execute();
    void undo();
}

// 具体命令
class LightOnCommand implements Command {
    private Light light;
    
    public LightOnCommand(Light light) {
        this.light = light;
    }
    
    public void execute() {
        light.on();
    }
    
    public void undo() {
        light.off();
    }
}

class LightOffCommand implements Command {
    private Light light;
    
    public LightOffCommand(Light light) {
        this.light = light;
    }
    
    public void execute() {
        light.off();
    }
    
    public void undo() {
        light.on();
    }
}

// 调用者 - 遥控器
class RemoteControl {
    private Command command;
    private Command lastCommand;
    
    public void setCommand(Command command) {
        this.command = command;
    }
    
    public void pressButton() {
        command.execute();
        lastCommand = command;
    }
    
    public void pressUndo() {
        if (lastCommand != null) {
            lastCommand.undo();
            lastCommand = null;
        }
    }
}

// 客户端
public class Client {
    public static void main(String[] args) {
        Light livingRoomLight = new Light();
        Command lightOn = new LightOnCommand(livingRoomLight);
        Command lightOff = new LightOffCommand(livingRoomLight);
        
        RemoteControl remote = new RemoteControl();
        
        remote.setCommand(lightOn);
        remote.pressButton();  // 打开灯光
        
        remote.setCommand(lightOff);
        remote.pressButton();  // 关闭灯光
        
        remote.pressUndo();    // 撤销操作(重新打开灯光)
    }
}

6. 模式变种

  1. 宏命令(组合命令)
class MacroCommand implements Command {
    private List<Command> commands = new ArrayList<>();
    
    public void add(Command cmd) {
        commands.add(cmd);
    }
    
    public void execute() {
        for (Command cmd : commands) {
            cmd.execute();
        }
    }
    
    public void undo() {
        for (int i = commands.size()-1; i >= 0; i--) {
            commands.get(i).undo();
        }
    }
}
  1. 事务性命令
class TransactionManager {
    private Stack<Command> history = new Stack<>();
    
    public void execute(Command cmd) {
        cmd.execute();
        history.push(cmd);
    }
    
    public void rollback() {
        while (!history.isEmpty()) {
            history.pop().undo();
        }
    }
}

7. 优缺点分析

✔️ 优点

  • 解耦请求发送者与接收者
  • 支持撤销/重做操作
  • 容易扩展新命令
  • 支持命令的组合(宏命令)
  • 支持请求排队与日志记录

缺点

  • 增加系统复杂度(每个操作都需要命令类)
  • 可能产生大量具体命令类
  • 需要管理命令的生命周期

8. 相关模式对比

模式目的关键区别
策略模式封装算法族关注算法选择,通常不可逆
观察者模式状态变化通知事件驱动,被动通知
备忘录模式状态保存与恢复关注对象状态保存
责任链模式请求传递处理多个对象处理请求

9. 实际应用案例

  • Java的Runnable接口(命令模式+线程池)
  • Swing的Action接口
  • 数据库事务管理
  • 操作系统的任务调度
  • 游戏开发中的输入处理
  • 文本编辑器的撤销/重做功能
  • Spring的JdbcTemplate(命令模式+模板方法)

10. 最佳实践建议

  1. 使用命令接口:保持命令的通用性
  2. 实现撤销机制:存储必要的状态信息
  3. 结合组合模式:实现宏命令功能
  4. 使用对象池:管理频繁创建的命令对象
  5. 添加日志功能:记录命令执行历史
  6. 支持异步执行:结合线程池实现
  7. 使用空对象模式:处理无操作命令(NullCommand)

11. 扩展应用(结合线程池)

// 命令接口(扩展支持异步)
interface AsyncCommand extends Command {
    Future<?> executeAsync(ExecutorService executor);
}

// 具体命令实现
class FileBackupCommand implements AsyncCommand {
    private String filePath;
    
    public FileBackupCommand(String path) {
        this.filePath = path;
    }
    
    public void execute() {
        // 同步执行
        backup();
    }
    
    public Future<?> executeAsync(ExecutorService executor) {
        return executor.submit(this::backup);
    }
    
    private void backup() {
        System.out.println("备份文件: " + filePath);
        // 具体备份逻辑...
    }
    
    public void undo() {
        System.out.println("恢复备份: " + filePath);
        // 恢复逻辑...
    }
}

// 使用示例
ExecutorService pool = Executors.newFixedThreadPool(2);
AsyncCommand cmd = new FileBackupCommand("data.db");
Future<?> task = cmd.executeAsync(pool);

🎮 设计原则体现

  1. 单一职责原则:命令对象只负责执行特定操作
  2. 开闭原则:新增命令无需修改现有代码
  3. 依赖倒置原则:调用者依赖抽象命令接口

通过命令模式,可以实现灵活的操作管理机制,特别适合需要支持事务操作、任务队列、撤销/重做等功能的场景。该模式在GUI编程和事务处理系统中应用广泛,是解耦操作请求与具体实现的经典解决方案。


http://www.niftyadmin.cn/n/5869263.html

相关文章

重构清洁想象,石头科技首创五轴仿生机械手打破传统清洁边界

2月25日&#xff0c;主题为“重构清洁想象”的石头科技2025发布会在上海天文馆正式召开。石头科技清洁产品BU总裁钱启杰在会上宣布&#xff0c;石头科技正式成为上海天文馆授权合作伙伴&#xff0c;希望借助航天科技到家庭科技的跨越&#xff0c;进一步简化家庭清洁工作&#x…

升维商哲院高考院:用易经命理为未来导航,开启慧性思维新篇章

&#xff08;2025年2月19日&#xff0c;中国北京&#xff09;在人工智能飞速发展、社会变革日新月异的今天&#xff0c;如何帮助青少年更好地规划未来&#xff0c;培养适应未来社会发展的核心素养&#xff0c;成为教育领域的重要课题。近日&#xff0c;名为“升维商哲院高考院”…

如何基于PyTorch做二次开发

基于PyTorch进行二次开发以实现可视化工程&#xff0c;可以从以下几个方面入手&#xff1a;模型结构可视化、训练过程监控、特征可视化等。以下是一些推荐的GitHub项目&#xff0c;这些项目可以帮助你快速搭建一个可视化的工程环境&#xff1a; ### 1. **PyTorch CNN Visualiz…

Visual Studio Code 跨平台安装与配置指南(附官方下载链接)

一、软件定位与核心功能 Visual Studio Code&#xff08;简称VS Code&#xff09;是微软开发的开源跨平台代码编辑器&#xff0c;支持超过50种编程语言的智能补全、调试和版本控制功能。2025版本新增AI辅助编程模块&#xff0c;可自动生成单元测试代码和API文档注释。 二、下载…

蓝桥杯刷题-dp-线性dp(守望者的逃离,摆花,线段)

[NOIP 2007 普及组] 守望者的逃离 题目描述 恶魔猎手尤迪安野心勃勃&#xff0c;他背叛了暗夜精灵&#xff0c;率领深藏在海底的娜迦族企图叛变。 守望者在与尤迪安的交锋中遭遇了围杀&#xff0c;被困在一个荒芜的大岛上。 为了杀死守望者&#xff0c;尤迪安开始对这个荒岛…

linux 命令+相关配置记录(持续更新...)

linux 命令记录相关配置记录 磁盘切换 cd D&#xff1a;#这里表示切换到D盘查看wsl 安装的linux 子系统 wsl --list -vwsl 卸载 linux 子系统 wsl --unregister -xxx # xxx 表示子系统的名字备份Linux 子系统 导出 wsl --export xxx yyy # xxx 表示子系统的名字 yyy 表示压…

Modelfile配置说明

参数说明翻译 参数描述值类型示例用法mirostat启用Mirostat采样以控制困惑度。&#xff08;默认&#xff1a;0&#xff0c;0禁用&#xff0c;1Mirostat&#xff0c;2Mirostat 2.0&#xff09;intmirostat 0mirostat_eta影响算法对生成文本反馈的响应速度。较低的学习率将导致调…

RAGS评测后的数据 如何利用influxdb和grafan 进行数据汇总查看

RAGS(通常指相关性、准确性、语法、流畅性)评测后的数据能借助 InfluxDB 存储,再利用 Grafana 进行可视化展示,实现从四个维度查看数据,并详细呈现每个问题对应的这四个指标情况。以下是详细步骤: 1. 环境准备 InfluxDB 安装与配置 依据自身操作系统,从 InfluxDB 官网下…