The way of software design pattern -- command pattern

 

In normal development, one method calls another method directly. In this way, there is a close coupling between the method caller and the method executor. This is not conducive to expansion, so we can separate method invocation from method execution, which is the role of command mode

Definition of command pattern: encapsulate a request as an object, separating the responsibility of issuing the request from the responsibility of executing the request. In this way, the two communicate through the command object, which is convenient to store, transfer, call, add and manage the command object

advantage:

  • The coupling degree of the system is reduced by introducing middleware (abstract interface).  
  • It has good expansibility and is very convenient to add or delete commands.
  • Using the command mode, adding and deleting commands will not affect other classes, and meet the "opening and closing principle".  
  • Macro commands can be implemented. Command mode can be combined with combined mode to assemble multiple commands into a combined command, that is, macro command.  
  • It is convenient to realize Undo and Redo operations. The command mode can be combined with the memo mode described later to realize the revocation and recovery of commands.  
  • You can add additional functions to existing commands. For example, logging, combined with decorator mode, will be more flexible.

Disadvantages:

  • A large number of specific command classes may be generated. Because each specific operation needs to design a specific command class, which will increase the complexity of the system.
  •   The result of the command mode is actually the execution result of the receiver. However, in order to structure and decouple the request and implementation in the form of command, additional type structure (the interface between the requester and abstract command) is introduced, which increases the difficulty of understanding. However, this is also a common problem of design patterns. Abstraction will inevitably increase the number of classes. Code separation must be more difficult to understand than code aggregation.

Structure and implementation of command mode

structure

  1. Abstract Command class (Command) role: it declares the interface for executing commands and has the abstract method execute() for executing commands.  
  2. Concrete Command role: it is the concrete implementation class of the abstract command class. It has the receiver object and completes the operation to be executed by calling the receiver's function.  
  3. Implementer / Receiver role: performs operations related to command functions and is the real implementer of specific command object business.  
  4. Caller / requester role: it is the sender of a request. It usually has many command objects and executes related requests by accessing the command object. It does not directly access the receiver.

realization

Implementation scenario: menu ordering.

package com.wly.DesignPatterns;

/**
 * @program: StudyDome
 * @author: yuanzhang
 * @create: 2020-12-30 17:10
 **/
public class CommandPattern {
    public static void main(String[] args) {
        Menu chineseMenu = new ChineseMenu();
        Menu westernStyleMenu = new WesternStyleMenu();
        //A cook came and gave him two menus
        Guest guest = new Guest(chineseMenu,westernStyleMenu);
        System.out.println("The cook ordered a Chinese dish");
        guest.orderchineseFood();
        System.out.println("The cook wants some Western food again");
        guest.orderWesternStyleFood();
    }
}
/**
 * @Annotation:Caller: cook
 * @Author: yuanzhang
 * @Date:  17:12
 */
class Guest{
    private Menu chineseMenu,westernStyleMenu;

    public Guest(Menu chineseMenu, Menu westernStyleMenu) {
        this.chineseMenu = chineseMenu;
        this.westernStyleMenu = westernStyleMenu;
    }
    public void orderchineseFood(){
        chineseMenu.orderFood();
    }
    public void orderWesternStyleFood(){
        westernStyleMenu.orderFood();
    }
}
/**
 * @Annotation:Abstract commands: menus
 * @Author: yuanzhang
 * @Date:  17:15
 */
interface Menu{
    void orderFood();
}
/**
 * @Annotation:Specific command: Chinese menu
 * @Author: yuanzhang
 * @Date:  17:15
 */
class ChineseMenu implements Menu{
    private ChineseChef chineseChef;

    public ChineseMenu(){
        this.chineseChef = new ChineseChef();
    }

    @Override
    public void orderFood() {
        chineseChef.makeChineseFood();
    }
}
/**
 * @Annotation:Specific command: Western menu
 * @Author: yuanzhang
 * @Date:  17:23
 */
class WesternStyleMenu implements Menu{
    private WesternStyleChef westernStyleChef;

    public WesternStyleMenu() {
        this.westernStyleChef = new WesternStyleChef();
    }

    @Override
    public void orderFood() {
        westernStyleChef.makeWesternStyleFood();
    }
}
/**
 * @Annotation:Receiver: Chinese Chef
 * @Author: yuanzhang
 * @Date:  17:41
 */
class ChineseChef{
    public void makeChineseFood(){
        System.out.println("Chinese chef, boil oil in a pot and fry everything");
    }


}
/** 
 * @Annotation:Receiver: Western menu
 * @Author: yuanzhang
 * @Date:  17:41
 */ 
class WesternStyleChef{
    public void makeWesternStyleFood(){
        System.out.println("Western chef, roast a steak");
    }
}

output

The cook ordered a Chinese dish
 Chinese chef, boil oil in a pot and fry everything
 The cook wants some Western food again
 Western chef, roast a steak

Keywords: Design Pattern

Added by sambkk on Fri, 24 Sep 2021 11:31:51 +0300