【Design mode】——17, command mode

(17) Command mode

Command pattern refers to the encapsulation of commands. The command pattern decouples the requester and the receiver. The requester only needs to execute the request command, and does not need to care how the request is executed and whether it is executed.

1. Design principles of command mode

Receiver: implements or executes a request;

Command role (ICommand): defines all commands that need to be executed;

Concrete command (ConcreateCommand): maintains a Receiver internally, and calls the related methods of the Receiver in the execution method;

Invoker: Receives commands and executes them.

image-20210316211517382

2. Simple example

The play function, pause function, and stop function in the player are all executed by the control bar command passed to the player core, rather than the player itself.

public class GPlayer {
    
    
//    这里模仿的是播放器内核我们的receiver,他能够直接执行相关指令
    public void play(){
    
    
        System.out.println("播放");
    }
    public void pause(){
    
    
        System.out.println("暂停");
    }
    public void stop(){
    
    
        System.out.println("停止");
    }
}
public interface IAction {
    
    
    void execute();
}
public class PlayAction implements IAction {
    
    
//    内部维护一个接收者对象
    private GPlayer gPlayer;
//    构造方法
    public PlayAction(GPlayer gPlayer){
    
    
        this.gPlayer = gPlayer;
    }
    @Override
    public void execute() {
    
    
        gPlayer.play();
    }
}
public class PauseAction implements IAction {
    
    
    private GPlayer gPlayer;

    public PauseAction(GPlayer gPlayer){
    
    
        this.gPlayer = gPlayer;
    }
    @Override
    public void execute() {
    
    
        gPlayer.pause();
    }
}
public class StopAction implements IAction {
    
    
    private GPlayer gPlayer;

    public StopAction(GPlayer gPlayer){
    
    
        this.gPlayer = gPlayer;
    }
    @Override
    public void execute() {
    
    
        gPlayer.stop();
    }
}
public class Controller {
    
    
//    即我们的invoker
    private List<IAction> actionList = new ArrayList<>();
    public void addAction(IAction action){
    
    
        actionList.add(action);
    }
    public void execute(IAction action){
    
    
        action.execute();
    }
//    批量执行命令
    public void executes(){
    
    
        for(IAction action:actionList){
    
    
            action.execute();
        }
        actionList.clear();
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        GPlayer gPlayer = new GPlayer();
        Controller controller = new Controller();
        controller.execute(new PlayAction(gPlayer));

        controller.addAction(new PauseAction(gPlayer));
        controller.addAction(new StopAction(gPlayer));
        controller.executes();
    }
}

image-20210316215959241

3. Comments on command mode

The essence of the command pattern is to encapsulate the command and separate the responsibility of issuing the command from the responsibility of executing the command.

Other references:

https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/command.html

Guess you like

Origin blog.csdn.net/qq_40589204/article/details/118343209