The command mode belongs to the behavior mode of the object. Command mode is also called Action mode or Transaction mode. The command pattern encapsulates a request or operation into an object. The command mode allows the system to parameterize clients with different requests, queue requests or log requests, and provide command undo and recovery functions.

Structure of the command pattern

Command mode encapsulates commands. The command pattern separates the responsibility for issuing and executing commands and delegates them to different objects. Each command is an operation: the requesting party makes a request to perform an operation; The receiving party receives the request and performs the action. The command pattern allows the requesting party and the receiving party to be separated, so that the requesting party does not have to know the interface of the receiving party, let alone how the request is received, and whether, when, and how the operation is performed.

Commands allow the requesting party and the receiving party to evolve independently, which has the following advantages: (1) Command mode makes it easy to add new commands into the system. (2) Allow the receiving party to decide whether to deny the request. (3) It is easier to design a command queue. (4) The request can be easily undone and resumed. (5) Commands can be logged more easily if needed.

The following illustrates the structure of the command pattern as a schematic system.

The command pattern involves five roles: Client Role: Create a ConcreteCommand object and determine its receiver. Command role: Declares an abstract interface to all concrete Command classes. ConcreteCommand roles: Define a weak coupling between the receiver and the behavior; Implement the execute() method, which is responsible for invoking the recipient’s actions. The execute() method is usually called the execute method. The Invoker role: Is responsible for calling the command object to perform the request. The related methods are called action methods. Receiver role: Responsible for implementing and executing a request. Any class can be a receiver, and the methods that implement and execute requests are called action methods.

Receiver role class

Public class Receiver {/** * public void action(){system.out.println (" execute "); }}Copy the code

Abstract command role class

Public interface Command {/** * execute method */ void execute(); }Copy the code

Specific command role classes

Public class ConcreteCommand implements Command {// Hold corresponding Receiver object private Receiver = null; Public ConcreteCommand(Receiver Receiver){this. Receiver = Receiver; } @override public void execute() {// Usually calls the corresponding method of the receiver object to actually execute the function receiver.action(); }}Copy the code

Requester role class

Public class Invoker {/** * private Command Command = null; /** * public Invoker(Command Command){this.mand = Command; } public void action() {command.execute(); }}Copy the code

Client role classes

Public class Client {public static void main(String[] args) {// create Receiver Receiver = new Receiver(); Command Command = new ConcreteCommand(receiver); Invoker Invoker = new Invoker(command); Invoker.action (); }}Copy the code

AudioPlayer system

Julia, a little girl, has a cassette recorder that can Play, Rewind and Stop. The keyboard is Invoker. Julia is the client role and the recorder is the receiver role. The Command class acts as an abstract Command, while PlayCommand, StopCommand, and RewindCommand are concrete Command classes. Julia didn’t need to know how the play, rewind and stop functions worked. The details were all carried out by the Keypad. All Julia has to do is press the appropriate key on her keyboard. The recorder is a typical command mode. The recorder button separates the client from the recorder operation details.

The receiver role, played by the 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... ); }}Copy the code

Abstract command role class

Public interface Command {/** * public void execute(); }Copy the code

Specific command role classes

public class PlayCommand implements Command { private AudioPlayer myAudio; public PlayCommand(AudioPlayer audioPlayer){ myAudio = audioPlayer; Override public void execute() {myAudio. Play (); }}Copy the code
public class RewindCommand implements Command { private AudioPlayer myAudio; public RewindCommand(AudioPlayer audioPlayer){ myAudio = audioPlayer; } @Override public void execute() { myAudio.rewind(); }}Copy the code
public class StopCommand implements Command { private AudioPlayer myAudio; public StopCommand(AudioPlayer audioPlayer){ myAudio = audioPlayer; } @Override public void execute() { myAudio.stop(); }}Copy the code

The requester role, played by the 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; } /** * public void play(){playCommand. Execute (); } public void rewind(){rewindCommand. Execute (); } /** * public void stop(){stopCommand. Execute (); }}Copy the code

Client role, played by the little girl Julie

Public class Julia {public static void main(String[]args){AudioPlayer AudioPlayer = new AudioPlayer(); PlayCommand = new playCommand (audioPlayer); Command rewindCommand = new RewindCommand(audioPlayer); Command stopCommand = new StopCommand(audioPlayer); // create requester object Keypad Keypad = new Keypad(); keypad.setPlayCommand(playCommand); keypad.setRewindCommand(rewindCommand); keypad.setStopCommand(stopCommand); / / test keypad. The play (); keypad.rewind(); keypad.stop(); keypad.play(); keypad.stop(); }}Copy the code

Advantages of command mode

  • The looser coupled command pattern completely decoupled the object that initiates the command, the client, from the object that implements the command, the receiver, meaning that the object that initiates the command has no idea who the implementation object is or how to implement it.
  • The more dynamic control command mode encapsulates the request and allows it to be parameterized, queued, and logged dynamically, making the system more flexible.
  • Natural composite commands Command objects in the command pattern can be easily combined into composite commands, known as macro commands, making the system simpler and more powerful.
  • Better extensibility Due to initiate command objects and concrete implementation completely decoupled, thus extending the new command is very easy, only need to implement a new command object, and then at the time of assembly, the concrete implementation object set to the command object, then you can use this command object, the realization of have no change.

reference

www.cnblogs.com/betterboyz/…