The command pattern belongs to the behavior pattern of the object. Command mode is also known as action mode or trading (Transaction) mode.
The command pattern encapsulates a request or operation 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.
Structure of the command pattern
The command pattern is the encapsulation of the command. The command mode divides the responsibility for issuing the command and the responsibility for executing the command, delegating it to different objects.
Each command is an action: the requesting party makes a request to perform an action, the receiving party receives the request, and executes the action. The command pattern allows the requesting party and the receiving party to be independent, so that the requesting party does not have to know the interface of the party receiving the request, not to know how the request was received, and whether the operation was executed, when it was executed, and how it was executed.
The command allows the requesting party and the party receiving the request to evolve independently, with the following advantages:
(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.
The following is a schematic system that describes the structure of the command pattern.
The command pattern involves five characters, each of which is:
Client Role: Create a specific command (Concretecommand) object and determine its recipient.
command role: declares an abstract interface to all specific command classes.
specific command (concretecommand) role: defines a weak coupling between a receiver and a behavior, and implements the Execute () method, which is responsible for invoking the corresponding operation of the receiver. The Execute () method is often called the execution method.
Requestor (Invoker) role: responsible for invoking the Command object execution request, the related method is called the action method.
Receiver Role: responsible for specific implementation and execution of a request. Any class can be a recipient, and the method of implementing and executing the request is called the action method.
Source
Recipient Role Class
public class Receiver { /** * Actually executes the command corresponding to the action */public Void action () { System.out.println ("perform operation"); }}
Abstract command Role Class
Public interface Command { /** * Execute method * /Void execute ();}
Specific command role classes
public class Concretecommand implements Command { //holds the corresponding recipient object private receiver receiver = null; /** * Construction Method * /public Concretecommand (receiver receiver) { this.receiver = receiver; } @Override public Void execute () { //usually transpose the corresponding method of the recipient object, allowing the receiver to actually perform the function receiver.action ();} }
Requestor role Class
public class Invoker { /** * Holds command object */private command command = null; /** * Construction Method * /public Invoker (Command command) { this.command = command; } /** * Action Method * /public void action () { command.execute ();} }
Client Role Classes
public class Client {public static void Main (string[] args) { //Create receiver receiver receiver = new receiver (); c21/>//creates a command object and sets its recipient command command = new Concretecommand (receiver); Create the requestor and set the Command object in Invoker Invoker = new Invoker (command); Execution Method invoker.action (); }}
Audioplayer system
Julia has a cassette recorders, the recorder has broadcast (Play), Rewind (Rewind) and stop (stop) function, the keyboard of the recorder is the requestor (Invoker) role, Julie (Julia) is the client role, and the recorder is the recipient role. The command class plays the role of abstract commands, while Playcommand, Stopcommand, and Rewindcommand are the specific commands classes. Julia does not need to know how the broadcast (play), Rewind (rewind), and stop (stop) functions are performed, and the details of these commands are all implemented by the keyboard (Keypad). Julie (Julia) just needs to press the appropriate key on the keyboard.
A tape recorder is a typical command mode. The Recorder button separates the client from the operation details of the recorder.
Source
Receiver role, played by Sound Recorder class
public class Audioplayer {public void play () { System.out.println ("Play ..."); public void Rewind () { System.out.println ("Rewind ..."); public void Stop () { System.out.println ("Stop ...");} }
Abstract command Role Class
Public interface Command { /** * Execute method * /public void execute ();}
Specific command role classes
public class Playcommand implements Command { private audioplayer myaudio; Public Playcommand (Audioplayer audioplayer) { myaudio = Audioplayer; } /** * Execution Method * /@Override public Void execute () { myaudio.play ()} }
public class Rewindcommand implements Command { private audioplayer myaudio; Public Rewindcommand (Audioplayer audioplayer) { myaudio = Audioplayer; } @Override public Void execute () { myaudio.rewind (); }}
public class Stopcommand implements Command { private audioplayer myaudio; Public Stopcommand (Audioplayer audioplayer) { myaudio = Audioplayer; } @Override public Void execute () { myaudio.stop (); }}
Requestor role, played by keyboard class
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; } /** * Execute Playback method * /public void Play () { playcommand.execute (); } /** * Execute Rewind Method */public Void Rewind () { rewindcommand.execute (); } /** * Execute Playback method * /public void Stop () { stopcommand.execute ()} }
Client role, played by Julie Little girl
public class Julia {public static void Main (String[]args) { //Create receiver object audioplayer Audioplayer = new Audioplayer (); Create Command object Playcommand = new Playcommand (audioplayer); Command Rewindcommand = new Rewindcommand (audioplayer); Command Stopcommand = new Stopcommand (audioplayer); Create requestor 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 results of the operation are as follows:
Macro command
The so-called Macro command is simply a command that contains multiple commands and is a combination of commands.
Imagine that Julie's tape recorder has a recording function that can record a single command and then re-execute the recorded commands at any time, which is called the Macro command set function. As a result, Julie's recorder system now has four keys, namely, broadcast, rewind, stop and Macro command functions. At this point the design of the system and the previous design has been enhanced, mainly reflected in the Julia class now has a new method to operate the Macro command key.
Source
The system requires an interface that represents a macro command to define the interfaces required for a specific macro command.
Public interface Macrocommand extends command { /** * Macro Commands Clustered Management method * You 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);
The specific macro command Macroaudiocommand class is responsible for combining individual commands with macro commands.
public class Macroaudiocommand implements Macrocommand { private list<command> commandlist = new arraylist< Command> (); /** * Macro Command Aggregation Management method */ @Override public void Add (command cmd) { commandlist.add (cmd); } /** * Macro Command Aggregation Management method * /@Override public void Remove (Command cmd) { commandlist.remove (cmd); } /** * Execution method */ @Override public Void execute () {for Command cmd:commandlist) { Cmd.execute ();}}}
Client class Julia
public class Julia {public static void Main (String[]args) { //Create receiver object audioplayer Audioplayer = new Audioplayer (); Create Command object 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 (); }}
The results of the operation are as follows:
Advantages of the Command mode
more loosely coupled
The command pattern completely decouples the object that originated the command-the client, and the object that implements the command-that is, the object that initiated the command is completely unaware of who the implementation object is and how it is implemented.
more dynamic Control
The command mode encapsulates the request, and it can be parameterized, queued and journaled dynamically, which makes the system more flexible.
It's a natural compound command.
Command objects in command mode can be easily combined into composite commands, which are macro commands, making the system easier to operate and more powerful.
Better Extensibility
Since the object that initiated the command is fully decoupled from the specific implementation, it is easy to extend the new command, just implement the new command object, then set the specific implementation object to the Command object when assembling, and then you can use the command object, and the existing implementation does not have to change at all.
The command mode of Java and mode