命令设计模式

命令设计模式

一、命令设计模式

1、介绍

在软件设计中,我们经常向某些对象发送请求,但是不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可。遇到这种情况可以使用命令模式来设计



命令模式使得请求发送者请求接收者消除彼此之间的耦合,让对象之间的调用关系跟家灵活,实现解耦。



命令模式中,会将一个请求封装为一个对象,以便使用不同参数来表示不同的请求,同时命令模式也支持可撤销的操作




2、原理类图

在这里插入图片描述

Invoker:调用者角色

Receiver:接收者角色,接收命令并执行请求相关操作

Command:命令角色,需要执行的所有命令都在这里面定义。是一个接口或抽象类

ConcreteCommand:将一个接收者对象与一个命令绑定,调用接收者实现请求相关操作




3、命令模式注意事项

(1)将发起请求的对象与执行请求的对象解耦:发请请求的对象是调用者,调用者只要调用命令对象的execute()方法就可以让接收者工作,而不必知道具体的接收者是谁、是如何实现的,命令对象会负责让接收者执行请求的动作,也就是说:“请求发起者”和“请求执行者”之间的解耦是通过命令对象实现的,命令对象起到了纽带桥梁的作用


(2)命令模式容易实现对请求的撤销和重做。可以设计一个命令队列,只要把命令放入队列,就可以多线程的执行命令


(3)命令模式的不足:可能导致某些系统有过多的具体命令类,增加了系统的复杂度,这点在使用的时候要注意


(4)空命令也是一种设计模式,它为我们省去了判空的操作


(5)命令模式经典的应用场景:界面的一个按钮都是一条命令、模拟CMD(DOS命令)、订单的撤销/恢复、触发-反馈机制





二、命令设计模式案例

1、介绍

该案例是一个遥控器,用来控制家中的设备启动与关闭。

在这里插入图片描述




2、UML

在这里插入图片描述

Command:一个命令接口,定义了命令的执行和撤回

NoCommand:一个具体的空命令实现

LightOffCommand、LigntOnCommand、TVOffCommand、TVOnCommand:具体的命令实现,聚合了与命令相关的设备,调用与命令相关的设备操作

LightReceiver、TVReceiver:设备类,类中定义了与设备相关的操作方法

RemoteController:远程控制器,类似一个遥控器,定义了命令调用




3、代码

/**
 * @description: 命令接口
 * @author: dashu
 * @create: 15:01
 */
public interface Command {

    /**
     * 执行
     */
    public void execute();

    /**
     * 撤销
     */
    public void  undo();

}
/**
 * @description: 空命令
 * @author: dashu
 * @create: 15:15
 */
public class NoCommand implements Command{
    /**
     * 执行
     */
    @Override
    public void execute() {

    }

    /**
     * 撤销
     */
    @Override
    public void undo() {

    }
}
/**
 * @description: 灯关闭命令
 * @author: dashu
 * @create: 15:03
 */
public class LightOffCommand implements Command{

    LightReceiver lightReceiver;

    public LightOffCommand(LightReceiver lightReceiver){
        this.lightReceiver = lightReceiver;
    }


    /**
     * 执行
     */
    @Override
    public void execute() {
        lightReceiver.on();
    }

    /**
     * 撤销
     */
    @Override
    public void undo() {
        lightReceiver.off();
    }


}
/**
 * @description: 灯开启命令
 * @author: dashu
 * @create: 15:04
 */
public class LigntOnCommand implements Command{

    LightReceiver lightReceiver;

    public LigntOnCommand(LightReceiver lightReceiver) {
        this.lightReceiver = lightReceiver;
    }

    /**
     * 执行
     */
    @Override
    public void execute() {
        lightReceiver.on();
    }

    /**
     * 撤销
     */
    @Override
    public void undo() {
        lightReceiver.off();
    }


}
/**
 * @description: 灯接收者
 * @author: dashu
 * @create: 15:05
 */
public class LightReceiver {

    public void on(){
        System.out.println("灯开启了...");
    }

    public void off(){
        System.out.println("灯关闭了...");
    }

}
/**
 * @description: TV关闭命令
 * @author: dashu
 * @create: 15:03
 */
public class TVOffCommand implements Command{

    TVReceiver tvReceiver;

    public TVOffCommand(TVReceiver tvReceiver){
        this.tvReceiver = tvReceiver;
    }


    /**
     * 执行
     */
    @Override
    public void execute() {
        tvReceiver.on();
    }

    /**
     * 撤销
     */
    @Override
    public void undo() {
        tvReceiver.off();
    }


}
/**
 * @description: TV开启命令
 * @author: dashu
 * @create: 15:04
 */
