Home >Java >javaTutorial >Java Design Patterns Command Pattern
Definition
Pass the request from the client into an object, allowing you to parameterize the client with different requests. Used to decouple "behavior requester" and "behavior implementer" to achieve loose coupling between the two in order to adapt to changes. Separate changing and constant factors.
Role
Command
defines the interface of the command and declares the execution method.
ConcreteCommand
The command interface implementation object is a "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 actually executes the command. Any class may become a receiver as long as it can implement the corresponding functions required by the command.
Invoker
requires the command object to execute the request. It 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 is equivalent to the entrance to 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 actually uses the command triggers execution from the Invoker.
Advantages
1. Reduce the coupling between objects.
2. New commands can be easily added to the system.
3. It is easier to design a combination command.
4. Calling the same method to implement different functions
Disadvantages
Using command mode may cause some systems to have 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 will affect the use of command patterns.
Applicable situations
1. The system needs to decouple the request caller and the request receiver so that the caller and receiver do not interact directly.
2. The system needs to specify requests, queue requests and execute requests at different times.
3. The system needs to support the undo (Undo) operation and recovery (Redo) operation of the command.
4. The system needs to combine a group of operations, that is, support macro commands.
The application
simulates the operation of the TV with commands to turn on, turn off, and change channels. The code is as follows
//执行命令的接口 public interface Command { void execute(); } //命令接收者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); } } //开机命令ConcreteCommand public class CommandOn implements Command { private Tv myTv; public CommandOn(Tv tv) { myTv = tv; } public void execute() { myTv.turnOn(); } } //关机命令ConcreteCommand public class CommandOff implements Command { private Tv myTv; public CommandOff(Tv tv) { myTv = tv; } public void execute() { myTv.turnOff(); } } //频道切换命令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); } } //可以看作是遥控器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(); } } //测试类Client public class Client { public static void main(String[] args) { // 命令接收者Receiver Tv myTv = new Tv(); // 开机命令ConcreteCommond CommandOn on = new CommandOn(myTv); // 关机命令ConcreteCommond CommandOff off = new CommandOff(myTv); // 频道切换命令ConcreteCommond CommandChange channel = new CommandChange(myTv, 2); // 命令控制对象Invoker Control control = new Control(on, off, channel); // 开机 control.turnOn(); // 切换频道 control.changeChannel(); // 关机 control.turnOff(); } }
Execution results
The televisino is on.
Now TV channel is 2
The television is off.
Summary
1. The essence of command mode is to encapsulate the command and issue the command separation of responsibilities and responsibility for executing orders.
2. Each command is an operation: the requesting party sends a request to perform an operation; the receiving party receives the request and performs the operation.
3. The command mode allows the requesting party and the receiving party to be independent, so that the requesting party does not need to know the interface of the party receiving the request, let alone how the request is received, and whether and when the operation is executed. , and how it is 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 the introduction of an abstract command interface, and the sender programs for the abstract command interface. Only specific commands that implement the abstract command interface can be associated with the receiver.