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