Correct discussion:
Command mode encapsulates a request or operation into an object. The command mode runtime system uses different requests to parameterize the client, queue requests or record request logs, and can provide command revocation and recovery functions.
popular:
Actually it's easy to understand. Command mode, what you care about is commands (or operations). Let's give an example. In a company, the whole operation is like a system. A boss issued an order, and the middle-level leader received the order and then assigned it to the specific employee in charge. The whole process is clear. There is a requirement, how to fix this process and form a system. We just need to grasp the key point: order. Extract it and everything else will be solved. Extract the commands and encapsulate them into an independent object to achieve decoupling. As for the others, it can be easily expanded, whether this command was proposed by the CEO, the Human Resources Department, or your dad. Regardless of whether the executor of this order is Zhang San or turtle. The emergence of this model is actually a philosophical "grasping the main contradiction". More examples, such as the other writers cited "What is the difference between going to the roadside and going to a barbecue restaurant to eat barbecue" or "The Monkey King makes a big fuss about the Jade Emperor in the Heavenly Palace to capture monkeys with Taibai Venus".
The Java command mode is essentially an encapsulation of commands, which separates the responsibility for issuing commands and delegates them to different objects. In layman's terms, I am the boss, so I just send an order. As for whom to send this order to and who execute it, it's up to me. I don't send money to hire someone to cause trouble for myself. You are the employee in charge of things, and your duty is to do the tasks assigned by your superiors. Be down-to-earth, don’t know too much, don’t gossip, don’t ask too much.
accomplish:
Let's take a look at the class diagram first:
Based on this we look at the sample code:
public interface ICommand { void execute(); } public class ConcreteCommand implements ICommand { private Receiver receiver; public ConcreteCommand(Receiver receiver) { this.receiver = receiver; } @Override public void execute() { this.receiver.action(); } } public class Receiver { public void action() { System.out.println("receiver do something"); } } public class Invoker { private ICommand command; public Invoker(ICommand command) { this.command = command; } public void invoke() { this.command.execute(); } } public class Test { public static void main(String[] args) { Receiver receiver = new Receiver();//The real executor ICommand command = new ConcreteCommand(receiver);//The command used for isolation Invoker invoker = new Invoker(command);//The caller invoker.invoke(); } }
advantage:
shortcoming:
There may be too many specific command classes.