命令模式的本质是将请求封装成对象,将发出命令与执行命令的责任分开,命令的发送者和接收者完全解耦,发送者只需知道如何发送命令,不需要关心命令是如何实现的,甚至是否执行成功都不需要理会。命令模式的关键在于引入了抽象命令接口,发送者针对抽象命令接口编程,只有实现了抽象命令接口的具体命令才能与接收者相关联。另外命令可以像强对象一样可以被存储和传递,所以可支持撤销的操作
使用命令模式的优势在于降低了系统的耦合度,而且新命令可以很方便添加到系统中,也容易设计一个组合命令。但缺点在于会导致某些系统有过多的具体命令类,因为针对每一个命令都需要设计一个具体命令类。所以命令模式适用于以下场景:
(1)需要将请求调用者和请求接收者解耦,使得调用者和接收者不直接交互。
(2)系统需要在不同的时间指定请求、将请求排队和执行请求。
(3)系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作。
(4)系统需要将一组操作组合在一起,即支持宏命令。
命令对象
/**
* @author lq
* @PACKAGE_NAME: com.lq.builder
* @CLASS_NAME: Content
* @date 2022/11/13 11:46
* @Description: 命令对象
*/
public class Content {
String msg = "hello everybody ";
}
抽象命令类
/**
* @author lq
* @PACKAGE_NAME: com.lq.builder
* @CLASS_NAME: Command
* @date 2022/11/13 11:46
* @Description: 抽象命令类
*/
public abstract class Command {
/**
* 执行
*/
public abstract void doImplement();
/**
* 回退
*/
public abstract void undo();
}
/**
* @author lq
* @PACKAGE_NAME: com.lq.builder
* @CLASS_NAME: CopyCommand
* @date 2022/11/13 11:46
* @Description: 具体命令类 拷贝
*/
public class CopyCommand extends Command {
Content c;
public CopyCommand(Content c) {
this.c = c;
}
@Override
public void doImplement() {
c.msg = c.msg + c.msg;
}
@Override
public void undo() {
c.msg = c.msg.substring(0, c.msg.length()/2);
}
}
/**
* @author lq
* @PACKAGE_NAME: com.lq.builder
* @CLASS_NAME: DeleteCommand
* @date 2022/11/13 11:46
* @Description: 具体命令类 删除
*/
class DeleteCommand extends Command {
Content c;
String deleted;
public DeleteCommand(Content c) {
this.c = c;
}
@Override
public void doImplement() {
deleted = c.msg.substring(0, 5);
c.msg = c.msg.substring(5, c.msg.length());
}
@Override
public void undo() {
c.msg = deleted + c.msg;
}
}
/**
* @author lq
* @PACKAGE_NAME: com.lq.builder
* @CLASS_NAME: InsertCommand
* @date 2022/11/13 11:46
* @Description: 具体命令类 新增
*/
class InsertCommand extends Command {
Content c;
String strToInsert = "http://www.baidu.com";
public InsertCommand(Content c) {
this.c = c;
}
@Override
public void doImplement() {
c.msg = c.msg + strToInsert;
}
@Override
public void undo() {
c.msg = c.msg.substring(0, c.msg.length()-strToInsert.length());
}
}
测试
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Content c = new Content();
Command insertCommand = new InsertCommand(c);
insertCommand.doImplement();
insertCommand.undo();
Command copyCommand = new CopyCommand(c);
copyCommand.doImplement();
copyCommand.undo();
Command deleteCommand = new DeleteCommand(c);
deleteCommand.doImplement();
deleteCommand.undo();
List<Command> commands = new ArrayList<>();
commands.add(new InsertCommand(c));
commands.add(new CopyCommand(c));
commands.add(new DeleteCommand(c));
for(Command comm : commands) {
comm.doImplement();
}
System.out.println(c.msg);
for(int i= commands.size()-1; i>=0; i--) {
commands.get(i).undo();
}
System.out.println(c.msg);
}
}
结果
everybody http://www.baidu.comhello everybody http://www.baidu.com
hello everybody