16 命令行模式

命令行模式

将行为的执行与与行为的调用通过命令分离,行为的的调用者不需要知道具体是哪个类执行的,他们之间通过命令连接。
在这里插入图片描述

demo的目录结构
在这里插入图片描述
命令的执行者(接口)

package behavioralpattern.commandpattern.actuator;

import java.math.BigDecimal;

/**
 * @author tx
 * @version 1.0
 * @date 2024/1/15 16:12
 * @description:
 * 命令的执行者
 */
public interface Actuator {
    BigDecimal act(BigDecimal x,BigDecimal y);
}

命令的执行者(实现)

package behavioralpattern.commandpattern.actuator;

import java.math.BigDecimal;

/**
 * @author tx
 * @version 1.0
 * @date 2024/1/15 16:17
 * @description: 加法执行器
 */
public class Add implements Actuator{
    @Override
    public BigDecimal act(BigDecimal x, BigDecimal y) {
        return x.add(y);
    }
}

命令的抽象层

package behavioralpattern.commandpattern.command;

import behavioralpattern.commandpattern.actuator.Actuator;
import behavioralpattern.commandpattern.actuator.Add;

import java.math.BigDecimal;

/**
 * @author tx
 * @version 1.0
 * @date 2024/1/15 16:07
 * @description:
 * 命令的抽象
 */
public abstract class Command {
    private final Actuator actuator;

    public Command() {
        // 初始化一个具体的执行器
        actuator = setActuator();
    }
    // 交给子类确定是什么执行器
    protected abstract Actuator setActuator();

    /**
     * 命令执行,command本身不进行具体的操作
     * @param x 第一个参数
     * @param y 第二个参数
     * @return 返回的执行结果
     */
    public BigDecimal execute(BigDecimal x,BigDecimal y){
        return actuator.act(x,y);
    }
}

命令的实现

package behavioralpattern.commandpattern.command;

import behavioralpattern.commandpattern.actuator.Actuator;
import behavioralpattern.commandpattern.actuator.Add;

/**
 * @author tx
 * @version 1.0
 * @date 2024/1/15 16:09
 * @description:
 * 加法命令
 */
public class AddC extends Command{
    @Override
    protected Actuator setActuator() {
        return new Add();
    }
}

客户端&调用者

package behavioralpattern.commandpattern;

import behavioralpattern.commandpattern.command.*;

import java.math.BigDecimal;

/**
 * @author tx
 * @version 1.0
 * @date 2024/1/14 19:23
 * @description:
 * 命令模式
 *
 */
public class CommandPattern {
    public static void main(String[] args) {
        // 创建一个命令发起者
        Invoker invoker = new Invoker();
        // 创建几个命令
        Command addC = new AddC();
        Command subC = new SubC();
        Command mulC = new MulC();
        Command divC = new DivC();
        // 定义2个数
        String x = "100";
        String y = "10";
        System.out.printf("x:%s,y:%s\n",x,y);
        // 执行加法
        invoker.setCommand(new AddC());
        BigDecimal bigDecimal = invoker.execCommand(x, y);
        System.out.println("加法执行结果:"+bigDecimal);
        // 执行除法
        invoker.setCommand(new DivC());
        BigDecimal bigDecimal1 = invoker.execCommand(x, y);
        System.out.println("除法执行结果:"+bigDecimal1);
    }
}
// 命令发起者
class Invoker{
    private Command command;

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

    public BigDecimal execCommand(String x, String y){
        return command.execute(new BigDecimal(x),new BigDecimal(y));
    }
}

对于调用者来说,命令的具体执行者是不可见的,他接触到的是一个个命令,命令和命令的执行者都可以非常方便的扩展。完整代码见 gitee

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值