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