博客
关于我
命令模式
阅读量:580 次
发布时间:2019-03-09

本文共 2211 字,大约阅读时间需要 7 分钟。

命令模式(Command Pattern)

概述

命令模式是一种数据驱动的设计模式,属于行为型模式。它将操作请求以命令的形式包裹,并传递给目标对象。目标对象会寻找适合处理该命令的接收者,并将命令交给相应的接收者接收者执行命令。

目的

通过将操作封装到命令对象中,可以实现以下功能:

  • 用命令参数化其他对象
  • 将命令排入队列
  • 记录命令到日志
  • 支持命令的可撤销操作

类图说明

命令模式主要包含以下四个角色:

  • Command(命令):封装操作
  • Receiver(接收者):命令的实际执行者
  • Invoker(调用者):执行命令的主体
  • Client(客户端):设置命令及其接收者

示例实现

以下是一个使用命令模式控制电灯开关的简单实现:

Command 接口

public interface Command {    void execute();}

LightOnCommand 实现

public class LightOnCommand implements Command {    private Light light;    public LightOnCommand(Light light) {        this.light = light;    }    @Override    public void execute() {        light.on();    }}

LightOffCommand 实现

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

Light 类

public class Light {    public void on() {        System.out.println("灯是开启的");    }    public void off() {        System.out.println("灯是关闭的");    }}

Invoker 类

public class Invoker {    private Command[] onCommands;    private Command[] offCommands;    private static final int SLOT_NUM = 7;    public Invoker() {        this.onCommands = new Command[SLOT_NUM];        this.offCommands = new Command[SLOT_NUM];    }    public void setOnCommand(Command command, int slot) {        onCommands[slot] = command;    }    public void setOffCommand(Command command, int slot) {        offCommands[slot] = command;    }    public void onButtonPushed(int slot) {        onCommands[slot].execute();    }    public void offButtonPushed(int slot) {        offCommands[slot].execute();    }}

Client 类

public class Client {    public static void main(String[] args) {        Invoker invoker = new Invoker();        Light light = new Light();                Command lightOnCommand = new LightOnCommand(light);        Command lightOffCommand = new LightOffCommand(light);                invoker.setOnCommand(lightOnCommand, 0);        invoker.setOffCommand(lightOffCommand, 0);                invoker.onButtonPushed(0);        invoker.offButtonPushed(0);    }}

JDK工具

在JDK中,可以使用以下工具进行命令模式的实现:

  • Command 接口:声明命令操作
  • 命令实现类:实现具体命令操作
  • 接收者接口:定义接收命令的实现
  • 调用器类:管理和执行命令

通过命令模式,可以实现对象之间的松耦合设计,便于扩展和维护多种操作行为。

转载地址:http://qdhiz.baihongyu.com/

你可能感兴趣的文章
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>
npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
查看>>