Mode of command in design mode

Source: Internet
Author: User

Command mode

Definition: A command pattern encapsulates a request or action into an object. The command mode allows the system to parameterize the client using different requests, queue requests, or log request logs, which can provide undo and redo functions for the command.

Code: (Take recorder as an example)

Sound Recorder Class: Concrete method realization

 Public class Audioplayer {        publicvoid  play () {        System.out.println ("Play ...");    }          Public void Rewind () {        System.out.println ("Rewind ..."        );  Public void Stop () {        System.out.println ("Stop ...");}    }

Command interface:

 Public Interface Command {    /**     * Execute method     * /public     void execute ();}

Specific command implementation: etc.

 Public class Implements Command {    private  audioplayer myaudio;          Public Playcommand (Audioplayer audioplayer) {        = audioplayer;    }     /**      * Execution Method     * /     @Override    publicvoid   Execute () {        myaudio.play ();    }}
。。。。。

Macro Command interface:

 Public Interface 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);

Macro Command Interface implementation:

 Public classMacroaudiocommandImplementsMacrocommand {PrivateList<command> commandlist =NewArraylist<command>(); /*** Macro Command Aggregation management method*/@Override Public voidAdd (Command cmd) {commandlist.add (cmd); }    /*** Macro Command Aggregation management method*/@Override Public voidRemove (Command cmd) {commandlist.remove (cmd); }    /*** Method of execution*/@Override Public voidExecute () { for(Command cmd:commandlist) {cmd.execute (); }    }}

Keyboard class:

 Public classKeypad {PrivateCommand Playcommand; PrivateCommand Rewindcommand; PrivateCommand Stopcommand;  Public voidSetplaycommand (Command playcommand) { This. Playcommand =Playcommand; }     Public voidSetrewindcommand (Command rewindcommand) { This. Rewindcommand =Rewindcommand; }     Public voidSetstopcommand (Command stopcommand) { This. Stopcommand =Stopcommand; }    /*** Execute Playback method*/     Public voidPlay () {Playcommand.execute (); }    /*** Perform Rewind method*/     Public voidRewind () {rewindcommand.execute (); }    /*** Execute Playback method*/     Public voidStop () {stopcommand.execute (); }}

Client:

public class Julia {

public static void Main (String[]args) {
Create a Recipient Object
Audioplayer Audioplayer = new Audioplayer ();
To create a Command object
Command Playcommand = new Playcommand (Audioplayer);
Command Rewindcommand = new Rewindcommand (Audioplayer);
Command Stopcommand = new Stopcommand (Audioplayer);

Macrocommand Marco = new Macroaudiocommand ();

Marco.add (Playcommand);
Marco.add (Rewindcommand);
Marco.add (Stopcommand);
Marco.execute ();
}
}

Structure:

Role:

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

(2) Allow the party receiving the request to decide whether to veto the request.

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

(4) The revocation and recovery of the request can be easily achieved.

(5) If required, it is easier to log commands into the logs.

Http://www.cnblogs.com/java-my-life/archive/2012/06/01/2526972.html

Mode of command in design mode

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.