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

你可能感兴趣的文章
python读excel
查看>>
Python 中读取 CSV 文件-ChatGPT4o作答
查看>>
Python 之 filecmp
查看>>
python请求html_使用Python请求获取HTML?
查看>>
Python 之匿名函数和偏函数
查看>>
python 之栈的实现
查看>>
python语音播放
查看>>
python语言:装饰器原理
查看>>
Python 交互式数据可视化详解
查看>>
python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
查看>>
Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
查看>>
Python 从数据库中存储和检索密码的最安全方法
查看>>
Python语言及其应用 - 知识点遍历
查看>>
Python 优化提速的 8 个小技巧
查看>>
Python 余弦相似度与皮尔逊相关系数 计算
查看>>
python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
查看>>
python 使用filetype校验文件
查看>>
Python 使用flush函数将缓冲区数据立即写磁盘
查看>>
python 使用in判断不准确,in不好使
查看>>
Python 使用pandas 进行查询和统计详解
查看>>