Design Mode-command mode

Source: Internet
Author: User

Design Mode-command mode

Command mode: Encapsulate a request as an object, thereby letting you parameterize clients with different request, queue lor log request, and support undoable operations. it encapsulates a request into an object, so that you can use different requests to parameterize the client, queue requests or record request logs, and provide command revocation and recovery functions.

Four roles in command mode:

Command: Declares the commands to be executed. It is an abstraction of a specific Command class.

ConcreteCommand: defines the weak coupling between a receiver and behavior, implements command methods, and calls the corresponding operations of the receiver.

Invoker: receives and executes commands.

Receiver role: Implements and executes a request.

Class Diagram of command mode:

Classes corresponding to each role:

Command interface:

 

Package com. zz. command;/*** Command interface * Copyright May 16, 2015 * created by txxs * all right reserved */public interface command {// public void execute ();}

Specific command class:

 

 

Package com. zz. command;/*** specific Command class * Copyright May 17, 2015 * created by txxs * all right reserved */public class ConcreteCommand implements command {// receiverprivate extends er that executes the Command; // use the constructor to accept the public ConcreteCommand (Receiver er handler) {this. extends ER = extends er;} @ Overridepublic void execute () {this. aggreger. doSomething ();}}

Caller class:

 

 

Package com. zz. command;/*** caller * Copyright May 16, 2015 * created by txxs * all right reserved */public class Invoker {private Command command Command; // set the caller public void setCommand (command Command) {this. command = command;} // execute the command public void action({%this.command.exe cute ();}}

Performer class:

 

 

Package com. zz. command;/*** executor * Copyright May 16, 2015 * created by txxs * all right reserved */public class aggreger {// The Operation public void doSomething () {// action executed }}

Client:

 

 

Package com. zz. command;/*** test class * Copyright May 16, 2015 * created by txxs * all right reserved */public class Client {public static void main (String args []) {// caller Invoker invoker = new Invoker (); // the Receiver's receiver er extends ER = new Receiver Er (); // defines a Command sent to the receiver: command Command = new ConcreteCommand (Receiver ER ); invoker. setCommand (command); invoker. action ();}}

Advantages of command mode:

 

1. decoupling between classes. The command mode decouples the requested object from the requested object.

2. Scalability: because the object initiating the command is completely decoupled from the specific implementation, it is easy to expand the new command. You only need to implement the new command object, and then during assembly, set the specific implementation object to the command object, and then you can use this command object. The existing implementation does not need to be changed at all.

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.