Java and mode 26-21st-command mode

Source: Internet
Author: User
Tags rewind
Document directory
  • Source code
  • Source code
  • Source code

Command mode is the behavior mode of the object. The command mode is also called action mode or transaction mode.

In command mode, a request or operation is encapsulated into an object. The command mode allows the system to parameterize the client using different requests, queue requests or record request logs, and provides command revocation and recovery functions.

Command mode structure

  Command mode is the encapsulation of commands. The command mode separates the responsibility for issuing commands from the responsibility for executing commands and delegates them to different objects.

Each Command is an operation: the Requesting Party sends a request to execute an operation; the receiving party receives the request and performs the operation. The command mode allows the requester and the receiver to be independent, so that the requester does not have to know the interfaces of the receiver, or how the request is received, and whether the operation is executed, when it is executed, and how it is executed.

Commands allow the requested party and the party receiving the request to evolve independently and thus have the following advantages:

(1) The command mode makes it easy to add new commands to the system.

(2) The requesting party is allowed to decide whether to reject the request.

(3) It is easier to design a command queue.

(4) The request can be easily revoked and restored.

(5) When necessary, it is easier to log commands.

The following uses a schematic system to describe the structure of the command mode.

The command mode involves five roles:

Client role:Create a concretecommand object and determine its receiver.

Command role:Declares an abstract interface for all specific command classes.

Specific command (concretecommand) role:Defines the weak coupling between a receiver and a behavior. implements the execute () method and calls the corresponding operations of the receiver. The execute () method is usually called the execution method.

Invoker role:Calls command objects to execute requests. The related methods are called Action methods.

Receiver role:Implements and executes a request. Any class can be the receiver. The method for implementing and executing requests is called the action method.

Source code

Receiver role

Package COM. bankht. command;/*** @ Author: -AK47 * @ Creation Time: 11:41:21, January 1, ** @ Class description: recipient's role class */public class extends er {/*** truly executes the corresponding command operation */Public void action () {system. out. println ("executed operations ");}}

 

Abstract command role

Package COM. bankht. command;/*** @ Author: -AK47 * @ Creation Time: 11:42:00, January 1, ** @ Class description: abstract command role class */public interface command {/*** execution Method */void execute ();}

 

Specific command Angle

Package COM. bankht. command;/*** @ Author: -AK47 * @ Creation Time: 11:42:23, January 1, ** @ Class description: specific command Angle class */public class concretecommand implements command {// hold the corresponding recipient object private receiver er extends ER = NULL;/*** constructor */Public concretecommand (receiver extends ER) {This. extends ER = extends er;} @ overridepublic void execute () {// It usually calls the corresponding method of the receiver object, so that the receiver can truly execute the function of the receiver. action ();}}

 

Requester role

Package COM. bankht. command;/*** @ Author: -AK47 * @ Creation Time: 11:42:45, January 1, ** @ Class description: requester's Angle class */public class invoker {/*** holding command object */private command = NULL;/*** constructor */Public invoker (command) {This. command = command;}/*** Action Method */Public void action () using command.exe cute ();}}

Client role

Package COM. bankht. command;/*** @ Author: -AK47 * @ Creation Time: 11:41:21, January 1, ** @ Class description: recipient's role class */public class extends er {/*** truly executes the corresponding command operation */Public void action () {system. out. println ("executed operations ");}}

 

Audioplayer System

Julia has a video recorder with play, rewind, and stop functions. The Recorder's keyboard is the invoker role; julia is the client role, while the recorder is the receiver role. The command class plays the abstract command role, while playcommand, stopcommand, and rewindcommand are specific command classes. Julia does not need to know how the play, rewind, and stop functions are executed. The details of these commands are all implemented by the keyboard (keypad) implementation. Julia only needs to press the corresponding key on the keyboard.

A recorder is a typical command mode. The recorder buttons separate the operation details of the client and the recorder.

Source code

Receiver role, played by the recorder class

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:30:19, December 2, ** @ Class description: receiver role, played by the recorder class */public class audioplayer {public void play () {system. out. println ("play... ");} public void rewind () {system. out. println ("rewind... ");} public void stop () {system. out. println ("stop... ");}}

 

Abstract command role

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:30:44, January 2, ** @ Class description: abstract command role class */public interface command {/*** execution Method */Public void execute ();}

