Definition
Encapsulate a request as an object, thereby leize you parameterize clients with different requests, queue or log requests, and support undoable operations.
Encapsulate requests into objects so that different requests, queues, or logs can be used to parameterize the client, and revocation is also provided.
The command mode decouples the "Action requestor" from the "action performer" object, and communicates with the other two through the command object.
Class Diagram
1) Command
[Java]
Public interface Command {
Public void execute ();
Public void undo ();
}
Public interface Command {
Public void execute ();
Public void undo ();
}
2) ConcreteCommand: The command object binds the action with the executor, which is the communication channel between the requester and the executor. Similar restaurant orders
[Java]
Public class LightOnCommand implements Command {
// Reciever
Light light;
Public LightOnCommand (Light light ){
This. light = light
}
Public void execute (){
Light. on ();
}
Public void undo (){
Light. off ();
}
}
Public class LightOnCommand implements Command {
// Reciever
Light light;
Public LightOnCommand (Light light ){
This. light = light
}
Public void execute (){
Light. on ();
}
Public void undo (){
Light. off ();
}
}
3) Invoker action requestor
The requester does not need to care about how the task is completed. Comparable restaurant attendants
[Java]
Public class Invoker {
// Command
Command cmd;
Public voic setCommand (Command cmd ){
This. cmd = cmd;
}
Public void buttonAction (){
Cmd.exe cute ();
}
Public void undoBtnAction (){
Cmd. undo ();
}
}
Public class Invoker {
// Command
Command cmd;
Public voic setCommand (Command cmd ){
This. cmd = cmd;
}
Public void buttonAction (){
Cmd.exe cute ();
}
Public void undoBtnAction (){
Cmd. undo ();
}
}
4) executor. Comparable restaurant chefs
[Java]
Public class Light {
Public void on ()...
Public void off ()...
}
Public class Light {
Public void on ()...
Public void off ()...
}
5) Client, similar to customers
[Java]
Public class Client {
Public static void main (String [] args ){
// Aggreger
Light light = new Light ("room light ");
// Concrete Command
LightOnCommand cmd = new LightOnCommand (light );
// Invoker
Invoker invoker = new Invoker ();
Invoker. setCommand (cmd );
Invoker. buttonAction ();
}
}
Public class Client {
Public static void main (String [] args ){
// Aggreger
Light light = new Light ("room light ");
// Concrete Command
LightOnCommand cmd = new LightOnCommand (light );
// Invoker
Invoker invoker = new Invoker ();
Invoker. setCommand (cmd );
Invoker. buttonAction ();
}
}
Advantages
1) decoupling:
Invoker and consumer er are completely decoupled and do not need to communicate directly.
The Command object encapsulates the aggreger and its tasks.
2) scalability
Add the sub-class of Command in the future to add the function.
3) Other modes can be structured, such as the responsibility chain mode and template method mode.
Disadvantages
There may be many sub-classes of Command. (It can be solved in combination with the template method)
Use Cases
Can be used in any place involving "commands", such as GUI Button clicking and doscommand
Party A directs Party B to add page elements .. Customers order meals in the restaurant ..