Hello, everyone, I am Hong Jue, you look at the stars in the sky, is not arranged into four characters: design mode.

Today I’m going to talk about command patterns, and command patterns are behavioral patterns, and behavioral patterns are particularly concerned with communication between objects.

So what does command mode do and what does it do? Without further ado, gogogo!

Command mode can be seen everywhere. For example, when you press the air conditioner remote control, the air conditioner will do the corresponding command. For example, if you type Java -version, then if you successfully install Java, it will respond to your command by printing the corresponding version number to the screen. On Linux, type ls, CD, cat, etc.

So since command patterns are so common, we’re not learning yet! Implement your own Java-version!

The command pattern places the request as a command (signal) in an object (button) and passes it to the calling object (TV remote control), which looks for an object (TV) that can handle the command and passes the command to the object to execute the corresponding command.

Has anyone ever wondered why the signal (property) is already in the button (object) and can’t be sent directly to the TV instead of going through the TV remote control?

Because of the over-coupling, the over-coupling of the behavior requester and the behavior implementer can lead to problems later on, such as if I want to record, undo, or redo my behavior now, this highly coupled system design becomes difficult to implement.

Class diagram:

Last wave of code, thoroughly understand the command pattern.

public class Computer {

    public void getResponse(a) {
        System.out.println("Computer response"); }}public abstract class Command {
    protected String key;

    void execute(a) {}}public class ClickCommand extends Command {

    Computer computer;

    public ClickCommand(Computer computer) {
        key = "click";
        this.computer = computer;
    }

    @Override
    public void execute(a) { computer.getResponse(); }}public class SlidingRollerCommand extends Command {
    Computer computer;

    public SlidingRollerCommand(Computer computer) {
        key = "sliding";
        this.computer = computer;
    }

    @Override
    public void execute(a) { computer.getResponse(); }}public class Mouse {
    
    // Store the corresponding command
    Map<String, Command> commandMap = new ConcurrentHashMap<>();
    
	// Stores log information
    List<String> bizLog = new ArrayList<>();

    public Mouse(a) {}private void logs(String key, String msg) {
        System.out.println(key + msg);
        bizLog.add(key + msg);
    }

    public void showLogs(a) {
        for(String log : bizLog) { System.out.println(log); }}public void addCommand(String key, Command command) {
        if(! Objects.isNull(commandMap.get(key))) {return;
        }
        logs(key, "It's a new order.");
        commandMap.put(key, command);
    }

    public void action(String key) {
        if(Objects.isNull(commandMap.get(key))) {
            return;
        }
        Command command = commandMap.get(key);

        logs(key, "Executed"); command.execute(); }}public class client {
    public static void main(String[] args) {
        // Command receiver
        Computer computer = new Computer();

        // Specify the command
        Command clickCommand = new ClickCommand(computer);
        Command slidingRollerCommand = new SlidingRollerCommand(computer);

        // Execute the command
        Mouse mouse = new Mouse();
        mouse.addCommand(clickCommand.key, clickCommand);
        mouse.addCommand(slidingRollerCommand.key, slidingRollerCommand);

        mouse.action(clickCommand.key);

        mouse.action(slidingRollerCommand.key);

        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"); mouse.showLogs(); }} click is a new command. Sliding is a new command. So click is executed and the computer responds Click is performed sliding is performedCopy the code

Listen to the applause! There are many more examples like this, you can try to implement a version of your own.

Well, that’s the end of the command mode for this installment. I hope you found this article helpful!

May everyone read this article with skepticism and explore how it works.

Road obstruction and long, the past for preface, the future for chapter.

Looking forward to our next meeting!