Java and mode: Command mode

Source: Internet
Author: User
ArticleDirectory
    • Source code
    • Source code

The following is the description of the command mode in the book "Java and mode" by Dr. Yan Hong:

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

Public ClassExplorer {/*** Perform commands*/Public VoidAction () {system. Out. println ("Perform operations");}}

Abstract command role

 
Public InterfaceCommand {/*** Execution Method*/VoidExecute ();}

Specific command Angle

Public   Class Concretecommand Implements  Command {  //  Hold the corresponding recipient object      Private Extends er extends ER = Null  ;  /**  * Constructor  */      Public  Concretecommand (extends er extends ER ){  This . Cycler =Extends er;} @ override  Public   Void  Execute (){  //  It usually calls the corresponding method of the recipient object to allow the recipient to truly execute the function.  Receiver. Action ();}} 

Requester role

 Public   Class  Invoker {  /**  * Holding command objects  */      Private Command command =Null  ;  /**  * Constructor  */      Public  Invoker (command ){  This . Command = Command ;}  /**  * Action Method  */      Public   Void  Action () {command.exe cute ();}} 

Client role

Public   Class  Client {  Public   Static   Void  Main (string [] ARGs ){  //  Create Receiver Extends er extends ER = New  Aggreger ();  //  Create a command object and set its Receiver Command command = New Concretecommand (aggreger );  //  Create a requester and set the command object Invoker = New  Invoker (command );  //  Execution Method  Invoker. Action ();}} 

 

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

 
Public ClassAudioplayer {Public VoidPlay () {system. Out. println ("Playing ...");}Public VoidRewind () {system. Out. println ("Rewind ...");}Public VoidStop () {system. Out. println ("Stop ...");}}

Abstract command role

 
Public InterfaceCommand {/*** Execution Method*/Public VoidExecute ();}

Specific command Angle

  Public   class  playcommand  implements   command {  private   audioplayer myaudio;   Public   playcommand (audioplayer) {myaudio  =  audioplayer ;} /**   * execution method   */  @ override   Public   void   execute () {myaudio. play () ;} 
  Public   class  rewindcommand  implements   command {  private   audioplayer myaudio;   Public   rewindcommand (audioplayer) {myaudio  =  audioplayer ;}@ override   Public   void   execute () {myaudio. rewind () ;} 
Public ClassStopcommandImplementsCommand {PrivateAudioplayer myaudio;PublicStopcommand (audioplayer) {myaudio=Audioplayer;} @ overridePublic VoidExecute () {myaudio. Stop ();}}

The requester role, played by the keyboard.

 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 ;}  /**  * Method of playing the video  */      Public   Void  Play () {playcommand.exe cute ();}  /**  * Execute the rewind method. */      Public   Void  Rewind () {rewindcommand.exe cute ();}  /**  * Method of playing the video  */      Public   Void  Stop () {stopcommand.exe cute ();}} 

Client role, played by Julie

 Public   Class  Julia {  Public   Static  Void  Main (string [] ARGs ){  //  Create recipient object Audioplayer = New  Audioplayer ();  //  CREATE command object Command playcommand = New  Playcommand (audioplayer); command rewindcommand = New  Rewindcommand (audioplayer); command stopcommand = New Stopcommand (audioplayer );  //  Create a requester object Keypad 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:

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.