public class TVOnCommand implements Command{

    TVReceiver tvReceiver;

    public TVOnCommand(TVReceiver tvReceiver){
        this.tvReceiver = tvReceiver;
    }

    /**
     * 执行
     */
    @Override
    public void execute() {
        tvReceiver.on();
    }

    /**
     * 撤销
     */
    @Override
    public void undo() {
        tvReceiver.off();
    }


}
/**
 * @description: TV接收者
 * @author: dashu
 * @create: 15:05
 */
public class TVReceiver {

    public void on(){
        System.out.println("TV开启了...");
    }

    public void off(){
        System.out.println("TV关闭了...");
    }

}
/**
 * @description: 远程控制器
 * @author: dashu
 * @create: 15:19
 */
public class RemoteController {

    /**
     * 开按钮集合
     */
    Command[] onCommands;

    /**
     * 关按钮集合
     */
    Command[] offCommands;


    /**
     * 撤销按钮
     */
    Command undoCommand;


    /**
     * 构造方法初始化
     */
    public RemoteController(){
        onCommands = new Command[5];
        offCommands = new Command[5];

        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }

    }

    /**
     * 遥控器按钮设置
     * @param no
     */
    public void setCommand(int no, Command onCommand,Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    /**
     * 开启按钮
     * @param no
     */
    public void onButtonWasPushed(int no){
        onCommands[no].execute();
        undoCommand = onCommands[no];
    }

    /**
     * 关闭按钮
     * @param no
     */
    public void offButtonWasPushed(int no){
        offCommands[no].execute();
        undoCommand = offCommands[no];
    }


    /**
     * 撤销按钮
     * @param 
     */
    public void ondoButtonWasPushed(){
        undoCommand.undo();
    }


}
/**
 * @description: 客户端
 * @author: dashu
 * @create: 15:00
 */
public class Client {

    public static void main(String[] args) {

        RemoteController remoteController = new RemoteController();


        LightReceiver lightReceiver = new LightReceiver();
        remoteController.setCommand(0,new LigntOnCommand(lightReceiver),new LightOffCommand(lightReceiver));

        TVReceiver tvReceiver = new TVReceiver();
        remoteController.setCommand(1,new TVOnCommand(tvReceiver),new TVOffCommand(tvReceiver));

        remoteController.onButtonWasPushed(0);
        remoteController.ondoButtonWasPushed();

    }

}


        LightReceiver lightReceiver = new LightReceiver();
        remoteController.setCommand(0,new LigntOnCommand(lightReceiver),new LightOffCommand(lightReceiver));

        TVReceiver tvReceiver = new TVReceiver();
        remoteController.setCommand(1,new TVOnCommand(tvReceiver),new TVOffCommand(tvReceiver));

        remoteController.onButtonWasPushed(0);
        remoteController.ondoButtonWasPushed();

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
命令设计模式是一种行为型设计模式,它允许将请求封装成一个对象,从而使不同的请求能够在客户端之间进行参数化。下面是一个简单的命令设计模式的代码实现示例: ```python # 命令接口 class Command: def execute(self): pass # 具体命令类 class LightOnCommand(Command): def __init__(self, light): self.light = light def execute(self): self.light.on() class LightOffCommand(Command): def __init__(self, light): self.light = light def execute(self): self.light.off() # 接收者类 class Light: def on(self): print("灯已打开") def off(self): print("灯已关闭") # 调用者(遥控器)类 class RemoteControl: def __init__(self): self.commands = [] def add_command(self, command): self.commands.append(command) def execute_commands(self): for command in self.commands: command.execute() # 客户端代码 if __name__ == '__main__': light = Light() light_on_command = LightOnCommand(light) light_off_command = LightOffCommand(light) remote_control = RemoteControl() remote_control.add_command(light_on_command) remote_control.add_command(light_off_command) remote_control.execute_commands() ``` 在上面的示例中,我们定义了一个命令接口 `Command`,并实现了两个具体的命令类 `LightOnCommand` 和 `LightOffCommand`。这些具体命令类都实现了 `execute` 方法,用于执行相应的操作。`Light` 类是接收者类,它定义了真正执行操作的方法。`RemoteControl` 类是调用者类,它可以添加命令并执行它们。 在客户端代码中,我们创建了一个 `Light` 对象,并为它定义了打开和关闭的命令。然后,我们创建了一个遥控器对象 `RemoteControl`,并将命令添加到它里面。最后,我们调用 `execute_commands` 方法,执行所有添加的命令。 这就是一个简单的命令设计模式的代码实现示例。当然,实际应用可以更加复杂,可以根据具体需求进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大树下躲雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值