Pass requests from the client into an object, allowing you to parameterize the client with different requests. Used to decouple "behavior requester" and "behavior implementer", loose coupling between the two can be achieved in order to adapt to changes. Separate the changing and unchanging factors.
1. Role
Command
Defines the interface of the command and declares the execution method.
ConcreteCommand
The command interface implementation object is an "virtual" implementation; it usually holds the receiver and calls the receiver's function to complete the operation to be performed by the command.
Receiver
Receiver, the object that truly executes the command. Any class may become a receiver as long as it can implement the corresponding functions required by the command.
Invoker
The command object is required to execute the request, and usually holds the command object and can hold many command objects. This is where the client actually triggers the command and requires the command to perform the corresponding operation, which means it is equivalent to using the entry of the command object.
Client
Create a specific command object and set the receiver of the command object. Note that this is not a client in our conventional sense, but is assembling the command object and receiver. Perhaps, it would be better to call this client an assembler, because the client that really uses the command triggers execution from Invoker.
2. Advantages
3. Disadvantages <br />Using command mode may cause some systems to have too many specific command classes. Because a specific command class is required for each command, some systems may require a large number of specific command classes, which will affect the use of command mode.
IV. Applicable situation
5. Application <br />The simulation includes power-on, shutdown, and change-in commands for the operation of the TV. The code is as follows
//Interface for executing command public interface Command { void execute(); } //Command receiver Receiver public class Tv { public int currentChannel = 0; public void turnOn() { System.out.println("The televisino is on."); } public void turnOff() { System.out.println("The television is off."); } public void changeChannel(int channel) { this.currentChannel = channel; System.out.println("Now TV channel is " + channel); } } //Shut down command ConcreteCommand public class CommandOn implements Command { private Tv myTv; public CommandOn(Tv tv) { myTv = tv; } public void execute() { myTv.turnOn(); } } //Shut down command ConcreteCommand public class CommandOff implements Command { private Tv myTv; public CommandOff(Tv tv) { myTv = tv; } public void execute() { myTv.turnOff(); } } //Channel switching command ConcreteCommand public class CommandChange implements Command { private Tv myTv; private int channel; public CommandChange(Tv tv, int channel) { myTv = tv; this.channel = channel; } public void execute() { myTv.changeChannel(channel); } } //It can be regarded as a remote control Invoker public class Control { private Command onCommand, offCommand, changeChannel; public Control(Command on, Command off, Command channel) { onCommand = on; offCommand = off; changeChannel = channel; } public void turnOn() { onCommand.execute(); } public void turnOff() { offCommand.execute(); } public void changeChannel() { changeChannel.execute(); } } //Test class Client public class Client { public static void main(String[] args) { // Command recipient Receiver Tv myTv = new Tv(); // Power-on command ConcreteCommond CommandOn on = new CommandOn(myTv); // Power-off command ConcreteCommond CommandOff off = new CommandOff(myTv); // Channel switching command ConcreteCommond CommandChange channel = new CommandChange(myTv, 2); // Command control object Invoker Control control = new Control(on, off, channel); // Power-on control.turnOn(); // Switch channel control.changeChannel(); // shut down control.turnOff(); } } Execution results
The televisino is on.
Now TV channel is 2
The television is off.
6. Summary
1. The essence of the command mode is to encapsulate the command and separate the responsibility for issuing the command and the responsibility for executing the command.
2. Each command is an operation: the requesting party issues a request, requiring an operation to be performed; the receiving party receives the request and performs the operation.
3. The command mode allows the requesting party to be independent from the receiving party, so that the requesting party does not have to know the interface of the party receiving the request, nor does it have to know how the request was received, whether, when, and how the operation was executed.
4. The command mode makes the request itself an object, which can be stored and passed like other objects.
5. The key to the command mode is to introduce an abstract command interface, and the sender programes for the abstract command interface. Only specific commands that implement the abstract command interface can be associated with the receiver.
The above is a detailed introduction to the Java command design pattern, I hope it will be helpful to everyone's learning.