• 设计模式 - 命令模式


    目录

    一. 前言

    二. 实现

    三. 优缺点


    一. 前言

        命令模式:将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开,解耦合。这样两者之间通过命令对象进行沟通,这样方便将命令对象进行存储、传递、调用、增加与管理。

    命令模式的本质是对请求进行封装,一个请求对应于一个命令,将发出命令的责任和执行命令的责任分割开。每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行相应的操作。命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求如何被接收、操作是否被执行、何时被执行,以及是怎么被执行的。

    注:命令模式无法解决类的个数增加的问题

    二. 实现

    Command: 命令。定义命令的接口,声明执行的方法。
    Receiver: 命令接收者,也就是命令真正的执行者。任何类都可能成为一个接收者,只要它能够实现命令要求实现的相应功能。
    Invoker: 通过它来调用命令。这个是客户端真正触发命令并要求命令执行相应操作的地方,也就是说相当于使用命令对象的入口。
    Client: 可以设置命令与命令的接收者。

    设计一个遥控器,可以控制电灯开关。

    1. public interface Command {
    2. void execute();
    3. }
    4. public class LightOnCommand implements Command {
    5. Light light;
    6. public LightOnCommand(Light light) {
    7. this.light = light;
    8. }
    9. @Override
    10. public void execute() {
    11. light.on();
    12. }
    13. }
    14. public class LightOffCommand implements Command {
    15. Light light;
    16. public LightOffCommand(Light light) {
    17. this.light = light;
    18. }
    19. @Override
    20. public void execute() {
    21. light.off();
    22. }
    23. }
    1. public class Light {
    2. public void on() {
    3. System.out.println("Light is on!");
    4. }
    5. public void off() {
    6. System.out.println("Light is off!");
    7. }
    8. }
    1. /**
    2. * 遥控器
    3. */
    4. public class Invoker {
    5. private Command[] onCommands;
    6. private Command[] offCommands;
    7. private final int slotNum = 7;
    8. public Invoker() {
    9. this.onCommands = new Command[slotNum];
    10. this.offCommands = new Command[slotNum];
    11. }
    12. public void setOnCommand(Command command, int slot) {
    13. onCommands[slot] = command;
    14. }
    15. public void setOffCommand(Command command, int slot) {
    16. offCommands[slot] = command;
    17. }
    18. public void onButtonWasPushed(int slot) {
    19. onCommands[slot].execute();
    20. }
    21. public void offButtonWasPushed(int slot) {
    22. offCommands[slot].execute();
    23. }
    24. }
    1. public class Client {
    2. public static void main(String[] args) {
    3. Invoker invoker = new Invoker();
    4. Light light = new Light();
    5. Command lightOnCommand = new LightOnCommand(light);
    6. Command lightOffCommand = new LightOffCommand(light);
    7. invoker.setOnCommand(lightOnCommand, 0);
    8. invoker.setOffCommand(lightOffCommand, 0);
    9. invoker.onButtonWasPushed(0);
    10. invoker.offButtonWasPushed(0);
    11. }
    12. }

    三. 优缺点

    优点
    1. 降低系统的耦合度。命令模式能将调用操作的对象与实现该操作的对象解耦。
    2. 增加或删除命令非常方便。采用命令模式增加与删除命令不会影响其他类,它满足“开闭原则”,对扩展比较灵活。
    3. 可以实现宏命令。命令模式可以与组合模式结合,将多个命令装配成一个组合命令,即宏命令。
    4. 方便实现 Undo 和 Redo 操作。命令模式可以与后面介绍的备忘录模式结合,实现命令的撤销与恢复。
    缺点
    1. 使用命令模式可能会导致某些系统有过多的具体命令类。
    2. 系统结构更加复杂。

    JDK中的命令模式
    java.lang.Runnable
    javax.swing.Action

  • 相关阅读:
    初识C语言
    王者荣耀S29赛季是什么时候开始更新及王者荣耀S29赛季幻海映的新英雄皮肤是谁?
    图的邻接矩阵与搜索
    Linux | Linux使用互斥锁及条件变量替代信号量
    SpringBoot异步任务、邮件任务、定时任务
    STM32的外部SRAM
    从输入一个网址到浏览器页面展示到底发生了什么
    Linux 修改SSH端口
    漏洞复现-Apache Druid 任意文件读取 _(CVE-2021-36749)
    Home Assistant:基于Python的智能家居开源系统详解
  • 原文地址:https://blog.csdn.net/mrluo735/article/details/133672903