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

你可能感兴趣的文章
NoSQL数据库概述
查看>>
Notadd —— 基于 nest.js 的微服务开发框架
查看>>
NOTE:rfc5766-turn-server
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>