Design mode - command mode

Posted by lizzyd on Tue, 21 Dec 2021 06:05:32 +0100

1, Basic introduction

  • English Name: Command
  • Concept: encapsulate a request into an object, so that you can parameterize the client with different requests, queue requests or record request logs, and provide command revocation and recovery functions
  • Mode type: structural
  • Design principle:
  • Main features:

2, Realize

Requirements: requirements change of development project

  • It is necessary to modify the requirements document, adjust the page, change the code, etc

1. General implementation

I. code
public interface Group {
    //Party A and Party B work separately. If you want to discuss with a group, you must first find the group
    void find();

    //Required to add functions
    void add();

    //Requested to delete function
    void delete();

    //Required to modify function
    void change();

    //All change plans are required
    void plan();
}

public class RequirementGroup implements Group {
    @Override
    public void find() {
        System.out.println("Find requirement group...");
    }

    @Override
    public void add() {
        System.out.println("The customer asked for an additional demand...");
    }

    // ... Omit the remaining method implementations 
}

public class CodeGroup implements Group {
    @Override
    public void find() {
        System.out.println("Code group found...");
    }

    @Override
    public void add() {
        System.out.println("The customer requested to add a function...");
    }
    
    // ... Omit the remaining method implementations
}

public class Client {
    public static void main(String[] args) {
        //First, the customer went to the requirements group and said, come and talk about the requirements and modify them
		System.out.println("-----------The customer asked for an additional demand---------------"); 
        Group rg = new RequirementGroup(); 
        //Find requirement group 
        rg.find(); 
        //Add a demand 
        rg.add(); 
        //Request change plan 
        rg.plan();
    }
}
II. Advantages
III. disadvantages
  • The execution of each command needs to find the executor one by one, and the steps of command execution need to be clear

2. Pattern

  • Receive recipient role

    This role is the working role, and the command should be executed when it is passed here

  • Command role

    All commands that need to be executed are declared here

  • Invoker caller role

    Receive the command and execute the command

I. code
Abstract receiver
public interface Group {
    //Party A and Party B work separately. If you want to discuss with a group, you must first find the group
    void find();

    //Required to add functions
    void add();

    //Requested to delete function
    void delete();

    //Required to modify function
    void change();

    //All change plans are required
    void plan();
}
Specific recipient
public class RequirementGroup implements Group {
    @Override
    public void find() {
        System.out.println("Find requirement group...");
    }

    @Override
    public void add() {
        System.out.println("The customer asked for an additional demand...");
    }

    // ... Omit the remaining method implementations 
}

public class CodeGroup implements Group {
    @Override
    public void find() {
        System.out.println("Code group found...");
    }

    @Override
    public void add() {
        System.out.println("The customer requested to add a function...");
    }
    
    // ... Omit the remaining method implementations
}
Abstract command
public abstract class Command {
    // Aggregate recipient
    protected Group group;

    public Command(Group group) {
        this.group = group;
    }

    public abstract void execute();
}
order of the day
public class AddRequirementCommand extends Command {

    // Specific commands aggregate the executors of the command by default
    public AddRequirementCommand() {
        super(new RequirementGroup());
    }

    public AddRequirementCommand(Group group) {
        super(group);
    }

    @Override
    public void execute() {
        super.group.find();
        super.group.add();
    }
}

public class AddCodeCommand extends Command {

    public AddCodeCommand() {
        super(new CodeGroup());
    }

    public AddCodeCommand(Group group) {
        super(group);
    }

    @Override
    public void execute() {
        super.group.find();
        super.group.add();
    }
}
caller
public class Invoker {

    private Command command;

    // Execute command
    public void action() {
        this.command.execute();
    }

    public Command getCommand() {
        return command;
    }

    public void setCommand(Command command) {
        this.command = command;
    }
}
scene
public class Client {
    public static void main(String[] args) {
        Invoker invoker = new Invoker();
        Command command = new AddRequirementCommand();
        invoker.setCommand(command);
        invoker.action();
    }
}
II. Advantages
  • Interclass decoupling

    There is no dependency between the caller role and the receiver role. When implementing the function, the caller only needs to call the execute method of the Command abstract class without knowing which receiver is executing

  • Scalability

    The subclass of Command can be easily extended, and the caller Invoker and the high-level module Client do not produce serious code coupling

  • Command mode is better combined with other modes

    The Command mode can be combined with the responsibility chain mode to realize the Command family analysis task; Combined with the template method pattern, the expansion of the Command subclass can be reduced

III. disadvantages
  • If there are N commands, the subclasses of Command are not several, but N. this class expands greatly, which needs to be carefully considered in the project

3, Usage scenario

1. Common application scenarios

Ⅰ,

Topics: Design Pattern