Java command mode
There are often examples in real life, such as a bank. In the early years, when I went to the bank to handle business, a bunch of people around the lobby manager asked me where to handle the business ...., one question needs to be explained by people on the every day. Hello, what about this business? You can go to the XX counter for consultation. What about this business? You can go to the XY counter for consultation. Sometimes, an error may occur. I have to go back and forth several times, so it's annoying ...., the manager is helpless, but for the principle of customer first, always smiling. When is the day of relief. Haha, with the development of science and technology, managers have finally been freed from this bitter day, that is, the number server in the business processing area. Now, with simple operations, they can wait patiently for handling, which is convenient for customers, it is a good thing to benefit the masses. Based on the different business processes, the system automatically distributes the information to the respective sales personnel and processes the data by number. To put it bluntly, the above example shows a true portrayal of the command mode. The service handles number pick-up machines and processes customers' requests to the business in a unified manner. For different businesses, arrange for handling in the hands of different salesmen to avoid the previous tedious process.
Definition of command mode: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. (encapsulate a request into an object so that you can use different requests to parameterize the client, queue requests, or record request logs, and provide the command revocation and restoration functions.) To put it bluntly, it is to encapsulate requests into objects to respond to different requests in a unified manner and respond to them..
Command mode:
Explain the keywords of the above diagram:
Client: create a specific command object (ConcreteCommand1 or ConcreteCommand2...) and set its receiver (ConcreteReceiver1 or ConcreteReceiver2 ....)To put it bluntly, it is to write a test class, create a command object and the receiver object to initialize an Invoker, and test the command mode. In real life, a system has different requests, and the system cannot know what Command processing is required for the sent request and the final processor of the Command, just like the Invoker of our project, a general agent is required to ask the contact person for a project-related question, and then the contact person is arranging, which functional group (specific Command) should be arranged by the group and who will handle the problem (Specific handler)? Generally, one party who sends the problem knows which module has a problem, we should ask who to solve the problem, but we all have our own things. If so, let's ask, how can we work. The contact person can make the work orderly. In this way, the receiver just wants us to be the same. The hacker doesn't have to deal with people in other projects directly. It also avoids a lot of trouble and also makes our work, more focused and efficient, no ???
Invoker: requires the Command to execute the request, arrange the person to solve the problem, including the Command attribute parameters, Command set method, and Action () method implemented by the Command execute () method.
Command: Declares the interface for executing an operation. The abstract class ConcreteCommand inherits this Command. What operations should be used to handle this? Which functional group can handle this problem? Include at least one execute () method, or you can add a worker to abstract a layer. Then ConcreteCommand does not need to include a worker, as long as it is initialized.
ConcreteCommand: binds a receiver object to an action, and calls the corresponding operation of the receiver to implement the Execute method,Inherits from the Command to implement the execute method, including the executor, which is used to implement the execute Method
Extends Er: knows how to implement and execute operations related to a request. Any class can be used as a Receiver, class, or abstract class. The abstract class has a ConcreteReceiver implementation.
At the same time, pay attention to the definition. The Command should include the function of revoking execution and restoring State. Specifically, add related methods to the Command and implement them in the specific implementation class.
Code implementation example:
1 public class Invoker 2 {3 private Command command; 4 5/** 6 * set command 7*8 * @ param command to assign a value to command 9 */10 public void setCommand (Command command) 11 {12 this. command = command; 13} 14 15 public void action () 16 {17 command.exe cute (); 18} 19} 20 21 public abstract class Command 22 {23 public abstract void execute (); 24} 25 26 public class ConcreteCommand1 extends Command 27 {28 private extends er; 29/** 30 * @ param explorer 31 */32 public ConcreteCommand1 (assumer Explorer) 33 {34 this. author ER = author er; 35} 36 37/** {@ inheritDoc} */38 public void execute () 39 {40 guest er. doSomething (); 41} 42 43} 44 45 public class ConcreteCommand2 extends Command 46 {47 private aggreger extends er; 48/** 49 * @ param aggreger 50 */51 public ConcreteCommand2 (aggreger) 52 {53 this. extends ER = extends er; 54} 55 56/** {@ inheritDoc} */57 @ Override 58 public void execute () 59 {60 extends er. doSomething (); 61} 62 63} 64 65 public abstract class extends er 66 {67 public abstract void doSomething (); 68} 69 70 public class ConcreteReceiver1 extends er 71 {72 73/** {@ inheritDoc} */74 @ Override 75 public void doSomething () 76 {77 // TODO Auto-generated method stub 78} 79 80} 81 82 public class ConcreteReceiver2 extends er 83 {84 85/** {@ inheritDoc} */86 @ Override 87 public void doSomething () 88 {89 // TODO Auto-generated method stub 90} 91 92} 93 94 public class Client 95 {96 public static void main (String [] args) 97 {98 // first declare the caller Invoker 99 Invoker invoker = new Invoker (); 100 // define the Receiver 101 extends er extends ER = new ConcreteReceiver1 (); 102 // define a Command sent to the receiver 103 command Command = new ConcreteCommand1 (receiver); 104 // send the command to the caller to execute 105 invoker. setCommand (command); 106 invoker. action (); 107} 108}
Advantages and disadvantages of command mode