First, what is? effect
1. Mode Command "request" encapsulated into objects, to use a different request queue logs or other objects, then the parameters, since the command mode is also supported undone action
2. encapsulates the request into a command object, the specific command executed by the execution request of the recipient;
effect:
Decoupling the sender of the command executor ;
Each command is an operation
Class 3. FIG.
The Invoke (caller): The caller is responsible for command execution
The Command (Command Interface): responsible packaged into a unified operation method
Concreatecommand (command implementation class): responsible for the operation command to encapsulate the true executor
Receiver real executor command: (the recipient)
Second, the example
Code Scene: manufacturers to provide a remote control, the remote control has a switch button, you need to add this remote control different electrical appliances, such as: light, fan, air conditioning, the door
Code Analysis: The electrical command mode operation request encapsulated into objects
Real executor command: Light
/ ** * Light (Receiver) actually executed by the command * / public class Light { public void ON () { System.out.println ( "lights" ); } public void OFF () { System.out.println ( "lights" ); } }
Command Interface: There are two buttons above operation, of course, you can also add other operations, such as: undo
/ ** * command interface * / public interface the Command { void Execute (); }
The turn-on request command encapsulated in the object, if an operation of the other electrical command may be packaged as objects
/ ** * turn-on command ( LinghtOnCommand )
* /
public class LinghtOnCommand the implements the Command {
Private Light Light; // used herein decoupling delegate
public LinghtOnCommand (Light Light) { the this .light = Light;}
@Override
public void Execute () { light.on () ;}
}
(Remote Control) command to the caller: the install command on the remote control, while providing pressing the button, the command is executed
/ ** * remote * command caller Invoke * / public class the CommandController { Private the Command SOLT; public void the setCommand (the Command Command) { SOLT = Command; } / ** * remote control button pressed * / public void buttonWasPressed () { solt.execute (); } }
The client calls:
/ ** * test client * / public class MainClient { public static void main (String [] args) { the CommandController the CommandController = new new the CommandController (); Light Light = new new Light (); LinghtOnCommand linghtOnCommand = new new LinghtOnCommand (Light); the CommandController .setCommand (linghtOnCommand); commandController.buttonWasPressed (); } }
// console display
lights
Third, the summary
1. Summary
- The command mode request objects (the CommandController) and performs the requested object Decoupling
- Invoke the command object's execute (), the action will make the recipient will be executed
- Command mode is also supported revocation , the practice is to implement an undo command, returned execute () state before being executed
2. More use command mode:
- Queue request: a command taken out from the queue, calls its Execute (), wait for the call is completed, a command to discard the object, and then remove a command
- Log request
- Undo previous operation: Transaction
- Thread Pool
- 。。。