Command mode in Design Mode

Source: Internet
Author: User

In many designs, one object is often designed to request another object to execute a certain operation. If the requester cannot or does not want to directly deal with the respondent, that is, the request object cannot or does not want to contain the reference of the respondent, you can use the command mode. In command mode, the request object is called the requester, and the requested object is the receiver. In command mode, when an object requests another object to call its method, it does not directly deal with the request, but encapsulates this "request" into a "command" object, encapsulation means to encapsulate the "request" into a method of the "command" object. The core of the command mode is to use command objects to encapsulate method calls.

For example, when the army is in war, the commander must order three to attack the enemy. However, if the commander cannot or does not want to directly contact the three connections, the commander can issue a command to set the executor of the command to three connections. In this way, the commander only needs to deal with commands.

The command mode contains four roles:

Receiver: the receiver is an instance of the class, which is responsible for performing request-related operations.

Command interface: a command interface that specifies several methods used to encapsulate "requests", such as execute () and undo.

Specific commands: an instance of the class that implements the command interface in a specific command type, including the method of the command interface. It also contains the reference of the recipient, indicating the object to execute the command.

Requester: the requester is an instance containing the command interface variable class, which can store reference of any specific command. The requester is responsible for calling specific commands to execute the encapsulated request methods.

The following is an example of military operations:

1. Command receiver:

1 package COM. command; 2 // command receiver and executor 3 Public class companyarmy {4 Public void sneakattack () {5 system. out. println ("we know how to attack the enemy and ensure that tasks are completed"); 6} 7}

2. command interface:

1 package com. Command; 2 // command interface 3 Public interface command {4 void execute (); 5}

3. Specific commands:

1 package COM. command; 2 // specific command 3 Public class concretecommand implements command {4 companyarmy army; // reference containing the recipient 5 Public concretecommand (companyarmy army) {6 this. army = army; 7} 8 Public void execute () {// encapsulate the commander's request 9 army. sneakattack (); // attack the enemy 10} 11}

4. requester:

1 package COM. command; 2 // the requester, that is, the command sender 3 Public class armysuperior {4 command; // store the reference of a specific command 5 Public void setcommand (command) {6 this. command = command; 7} 8 Public void startexecutecommand () {9 command.exe cute (); 10} 11}

The four roles required for the command mode have been created. Let's test them as follows:

1 package COM. command; 2 3 Public class application {4 public static void main (string [] ARGs) {5 companyarmy army = new companyarmy (); // CREATE Command receiver 6 command = new concretecommand (Army); // create a specific command and specify receiver 7 armysuperior superior = new armysuperior (); // CREATE Command requestor 8 superior. setcommand (command); // set a specific command for the requester 9 superior. startexecutecommand (); // start to execute the command 10} 11}

In this way, the commander does not directly communicate with the command executor, but only needs to send a command to find the designated executor to execute the command. This greatly reduces the Coupling Degree of the program. Another advantage is that if you want to send another command, you only need to create another specific command. You do not need to modify other code, which enhances the scalability of the program.

Another benefit of using the command mode is that you can revoke the executed operation. That is, the requester sends a request. After the receiver executes the request, it can also cancel the operation. The following uses a simple example to illustrate how to implement the Undo () method in a specific command. The problem is as follows:

The requester requests to create a directory on the hard disk. If the request is successful, the request can be revoked. This requires the recipient not only to create a directory on the hard disk, but also to delete the last directory.

1 recipient: contains two methods: creating a directory and deleting a directory.

1 package COM. command1; 2 Import Java. io. file; 3 // command receiver 4 public class makedir {5 // create directory 6 Public void createdir (string name) {7 file dir = new file (name); 8 dir. mkdir (); 9} 10 // Delete directory 11 Public void deletedir (string name) {12 file dir = new file (name); 13 dir. delete (); 14} 15}

 

2. command INTERFACE

1 package com. command1; 2 // contains the undo command interface 3 Public interface command {4 void execute (string name); 5 void undo (); 6}

 

3. Specific commands

1 package COM. command1; 2 Import Java. util. arraylist; 3 // specific command 4 public class concretecommand implements command {5 arraylist <string> dirnamelist; 6 makedir; 7 public concretecommand (makedir) {8 dirnamelist = new arraylist <string> (); 9 this. makedir = makedir; 10} 11 Public void execute (string name) {12 makedir. createdir (name); 13 dirnamelist. add (name); 14} 15 public void undo () {16 I F (dirnamelist. size ()> 0) {17 makedir. deletedir (dirnamelist. get (dirnamelist. size ()-1); 18 dirnamelist. remove (dirnamelist. size ()-1); 19} else {20 system. out. println ("no action to undo! "); 21} 22} 23}

 

4. requester

1 package COM. command1; 2 // requester 3 Public class requestmakedir {4 command; 5 Public void setcommand (command) {6 this. command = command; 7} 8 Public void startexecutecommand (string name) {9 command.exe cute (name); 10} 11 Public void undocommand () {12 Command. undo (); 13} 14}

The four roles that contain the undo command mode have been created. The following is a test class:

Package COM. command1; public class application {public static void main (string [] ARGs) {makedir = new makedir (); // create the receiver command = new concretecommand (makedir ); // create a specific command and specify the receiver requestmakedir request = new requestmakedir (); // create the requester request. setcommand (command); // set the command request. startexecutecommand ("Haha"); // create a directory request. startexecutecommand ("hahaa"); Request. undocommand (); // cancel the request. undocommand ();}}

In this way, you can create a directory and cancel the operation.

  

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.