JAVA design mode-command mode

Source: Internet
Author: User

Command mode

Encapsulate requests as objects to parameterize other objects using different requests, queues, or logs. The command mode also supports unrecoverable operations.

We know that each command object encapsulates a request by binding a group of actions to a specific receiver. To achieve this, the command object includes the action and receiver into the object. This object exposes only one execute () method. When this method is called, the receiver will perform these actions. From the outside, other objects do not know which receiver has performed what actions. They only know that if the execute () method is called, the request can be fulfilled.

Example

All Command objects implement the same interface: Command. java

[Java]
Package com. designpattern. command;
 
Public interface Command {
Public void execute ();
Public void undo ();

}

Light. java
[Java]
Package com. designpattern. command;
 
Public class Light {
Private boolean on = false;
 
Public void on (){
This. on = true;
}
 
Public void off (){
This. on = false;
}
 
Public String getStatus (){
If (on ){
Return "ON ";
} Else {
Return "OFF ";
}
}
}

OnCommand. java defines the binding relationship between the action and the receiver. The caller can send a request by calling the execute () method, and then call one or more actions of the receiver.
[Java]
Package com. designpattern. command;
 
Public class OnCommand implements Command {
Private Light light;
 
Public OnCommand (Light l ){
This. light = l;
}
 
@ Override
Public void execute (){
Light. on ();
}
 
@ Override
Public void undo (){
Light. off ();
System. out. println ("The light is off! ");
}
 
}
Command object for turning off the light: OffCommand. java
[Java]
Package com. designpattern. command;
 
Public class OffCommand implements Command {
 
Private Light light;

Public OffCommand (Light l ){
This. light = l;
}

@ Override
Public void execute (){
Light. off ();
}
 
@ Override
Public void undo (){
Light. on ();
System. out. println ("The light is on! ");
}
 
}

Empty command: NoCommand. java, transfers the responsibility for processing null to the empty command object. When calling its execute () method, this object does nothing.
[Java]
Package com. designpattern. command;
 
Public class NoCommand implements Command {
 
@ Override
Public void execute (){
// TODO Auto-generated method stub
 
}
 
@ Override
Public void undo (){
// TODO Auto-generated method stub
 
}
 
}

Caller: Invoker. java, holding a command object, and calling the execute () method of the command object at a certain point in time to put the request into practice.
[Java]
Package com. designpattern. command;
 
// Caller
Public class Invoker {
 
Private Command command;
 
Private Command undoCommand;
 
Public Invoker (){
Command = new NoCommand ();
UndoCommand = new NoCommand ();
}
 
Public void setCommand (Command c ){
This. command = c;
}
 
Public void pressButton (){
This.command.exe cute ();
UndoCommand = command;
}
 
Public void undoPress (){
UndoCommand. undo ();
}
}

Test class: Test. java, responsible for creating a specific command and setting its receiver.
[Java]
Package com. designpattern. command;
 
Public class Test {
 
/**
* @ Param args
*/
Public static void main (String [] args ){

Light light = new Light ();

Command on = new OnCommand (light); // The specific Command. light is the receiver.
Command off = new OffCommand (light );

Invoker invoker = new Invoker ();
Invoker. setCommand (on );
Invoker. pressButton ();
Invoker. undoPress ();

System. out. println ("Light" + light. getStatus ());
}
 
}

Summary
1. In command mode, the requested object and the requested object are decoupled.

2. the decoupled two are communicated through command objects. The command object encapsulates the receiver and one or more actions.

3. The caller sends a request by calling the execute () method of the command object, which will call the receiver's action.

4. The caller can receive commands as parameters, or even dynamically execute them at runtime.

5. commands can be undone by implementing an undo () method to return to the status before execute () is executed.

6. Macro commands are a simple extension of commands, allowing you to call multiple commands. The macro method also supports revocation.

7. In actual operations, it is common to use the "smart" command object, that is, directly implementing the request, rather than entrusting the work to the recipient.

8. commands can also be used to implement logging and transaction systems.

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.