Intention:
Encapsulates a request as an object, allowing the client to be parameterized with different requests, queuing or logging requests, and supporting actions that can be undone
Motivation:
Separate the objects that make the request and the objects that receive and execute the requests.
Effect:
1), Command mode decouple the object that invokes the operation and the object that implements the operation
2), multiple commands can be assembled into a composite command, a composite command is an instance of the composite pattern
3), adding a new command is easy, without changing the existing class
Applicability:
1), abstract the action to be executed to parameterize an object
2), specify, arrange, and execute requests at different times. such as request queue
3), support cancel operation
4), support to modify the log
5), constructs a system with the high-level operation which constructs in the primitive language operation. Supporting Things
Participants:
1), Command
Declares an interface to perform an operation
2), Concretecommand
Binds a recipient object to an action
Invoke the corresponding action of the receiver to implement execute
3), Client
Create a specific command object and set its recipient
4), Invoker
Requires the command to execute this request
5), Receiver
Know how to implement the actions associated with executing a request. Any class may act as a recipient
Structure diagram:
Collaboration:
1), Client creates a Concretecommand object and assigns its receiver object
2), a Invoker object stores the Concretecommand object
3), the Invoker submits a request by invoking the Execute operation of the Command object. If the command is revocable, Concretecommand stores the current state before executing the Execute operation to cancel the command
4), the Concretecommand object invokes its receiver operation to execute the request
The Command object packs the action and the receiver into the object, which exposes only an execute () method, which is performed by the receiver when the method is called. From the outside, other objects do not know exactly which receiver is doing what, only know that if the execute () method is called, the purpose of the request can be achieved.
Realize the source code:
PackageCom.blankjor.command;/*** @desc Command interface *@authorblankjor * @date May 20, 2017 morning 9:49:53*/ Public InterfaceCommand {/*** Execute Command*/ voidExcute (); /*** Undo Command*/ voidundo ();} PackageCom.blankjor.command;/*** @desc Real command performer, recipient *@authorblankjor * @date May 20, 2017 morning 9:52:13*/ Public classReceiver { Public voidaction () {System.out.println ("Executing command ..."); } Public voidunaction () {System.out.println ("Revoking command ..."); }} PackageCom.blankjor.command;/*** @desc Create commands (multiple commands and multiple command executions are possible) *@authorblankjor * @date May 20, 2017 morning 9:54:27*/ Public classCreateCommandImplementsCommand {Privatereceiver receiver; /*** Initialize Command receiver * *@paramreceiver*/ PublicCreateCommand (receiver receiver) { This. Receiver =receiver; } @Override Public voidExcute () {receiver.action (); } @Override Public voidundo () {receiver.unaction (); }} PackageCom.blankjor.command;/*** @desc Command caller *@authorblankjor * @date May 20, 2017 morning 9:59:05*/ Public classInvoker {PrivateCommand command; /*** Command caller holds the command * *@paramCommand*/ PublicInvoker (Command command) { This. Command =command; } /*** Execute Command*/ Public voidRunCommand () {command.excute (); } /*** Undo Command*/ Public voidUnruncommand () {Command.undo (); }} PackageCom.blankjor.command;/*** @desc Command mode Test *@authorblankjor * @date May 20, 2017 morning 10:02:33*/ Public classTestmain { Public Static voidMain (string[] args) {//new A command executor.Receiver receiver =NewReceiver (); //new commandCommand command =NewCreateCommand (receiver); //start invoke commandInvoker Invoker =NewInvoker (command); Invoker.runcommand (); Invoker.unruncommand (); }}
Execution Result:
Common applications:
1. Work queue, thread pool, scheduling
2. Log request (System Recovery)
Points:
1. The command mode decouples the object making the request from the object executing the request
2, in the decoupling between the two is through the command object to communicate. The command object encapsulates the recipient and one or a set of actions
3. The caller makes a request by invoking execute () of the Command object, which causes the receiver's action to be invoked
4, the caller can accept the command as a parameter, even at run time dynamic
5, the command can support revocation, the practice is to implement an undo () method to return to execute () the state before execution
6, the Macro command is a simple extension of the command, allowing multiple commands to be invoked. The macro method can also support undo
7, the actual operation, it is common to use the "smart" command object, that is, the direct implementation of the request, rather than delegating work to the recipient (the disadvantage?). )
8, the command can also be used to implement the log and the thing system
Reference: http://www.cnblogs.com/ikuman/archive/2013/08/06/3233092.html
Java design mode Command mode