Specific command Angle

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:31:27, January 2, ** @ Class description: specific command Angle class-broadcasting */public class playcommand implements command {private audioplayer myaudio; Public playcommand (audioplayer) {myaudio = audioplayer ;} /*** execution Method */@ overridepublic void execute () {myaudio. play ();}}

 

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:32:40, January 2, ** @ Class description: specific command Angle class-inverted tape */public class rewindcommand implements command {private audioplayer myaudio; Public rewindcommand (audioplayer) {myaudio = audioplayer ;}@ overridepublic void execute () {myaudio. rewind ();}}

 

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:33:25, January 2, ** @ Class description: specific command Angle class-stop */public class stopcommand implements command {private audioplayer myaudio; Public stopcommand (audioplayer) {myaudio = audioplayer;} @ overridepublic void execute () {myaudio. stop ();}}

 

The requester role, played by the keyboard.

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:35:52, January 2, ** @ Class description: requester role, played by the keyboard class */public class keypad {private command playcommand; private command rewindcommand; private command stopcommand; Public void setplaycommand (command playcommand) {This. playcommand = playcommand;} public void setrewindcommand (command rewindcommand) {This. rewindcommand = rewindcommand;} public void setstopcommand (command stopcommand) {This. stopcommand = stopcommand;}/*** execution Playback Method */Public void play () implements playcommand.exe cute () ;}/ *** execution of the rewind Method */Public void rewind () repeated rewindcommand.exe cute ();}/*** method of playing the video */Public void stop () Stopping stopcommand.exe cute ();}}

 

Client role, played by Julie

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:36:17, January 2, ** @ Class description: client role, played by Julie */public class Julia {public static void main (string [] ARGs) {// create the receiver object audioplayer = new audioplayer (); // create the command object command playcommand = new playcommand (audioplayer); command rewindcommand = new rewindcommand (audioplayer); command stopcommand = new stopcommand (audioplayer ); // create the requester object keypad = new keypad (); keypad. setplaycommand (playcommand); keypad. setrewindcommand (rewindcommand); keypad. setstopcommand (stopcommand); // test keypad. play (); keypad. rewind (); keypad. stop (); keypad. play (); keypad. stop ();}}

The running result is as follows:

Play... rewind... stop... play... stop...

 

Macro command

A macro command is a combination of multiple commands.

Imagine Julie's recorder has a record function that records commands one by one and re-executes these recorded commands at a time as needed, this is the so-called macro command set function. Therefore, Julie's recorder system now has four keys: Broadcasting, ripple, stop, and macro command. At this time, the system design is enhanced compared with the previous design, mainly because the Julia class now has a new method to operate the macro command key.

Source code

The system needs an interface that represents the macro command to define the interface required by the macro command.

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:46:04, December 2, ** @ Class description: The system needs an interface that represents a macro command, to define the interface required to issue the macro command. */Public interface macrocommand extends command {/*** macro command aggregation management method can add a member command */Public void add (command cmd ); /*** macro command aggregation management method can delete a member command */Public void remove (command cmd );}

 

The specific macro command macroaudiocommand class is responsible for combining individual commands into macro commands.

 

Package COM. bankht. command. recorder; import Java. util. arraylist; import Java. util. list;/*** @ Author: -AK47 * @ Creation Time: 02:46:38, December 2, ** @ Class description: The macroaudiocommand class is used to synthesize some commands into macro commands. */Public class macroaudiocommand implements macrocommand {private list <command> commandlist = new arraylist <command> (); /*** macro command aggregation Management Method */@ overridepublic void add (command cmd) {commandlist. add (CMD);}/*** macro command aggregation Management Method */@ overridepublic void remove (command cmd) {commandlist. remove (CMD);}/*** execution Method */@ overridepublic void execute () {for (command cmd: commandlist) cmd.exe cute ();}}}

Client class juliahong

Package COM. bankht. command. recorder;/*** @ Author: -AK47 * @ Creation Time: 02:48:06, January 2, ** @ Class description: client class juliahong */public class juliahong {public static void main (string [] ARGs) {// create the receiver object audioplayer = new audioplayer (); // create the command object command playcommand = new playcommand (audioplayer); command rewindcommand = new rewindcommand (audioplayer); command stopcommand = new stopcommand (audioplayer ); macrocommand Marco = new route cute (); system. out. println ("-------- gorgeous split line ----------------" zookeeper marco.remove(playcommand?#marco.remove(stopcommand=#marco.exe cute ();}}

The running result is as follows:

Play... rewind... stop... -------- gorgeous split line ---------------- rewind... stop...

 

Advantages of command mode

Loose coupling

  The command mode completely decouples the client object that initiates the command from the receiver object of the specific implementation command. That is to say, the object that initiates the command has no idea who the specific implementation object is, I do not know how to implement it.

More dynamic control

  The command mode encapsulates requests and can dynamically parameterize, queue, and log the requests to make the system more flexible.

Natural Compound commands

The command objects in command mode can be easily combined into compound commands, that is, macro commands, so that system operations are simpler and more powerful.

Better scalability

Because the object that initiates 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.