The command pattern is to encapsulate a group of operation calls in an object, which is called a command object.
For example, we can have a command dedicated to turning on and off electrical appliances, and by calling this command, electrical appliances can be turned on and off.
system start 冰箱
system close 冰箱
system start 空调
system close 空调
Then we can write a command class about electrical appliances, which can be combined with specific electrical appliances to control specific electrical appliances when creating objects.
That is, we can encapsulate system start
and system close
into an object for you, and then the object can be combined with home appliances such as air conditioners and refrigerators to achieve the function of system close 空调
.
There are the following roles in command mode
- Abstract command: The parent class of all commands, defines the abstract interface for executing the command, and declares that the callee member is bound to the command.
- Specific commands: Implement specific execution details.
- Abstract callee: Defines the functional methods that the callee can call.
- Concrete caller: The specific details of the called method that implements the concrete product.
- Caller: Set the specific call command and implement the call method.
The following is a software example to implement the command mode
abstract command
public abstract class Command {
Software software;
public Command(Software software) {
this.software = software;
}
abstract void execute();
}
Specific Commands - On, Operate, Off
public class OpenSoftwareCommand extends Command{
public OpenSoftwareCommand(Software software) {
super(software);
}
@Override
void execute() {
software.open();
}
}
public class OperateSoftwareCommand extends Command{
public OperateSoftwareCommand(Software software) {
super(software);
}
@Override
void execute() {
software.operate();
}
}
public class CloseSoftwareCommand extends Command{
public CloseSoftwareCommand(Software software) {
super(software);
}
@Override
void execute() {
software.close();
}
}
Abstract Callee - Software
public interface Software {
/**
* 打开软件
*/
void open();
/**
* 操作软件
*/
void operate();
/**
* 关闭软件
*/
void close();
}
Specific callee - WeChat
public class Wechat implements Software{
@Override
public void open() {
System.out.println("打开微信");
}
@Override
public void operate() {
System.out.println("微信聊天");
}
@Override
public void close() {
System.out.println("推出微信");
}
}
caller
public class Invoker {
Command command;
public Invoker() {
}
public void setCommand(Command command) {
this.command = command;
}
public void execute(){
command.execute();
}
}
test
public class CommandTest {
@Test
public void test(){
Invoker invoker = new Invoker();
Command command = new OpenSoftwareCommand(new Wechat());
invoker.setCommand(command);
invoker.execute();
command = new OperateSoftwareCommand(new Wechat());
invoker.setCommand(command);
invoker.execute();
}
}
======结果======
打开微信
微信聊天
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。