23种设计模式-行为型模式-命令模式

命令模式

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

  • 特点:命令模式使得请求发送者与请求接收者消除彼此之间的耦合,让对象之间的调用关系更加灵活,实现解耦。
  • 通俗易懂的理解:将军(调用者)发布进攻(命令),王五(接收者)去执行。

案例描述

本例以通过遥控器开灯、关灯为例。其中遥控器相当于请求者(调用者),灯是接收者(执行者),开灯、关灯是命令

通用UML

UML

本例UML关系类图

在这里插入图片描述

具体代码实现

Command(命令)

package com.lcq.commandpattern;

/**
 *
 * @author coder
 * @date 2023/3/12 0:47
 * @description 命令
 */
public interface Command {
    void execute();//执行命令
}

LightOffCommand(关灯命令)

package com.lcq.commandpattern;

/**
 * @Author: coder
 * @CreateTime: 2023-03-12
 * @Description: 关灯命令
 */
public class LightOffCommand implements Command {
    private Light light;

    public LightOffCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.off();//关灯
    }
}

LightOnCommand(开灯命令)

package com.lcq.commandpattern;

/**
 * @Author: coder
 * @CreateTime: 2023-03-12
 * @Description: 开灯命令
 */
public class LightOnCommand implements Command {
    private Light light;

    public LightOnCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.on();//开灯
    }
}

Light(灯)

package com.lcq.commandpattern;

/**
 * @Author: coder
 * @CreateTime: 2023-03-12
 * @Description: 灯(接收者)
 */
public class Light {
    //开灯
    public void on(){
        System.out.println("开灯");
    }
    //关灯
    public void off(){
        System.out.println("关灯");
    }
}

RemoteController(遥控器)

package com.lcq.commandpattern;

/**
 * @Author: coder
 * @CreateTime: 2023-03-12
 * @Description: 遥控器(调用者)
 */
public class RemoteController {
    private Command button;

    public RemoteController(Command command) {
        this.button = command;
    }

    public void setCommand(Command command) {
        this.button = command;
    }

    //按下按钮
    public void buttonIsPress() {
        button.execute();
    }
}

Client(手持遥控者)

package com.lcq.commandpattern;

/**
 * @Author: coder
 * @CreateTime: 2023-03-12
 * @Description: 手持遥控者
 */
public class Client {
    public static void main(String[] args) {
        //灯(接收者)
        Light light = new Light();
        //遥控器(调用者)
        RemoteController remoteController = new RemoteController(new LightOnCommand(light));
        //按下按钮
        remoteController.buttonIsPress();//开灯
        System.out.println("----------------------");
        //设置关灯(命令)
        remoteController.setCommand(new LightOffCommand(light));
        //按下按钮
        remoteController.buttonIsPress();//关灯
    }
}

运行结果

开灯
----------------------
关灯

小结:通过遥控器(调用者)操控命令完成开、关灯操作,由灯(接收者)主动去真正执行关灯、开灯。调用者无需关注接收者如何去执行,只需对命令操作,再由命令去执行灯的开、关。实现调用者与接收者的解耦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值