[Design mode Summary]. Command mode

Source: Internet
Author: User

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 ..

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.