Command Mode of Design Mode 9

In development, there is often a tight coupling between method request and method execution, which is not conducive to system maintenance. The command mode appeared to solve this problem.

What is command mode

Command mode is a highly cohesive mode, which is defined as: Encapsulate a request as an object, there by letting you parameterize clients with different requests, queue or log requests, and support undoable operations. (Encapsulate a request as an object, thereby Allows you to use different requests to parameterize the client, queue requests or record request logs, and provide command cancellation and recovery functions.)

The following is a general class diagram of the command mode:

Command mode

The command mode is mainly composed of 3 elements:

  • Receive recipient: Receive command

  • Command role: encapsulate the command to be executed

  • Invoker caller: execute command

Code

First create an abstract receiverReceiver

public abstract class Receiver {
    //抽象接收者,定义每个接收者都必须完成的业务
    public abstract void doSomething();
}

Create another Receiverimplementation class ConcreteReceiver1. There can be multiple implementation classes, such as creating them ConcreteReceiver2.

public class ConcreteReceiver1 extends Receiver {
    @Override
    public void doSomething() {
        Console.log("执行ConcreteReceiver1的方法...");
    }
}

public class ConcreteReceiver2 extends Receiver {
    @Override
    public void doSomething() {
        Console.log("执行ConcreteReceiver2的方法...");
    }
}

Then create the command abstractionCommand

public abstract class Command {
    //每个命令类都必须有一个执行命令的方法
    public abstract void execute();
}

Similarly Receiver, Commandthere can be many implementation classes.

public class ConcreteCommand1 extends Command {
    //对哪个Receiver类进行命令处理
    private Receiver receiver;

    //构造函数传递接收者
    public ConcreteCommand1(Receiver _receiver) {
        this.receiver = _receiver;
    }

    //必须实现一个命令
    @Override
    public void execute() {
        //业务处理
        this.receiver.doSomething();
    }
}
public class ConcreteCommand2 extends Command {
    //对哪个Receiver类进行命令处理
    private Receiver receiver;

    //构造函数传递接收者
    public ConcreteCommand2(Receiver _receiver) {
        this.receiver = _receiver;
    }

    //必须实现一个命令
    @Override
    public void execute() {
        //业务处理
        this.receiver.doSomething();
    }
}

Create callerInvoker

public class Invoker {
    private Command command;

    //接受命令
    public void setCommand(Command _command) {
        this.command = _command;
    }

    //执行命令
    public void action() {
        this.command.execute();
    }
}

test:

@Test
public void test() {
    //首先声明调用者Invoker
    Invoker invoker = new Invoker();
    //定义接收者
    Receiver receiver = new ConcreteReceiver1();
    //定义一个发送给接收者的命令
    Command command = new ConcreteCommand1(receiver);
    //把命令交给调用者去执行
    invoker.setCommand(command);
    invoker.action();
}

Test Results:

执行ConcreteReceiver1的方法...

ReceiverThe method here is the method we need to use. This method is Commandencapsulated by acquisition and Invokerexecuted.

Thinking about command mode

The caller does not directly call the methods of the recipient, but to call Commandin execute(). For the caller, it does not need to know who the receiver is.

Generally speaking, the command mode combined with other design modes will have better results.

But in the command mode, Command cannot be extended infinitely. If there are N commands, there will be N Command subclasses, which is not very good.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/109153859