Java command design pattern details
- 2020-05-05 11:15:13
- OfStack
Passing a request from the client into an object allows you to parameterize the client with different requests. Used for decoupling "behavior requester" and "behavior implementor", the loose coupling between them can be realized to adapt to changes. Separate the variable from the constant.
1. Role
Command
Defines the interface to the command and declares the method to execute.
ConcreteCommand
Command interface implementation object, is the "virtual" implementation; It is common to hold the receiver and invoke the receiver's function to complete the operation the command is intended to perform.
Receiver
Receiver, the object that actually executes the command. Any class can be a receiver, as long as it implements the corresponding functionality required by the command.
Invoker
To require a command object to execute a request, it usually holds a command object, and can hold many command objects. This is where the client actually fires the command and asks the command to do something about it, which is the equivalent of an entry point to the command object.
Client
Creates a specific command object and sets the receiver of the command object. Note that this is not a client in our normal sense, but rather is assembling the command object and receiver. Perhaps it would be better to call this Client an assembler, since the actual client using the command triggers execution from Invoker.
2. Advantages
2. New commands can be easily added to the system.
3. It is relatively easy to design a composite command.
4. Call the same method to implement different functions
3. Disadvantages
Using command mode can result in some systems having too many specific command classes. Because a specific command class needs to be designed for each command, some systems may require a large number of specific command classes, which can affect the use of command patterns.
4.
5.
Simulation of the operation of the television has boot, shutdown, change the command. The code is
// The interface to execute the command
public interface Command {
void execute();
}
// Order 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);
}
}
// Boot command ConcreteCommand
public class CommandOn implements Command {
private Tv myTv;
public CommandOn(Tv tv) {
myTv = tv;
}
public void execute() {
myTv.turnOn();
}
}
// The shutdown command ConcreteCommand
public class CommandOff implements Command {
private Tv myTv;
public CommandOff(Tv tv) {
myTv = tv;
}
public void execute() {
myTv.turnOff();
}
}
// Channel switch 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);
}
}
// Think of it 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();
}
}
// The test class Client
public class Client {
public static void main(String[] args) {
// Order receiver Receiver
Tv myTv = new Tv();
// Boot command ConcreteCommond
CommandOn on = new CommandOn(myTv);
// The shutdown command ConcreteCommond
CommandOff off = new CommandOff(myTv);
// Channel switch command ConcreteCommond
CommandChange channel = new CommandChange(myTv, 2);
// Command control object Invoker
Control control = new Control(on, off, channel);
// boot
control.turnOn();
// Switching the channel
control.changeChannel();
// To turn it off
control.turnOff();
}
}
Execute the result
The televisino is on.
Now TV channel is 2
The television is off.
vi. Summary
1. The essence of command mode is to encapsulate the command and separate the responsibility of issuing the command from the responsibility of executing the command.
2. Each command is an operation: the requesting party issues a request to perform an operation; The receiving party receives the request and performs the operation.
3. The command mode allows the requesting party to be independent of the receiving party, so that the requesting party does not have to know the interface of the receiving party, let alone how the request is received, whether, when and how the operation is executed.
4. The command pattern makes the request itself an object that can be stored and passed like any other object.
5. The key of command mode is that abstract command interface is introduced, and the sender is programmed for the abstract command interface. Only the specific command that realizes the abstract command interface can be associated with the receiver.
The above is the detailed introduction of Java command design pattern, I hope to help you.