Definition of command mode
Encapsulates a request as an object that allows the client to be parameterized with different requests, to queue or log the request, and to support revocable operations, separating the "requesting object" and "the object receiving and executing the request".
Applicability of command mode
1. Abstract the action to be executed to parameterize an object.
2. Specify, arrange, and execute requests at different times.
3. Cancel operation is supported.
4. Support to modify the log so that when the system crashes, such modifications can be re-done again.
5. Construct a system with high-level operations built on primitive operations.
Command-mode application effect:
1) The command mode decouples the object that invokes the operation and the object that implements the operation
2) Multiple commands can be assembled into a composite command, which is an instance of the composite pattern
3) It is easy to add a new command without changing the existing class
Command-mode participants
1.Command
Declares an interface that performs an operation.
2.ConcreteCommand
Binds a recipient object to an action.
Invokes the corresponding action of the receiver to implement execute.
3.Client
Create a specific command object and set its recipient.
4.Invoker
This command is required to execute the request.
5.Receiver
Know how to implement an operation that is related to executing a request. Any class can be a recipient.
UML class diagram for Command mode
The specific code implementation:
First step: Define the command and declare the interface to perform the operation
1 // 2 public interface Command { 3 4 take action 5 public void Excute (); 6 Undo Operation 7 public void UnDo (); 8 }
Step two: Define Concretecommand and bind a recipient object to an action.
1 Public classConcretecommandImplementsCommand {2 3 //holds a reference to a recipient receiver, binds a specific operation4 receiver receiver;5 6 PublicConcretecommand (receiver receiver) {7 This. Receiver =receiver;8 }9 Ten @Override One Public voidExcute () { A //Recipient binding Execution Action - receiver.action (); - } the - @Override - Public voidUnDo () { - //revocation action for recipient bindings + receiver.unaction (); - } +}
Step three: Define receiver and know how to implement the actions associated with executing a request
1 Public classReceiver {2 3 Public voidaction () {4System.out.println ("Execute a command");5 }6 7 Public voidunaction () {8System.out.println ("Undo a Command");9 }Ten}
Fourth step: Define Invoker, which requires the command to execute the request.
1 Public classInvoker {2 3 //The caller holds a command object4 command command;5 6 PublicCommand GetCommand () {7 returncommand;8 }9 Ten Public voidsetcommand (Command command) { One This. Command =command; A } - - //Execute Command the Public voidExcutecommmand () { - Command.excute (); - } - + //Undo Command - Public voidUndocommand () { + Command.undo (); A } at}
Fifth step: Define Clien, let's test our command pattern.
1 Public classClient {2 3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //create the recipient of the command8Receiver receiver =NewReceiver ();9 //Create a Command object and set its recipientTenCommand command =NewConcretecommand (receiver); One //creates the command performer and passes the corresponding command as a parameter to the Invoker AInvoker Invoker =NewInvoker (); - invoker.setcommand (command); - //Client Side Test the Invoker.excutecommmand (); - Invoker.undocommand (); - } - +}
Operation Result:
Execute a command to undo a command
Summarize:
1, in the decoupling between the two is through the command object to communicate, the command object encapsulates the receiver and a group of actions;
2. The caller makes a request by invoking execute () of the Command object, which causes the receiver's action to be invoked;
3, the caller can accept the command as a parameter, even at run time dynamic;
4, the command can support revocation, the practice is to implement an undo () method to return to execute () the state before execution;
5. The command mode decouples the requesting object from the object executing the request;
Command mode for the Java Design Pattern series