十七、行为型(命令模式)

ops/2024/10/25 8:38:54/

命令模式(Command Pattern)

概念
命令模式是一种行为型设计模式,它将请求封装成一个对象,从而使您可以使用不同的请求对客户进行参数化,排队请求,以及支持可撤销操作。通过这种模式,调用操作的对象和实际执行操作的对象解耦,使得系统更加灵活。


应用场景

  1. 请求参数化:在某些情况下,您可能需要将请求参数化并传递给多个调用者。命令模式允许将请求封装为对象,便于传递和存储。

  2. 队列和日志:可以将请求存储在队列中,稍后处理。这在异步执行或日志记录系统中非常有用。

  3. 撤销操作:命令对象可以保存请求执行前的状态,从而支持撤销和重做操作。

  4. 宏命令:当需要执行一系列操作时,可以将它们封装成一个宏命令,以便一次性执行。


注意点

  1. 命令对象数量:如果系统中存在大量不同请求,命令对象的数量会显著增加,从而增加系统复杂性。

  2. 状态管理:需要确保命令对象能够保存执行前后的状态,以便实现撤销功能。

  3. 过于复杂的命令:如果命令执行逻辑非常复杂,可能会导致命令对象的实现变得难以维护。


核心要素

  1. Command(命令接口):定义命令的接口,通常包含 execute()undo() 方法。

  2. ConcreteCommand(具体命令):实现命令接口的类,负责调用接收者的相应方法来执行操作。

  3. Receiver(接收者):实际执行命令请求的对象。

  4. Invoker(调用者):负责调用命令对象的 execute() 方法来执行命令。

  5. Client(客户端):创建具体命令对象并将接收者与命令对象关联。


Java代码完整示例

示例:简单的命令模式实现

// 命令接口
interface Command {void execute();void undo(); // 撤销方法
}// 接收者类
class Light {public void turnOn() {System.out.println("The light is on.");}public void turnOff() {System.out.println("The light is off.");}
}// 具体命令类:打开灯
class TurnOnLightCommand implements Command {private Light light;public TurnOnLightCommand(Light light) {this.light = light;}@Overridepublic void execute() {light.turnOn();}@Overridepublic void undo() {light.turnOff();}
}// 具体命令类:关闭灯
class TurnOffLightCommand implements Command {private Light light;public TurnOffLightCommand(Light light) {this.light = light;}@Overridepublic void execute() {light.turnOff();}@Overridepublic void undo() {light.turnOn();}
}// 调用者类
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(); // 撤销上一个命令}}
}// 客户端代码
public class CommandPatternDemo {public static void main(String[] args) {Light livingRoomLight = new Light();// 创建命令对象Command turnOn = new TurnOnLightCommand(livingRoomLight);Command turnOff = new TurnOffLightCommand(livingRoomLight);// 设置命令到调用者RemoteControl remote = new RemoteControl();// 打开灯remote.setCommand(turnOn);remote.pressButton();// 撤销:关闭灯remote.pressUndo();// 关闭灯remote.setCommand(turnOff);remote.pressButton();// 撤销:打开灯remote.pressUndo();}
}

输出结果

The light is on.
The light is off.
The light is off.
The light is on.

各种变形用法完整示例

  1. 支持撤销操作的命令模式

    通过在命令接口中添加 undo() 方法,您可以轻松支持命令的撤销功能。

    代码示例:支持撤销的命令模式

    // 上面的代码示例已包含撤销功能,您可以直接使用。
    
  2. 命令模式

    宏命令允许将多个命令打包为一个命令对象,以便一次性执行。

    代码示例:宏命令

