preface

Command mode is a highly cohesive mode, and the frequency of use is actually not too high. The essence is to encapsulate commands and separate the responsibility of issuing commands from that of executing them.

The command mode encapsulates one or a group of commands into an object, which can transmit function methods as parameters and decouple the direct coupling between the client and the server. It is applicable to simple request queuing, request logging, and support for undoable operations.

directory

A, definitions,

Encapsulating a request into an object allows you to parameterize clients with different requests, queue or log requests, and provide command undo and restore capabilities.

Two, mode principle analysis

Public interface Command {void excute(); //1. } public class Command1 implements Command {private final Receiver Receiver; public Command1(Receiver receiver) { this.receiver = receiver; } @Override public void excute() { receiver.operationA(); }} public class Command2 implements Command {private final Receiver Receiver; public Command2(Receiver receiver) { this.receiver = receiver; } @Override public void excute() { receiver.operationB(); }} public class Command3 implements Command {private final Receiver Receiver; public Command3(Receiver receiver) { this.receiver = receiver; } @Override public void excute() { receiver.operationC(); Public interface Receiver {void operationA(); public interface Receiver {void operationA(); void operationB(); void operationC(); } //4 Specific Receiver public class Receiver1 implements Receiver {@override public void operationA() {system.out.println () A"); } @override public void operationB() {system.out.println (" operationB "); } @override public void operationC() {system.out.println (" operationC "); Public class Invoker {private final List<Command> commands; public Invoker() { commands = new ArrayList<>(); } public void setCommand(Command command) { commands.add(command); } public void run() { commands.forEach(Command::execute); Public class Demo {public static void main(String[] args) {Receiver receiver1 = new receiver1 (); Invoker invoker = new Invoker(); invoker.setCommand(new Command1(receiver1)); invoker.setCommand(new Command2(receiver1)); invoker.setCommand(new Command3(receiver1)); invoker.run(); } // Output the resultCopy the code

Iii. Usage Scenarios

  • You can use the command mode wherever you think there is a command

Four advantages,

  • There is no dependency between the caller role and the receiver role. The caller can only call the execute of the Command

  • Extensibility, subclasses of Command can be very easily extended

  • The Command pattern can be combined with the chain of responsibility pattern to implement Command family resolution tasks, and the template method can also reduce the problem of Command subclass swelling

Five, the disadvantages

  • If you have N commands, and you have N subclasses of Command, you have class inflation

  • Different receivers may need to implement duplicate commands, such as the Open, close, save command for Markdown, and the open, close, save command for non-MarkDown editors