Command mode for Java design mode

Source: Internet
Author: User

Command mode:

In the encapsulation of the command, the responsibility for issuing the command and the responsibility for executing the command are divided and delegated to different objects.

The command pattern involves five characters:

    • Client (Commandmain) role: Create a specific command and identify the recipient (triggering the recorder key)
    • Command role: Declares an abstract interface to all specific command classes (defines a command interface)
    • Specific commands (Playcommand, Rewindcommand, Stopcommand) roles: Implements the command interface, invokes the corresponding operation of the Sound Recorder;
    • Key (Keypad) role: Responsible for invoking the appropriate command object
    • Recorder (Audioplayer) role: Responsible for implementing and executing a request.

Sound Recorder class:

/***/Publicclass  audioplayer    {publicvoid  Play () {        System.out.println ("Play ...");    }      Public void Rewind () {        System.out.println ("Rewind ..."    );  Public void Stop () {        System.out.println ("Stop playing ...");}    }

Command interface:

/***/Publicinterface  Command    {publicvoid  execute ();}

Play Command:

/***/Publicclassimplements  Command    {private  audioplayer audioplayer;      Public Playcommand (Audioplayer audioplayer) {        this. Audioplayer= audioplayer;    }    @Override    publicvoid  execute () {        audioplayer.play ();    }}

Rewind command:

/***/Publicclassimplements  Command    {private  audioplayer audioplayer;      Public Rewindcommand (Audioplayer audioplayer) {        this. Audioplayer= audioplayer;    }    @Override    publicvoid  execute () {        audioplayer.rewind ();    }}

Stop command:

/***/Publicclassimplements  Command    {private  audioplayer audioplayer;      Public Stopcommand (Audioplayer audioplayer) {        this. Audioplayer= audioplayer;    }    @Override    publicvoid  execute () {        audioplayer.stop ();    }}

Key class:

** *Key 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; }    /*** Perform playback*/     Public voidPay () {playcommand.execute (); }    /*** Carry Rewind*/     Public voidRewind () {rewindcommand.execute (); }    /*** Execution Stop*/     Public voidStop () {stopcommand.execute (); }}

Recorder Operation class:

/*** Operation Button-"Send command to the recorder to perform specific actions*/ Public classCommandmain { Public Static voidMain (string[] args) {//new, a tape recorder .Audioplayer Audioplayer =NewAudioplayer (); //To create a play Command objectCommand Playcommand =NewPlaycommand (Audioplayer); //To Create a rewind command objectCommand Rewindcommand =NewRewindcommand (Audioplayer); //To create a Stop command objectCommand Stopcommand =NewStopcommand (Audioplayer); //Create a Key objectKeypad Keypad =NewKeypad (); //bind a play command to a keyKeypad.setplaycommand (Playcommand); //bind the Rewind command to the keyKeypad.setrewindcommand (Rewindcommand); //bind a Stop command to a keyKeypad.setstopcommand (Stopcommand); //PlayKeypad.pay (); //Rewind BeltKeypad.rewind (); //StopKeypad.stop ();        Keypad.pay ();    Keypad.stop (); }}

Operation Result:

Command mode for Java 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.