博客
关于我
命令模式
阅读量: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 | 爬虫实战——亚马逊搜索页监控(附详细源码)
查看>>
python | 高效使用Python工具自动生成模块文档的秘诀
查看>>
python 一个list去除另一个list中的值
查看>>
python 三大框架的 介绍。
查看>>
Python 下载的 11 种姿势,一种比一种高级!
查看>>
python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
查看>>
Python 中 3 个不可思议的返回功能
查看>>
python 中 dict 的另一种用法
查看>>
Python 中 PIL 读取图片出现异常旋转的解决方法
查看>>
python读取word表格内容(1)
查看>>
python 中os.path.join 双斜杠的解决办法
查看>>
python 中PIL.Image和OpenCV图像格式相互转换
查看>>
Python 中Semaphore 信号量对象、Event事件、Condition
查看>>
python 中with的使用及样例
查看>>
python读取wav文件并播放[pyaudio/wave]
查看>>
python读取txt文件的行数
查看>>
Python 中内置的最大堆 API
查看>>
Python 中只有一个 True 和一个 False 对象吗?
查看>>
python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
查看>>
Python 中多线程与多处理之间的区别
查看>>