设计模式-命令模式

设计模式-命令模式

命令模式的英文翻译是 Command Design Pattern。它是这么定义的:The command pattern encapsulates a request as an object, thereby letting us parameterize other objects with different requests, queue or log requests, and support undoable operations. 翻译一下就是命令模式将请求(命令)封装为一个对象,这样可以使用不同的请求参数化其他对象(将不同请求依赖注入到其他对象),并且能够支持请求(命令)的排队执行、记录日志、撤销等(附加控制)功能。

案例分析

一般客户只需要把需求提交给公司,公司内有不同的部门,一般这个需求需要公司的某个部门执行,但是客户是不关注这些的,抽象理解其实是客户端(client)不需要关注请求的具体处理者(Receiver)。

首先定义一个命令接口

java 复制代码
public interface Command {

    void execute();

}

假设前端传递来的请求可以封装为A和B两大类,A类请求由部门A实现,B类请求由部门B实现

java 复制代码
public class RequestA implements Command {

    private DepartmentA receiver;

    public RequestA(DepartmentA receiver) {
        this.receiver = receiver;
    }

    @Override
    public void execute() {
        receiver.computeRequestA();
    }

}

public class RequestB implements Command {

    private DepartmentB receiver;

    public RequestB(DepartmentB receiver) {
        this.receiver = receiver;
    }

    @Override
    public void execute() {
        receiver.computeRequestB();
    }

}

定义部门A和部门B,部门A可以处理A类请求,部门B可以处理B类请求

java 复制代码
public class DepartmentA {

    public void computeRequestA() {
        System.out.println("DepartmentA compute RequestA");
    }

}

public class DepartmentB {

    public void computeRequestB() {
        System.out.println("DepartmentB compute RequestB");
    }

}

其实如果只是为了实现上述功能,其他设计模式也可以达到,但是需要支持命令的排队,延时,就要靠调用者类(invoker)了

java 复制代码
public class Invoker {

    private List<Command> commandList = new ArrayList<>();

    public boolean addCommand(Command request) {
        return commandList.add(request);
    }

    public void invoke() {
        for (Command command : commandList) {
            command.execute();
        }
        commandList.clear();
    }

}

Invoker 类中 commandList 存储了所有待执行的命令,因此可以支持撤销、排队等功能

只有在真正调用 invoke() 方法时,命令才真正的被执行,以下是测试代码:

java 复制代码
public class Main {

    public static void main(String[] args) {

        DepartmentA departmentA = new DepartmentA();
        DepartmentB departmentB = new DepartmentB();

        Command requestA = new RequestA(departmentA);
        Command requestB = new RequestB(departmentB);

        Invoker invoker = new Invoker();
        invoker.addCommand(requestA);
        invoker.addCommand(requestB);

        invoker.invoke();
    }

}

上述代码中省略了将前端传递的参数封装为 RequestA 或 RequestB 的实现。

如上图所示,结合本例,Request 对象其实就是 Command 的具体表现类,内部引用了可以处理其需求的执行者 Receiver(Department),最终由 Invoker 调用执行。

相关推荐
七月丶4 小时前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞4 小时前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼4 小时前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟21 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder21 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室1 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo5 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4965 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式