博客
关于我
命令模式
阅读量: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/

你可能感兴趣的文章
pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
查看>>
pip 安装opencv-python卡死
查看>>
pip 安装出现异常
查看>>
Pip 安装失败:需要 SSL
查看>>
Pip 安装挂起
查看>>
pip 或 pip3 为 Python 3 安装包?
查看>>
pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
查看>>
pip 无法从 requirements.txt 安装软件包
查看>>
pip/pip3更换国内源
查看>>
pip3 install PyQt5 --user 失败
查看>>
pip3命令全解析:Python3包管理工具的详细使用指南
查看>>
pip3安装命令重复创建文件‘/tmp/pip-install-xxxxx/package‘失败
查看>>
PIPE 接口信号列表
查看>>
pipeline配置与管理Job企业级实战
查看>>
pipeline项目配置实战
查看>>
Pipenv 与 Conda?
查看>>
QVGA/HVGA/WVGA/FWVGA分辨率屏含义及大小//Android虚拟机分辨率
查看>>
pipreqs : 无法将“pipreqs”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径 正确,然后再试一次。
查看>>
pipy国内镜像的网址
查看>>
quiver绘制python语言
查看>>