Java implements Command pattern
Command pattern is a behavioral design pattern, which encapsulates a request into an object, thus decoupling the object sending the request from the object processing the request. By encapsulating the request as an object, the Command pattern allows request parameterization, and supports queuing, logging, revocation, and redo of requests.
Applicable scenarios:
1. When the specific request needs to be decoupled from the object processing the request, the Command pattern can be used.
2. When you need to support the undo and redo functions of operations, you can use the Command pattern.
3. The Command pattern can be used when the request needs to be queued and logged.
4. When you need to combine a group of operations into a command, you can use the Command pattern.
Benefits of Command pattern:
1. Reduce system coupling: the sender and receiver are Loose coupling, and the sender does not need to know the specific implementation of the receiver.
2. Easy to expand new commands: Adding a new command does not affect other existing commands, making it easy to add and maintain.
3. Support for undo and redo operations: By saving the history of commands, undo and redo functions can be achieved.
The following is a simple example code of Java Command pattern:
Firstly, define a command interface Command:
public interface Command {
void execute();
}
Then implement the command interface and create specific command classes, such as opening and closing TV commands:
public class OpenTVCommand implements Command {
private Television tv;
public OpenTVCommand(Television tv) {
this.tv = tv;
}
@Override
public void execute() {
tv.open();
}
}
public class CloseTVCommand implements Command {
private Television tv;
public CloseTVCommand(Television tv) {
this.tv = tv;
}
@Override
public void execute() {
tv.close();
}
}
Create the executor of the command again, such as for television:
public class Television {
public void open() {
System. out. println ("Turn on TV");
}
public void close() {
System. out. println ("Turn off TV");
}
}
Finally, create a command caller who can receive commands and execute:
public class CommandInvoker {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void executeCommand() {
command.execute();
}
}
Now you can test the above code, create a command caller, set the command, and then execute the command:
public class Main {
public static void main(String[] args) {
Television tv = new Television();
Command openCommand = new OpenTVCommand(tv);
Command closeCommand = new CloseTVCommand(tv);
CommandInvoker invoker = new CommandInvoker();
invoker.setCommand(openCommand);
invoker.executeCommand();
invoker.setCommand(closeCommand);
invoker.executeCommand();
}
}
The above code will print the following results:
Turn on TV
Turn off TV
This indicates that the Command pattern successfully decouples the requester from the processor and executes the command through the command caller.