    import java.util.ArrayList;
    import java.util.List;// 宏命令类
    class MacroCommand implements Command {private List<Command> commands = new ArrayList<>();public void addCommand(Command command) {commands.add(command);}@Overridepublic void execute() {for (Command command : commands) {command.execute();}}@Overridepublic void undo() {for (Command command : commands) {command.undo();}}
    }public class MacroCommandDemo {public static void main(String[] args) {Light livingRoomLight = new Light();Command turnOn = new TurnOnLightCommand(livingRoomLight);Command turnOff = new TurnOffLightCommand(livingRoomLight);MacroCommand macroCommand = new MacroCommand();macroCommand.addCommand(turnOn);macroCommand.addCommand(turnOff);// 执行宏命令RemoteControl remote = new RemoteControl();remote.setCommand(macroCommand);remote.pressButton(); // 执行所有命令}
    }
    
  3. 命令队列

    在异步执行或延迟执行场景中,可以将命令存储在队列中。

    代码示例:命令队列

    import java.util.LinkedList;
    import java.util.Queue;class CommandQueue {private Queue<Command> commandQueue = new LinkedList<>();public void addCommand(Command command) {commandQueue.offer(command);}public void executeAll() {while (!commandQueue.isEmpty()) {Command command = commandQueue.poll();command.execute();}}
    }public class CommandQueueDemo {public static void main(String[] args) {CommandQueue commandQueue = new CommandQueue();Light livingRoomLight = new Light();commandQueue.addCommand(new TurnOnLightCommand(livingRoomLight));commandQueue.addCommand(new TurnOffLightCommand(livingRoomLight));// 执行命令队列中的所有命令commandQueue.executeAll();}
    }
    

通过这些示例,命令模式的灵活性和强大功能得以体现,可以根据具体需求实现多种变形用法。


http://www.ppmy.cn/ops/128278.html

相关文章

Kubernetes:(二)K8Sv1.20二进制部署

文章目录 一、k8s项目架构二、二进制搭建 Kubernetes v1.20 &#xff08;单master节点&#xff09;1.操作系统初始化配置2.部署 docker引擎3. etcd的概念4. 证书认证5. node01 节点操作&#xff08;192.168.44.10&#xff09;6. node02 节点操作&#xff08;192.168.44.40&…

Maven学习笔记

目录 一、什么是Maven 二、maven下载和安装目录 1、安装目录解析 2、maven仓库 二、maven项目创建&#xff08;Hello&#xff09; 四、maven项目操作 五、创建HelloFriend 六、maven项目中pom.xml标签解释 1、坐标 2、依赖 &#xff08;1&#xff09;依赖的范围 &a…

Java最全面试题->Java主流框架->Zuukeeper面试题

文章目录 ZuukeeperZooKeeper是什么?ZooKeeper和dubbo的区别?Zookeeper的java客户端都有哪些?ZooKeeper提供了什么?说说ZooKeeper文件系统说说ZAB协议?Znode有哪些类型?Zookeeper节点宕机如何处理?Zookeeper有哪几种几种部署模式?Zookeeper的典型应用场景?说一下Zooke…

开启RefCell debug_refcell feature查看借用冲突位置

文章目录 背景分析解决方法 本文解决两个问题&#xff1a; 开启rust源码库中的feature开启debug_refcell feature的方法查看 borrow 借用冲突的位置 背景 使用 RefCell 来实现内部可变性是在 Rust 开发中常用的方式&#xff0c;但是当逻辑复杂起来&#xff0c;常常会有可变借…

C++(面向对象、封装性、构造函数)

面向对象 三大特征&#xff1a;封装、继承、多态 C中的class是从C的struct扩展来的&#xff0c;两者的区别是默认访问权限不同。 struct的默认访问权限是公有的public class默认访问权限是私有的private 访问权限 类的内部派生类&#xff08;子类&#xff09…

RK3568开发板(debain系统)与Ubuntu使用nfs共享文件

VMware虚拟机 Ubuntu18.04 【网络配置陈桥接模式】 RK3568开发板【我是用讯为的RK3568】 网线连接路由器或者和电脑直连&#xff08;J13网口&#xff09; 1、Ubuntu上nfs服务器安装 1.1、命令如下&#xff1a; sudo apt-get update sudo apt-get install nfs-kernel-server1…

缓存预取文章比较分析

这篇文章主要记录调研的cache prefetch部分有价值的论文&#xff0c;并给出一些总结 Baleen 核心内容 ML learning-based admission policy prefetch.exploit a new cache residency model to guide model training. 首先对 access pattern 进行了统计分析&#xff0c;提取…

【火山引擎】语音识别 |流式语音识别 | python

目录 一 准备工作 二 流式语音识别 三 实践 模型广场:账号登录-火山引擎