
什么是命令模式?
命令模式允许您将命令发送者(客户端)和命令执行者(接收者)解耦,使得发送者无需知道接收者的具体类别。
-
命令(Command):声明了执行操作的接口,通常包括一个 execute方法。 -
具体命令(Concrete Command):实现了命令接口,包含了实际的操作逻辑。每个具体命令对象都与一个接收者相关联。 -
接收者(Receiver):执行命令实际操作的对象。 -
调用者(Invoker):负责调用命令对象来执行请求。 -
客户端(Client):创建命令对象并设置其接收者,然后将命令对象传递给调用者。
为什么需要命令模式?
-
解耦:命令模式可以将发送者和接收者解耦,发送者无需知道接收者的具体实现,从而提高了系统的灵活性。 -
可扩展性:您可以轻松地添加新的命令类,而无需修改已有的代码。 -
撤销操作:命令对象通常会保存操作的状态,从而支持撤销操作。 -
日志记录和事务管理:您可以使用命令模式来记录所有执行的命令,以便进行事务管理或撤销。
命令模式的实现
Command,它包括了一个 execute 方法:void execute();
}
TurnOnCommand、ChangeChannelCommand 和 AdjustVolumeCommand,它们实现了 Command 接口,并分别执行相应的操作。private Television television;
public TurnOnCommand(Television television) {
this.television = television;
}
public void execute() {
television.turnOn();
}
}
// 类似地实现 ChangeChannelCommand 和 AdjustVolumeCommand
Television,它包含了实际的操作逻辑:public void turnOn() {
System.out.println("电视已打开");
}
public void changeChannel() {
System.out.println("切换频道");
}
public void adjustVolume() {
System.out.println("调整音量");
}
}
RemoteControl,它接收并执行命令:private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void pressButton() {
command.execute();
}
}
public static void mAIn(String[] args) {
Television television = new Television();
Command turnOnCommand = new TurnOnCommand(television);
Command changeChannelCommand = new ChangeChannelCommand(television);
Command adjustVolumeCommand = new AdjustVolumeCommand(television);
RemoteControl remoteControl = new RemoteControl();
remoteControl.setCommand(turnOnCommand);
remoteControl.pressButton();
remoteControl.setCommand(changeChannelCommand);
remoteControl.pressButton();
remoteControl.setCommand(adjustVolumeCommand);
remoteControl.pressButton();
}
}
宏命令
-
打开文件 -
编辑文件 -
保存文件
macroCommand,它包含了 add 和 execute 方法:void add(Command command);
void execute();
}
TextEditorMacro,它可以添加和执行多个子命令:private List<Command> commands = new ArrayList<>();
public void add(Command command) {
commands.add(command);
}
public void execute() {
for (Command command : commands) {
command.execute();
}
}
}
OpenFileCommand、EditFileCommand 和 SaveFileCommand,它们分别执行打开、编辑和保存文件的操作。public static void main(String[] args) {
OpenFileCommand openFile = new OpenFileCommand();
EditFileCommand editFile = new EditFileCommand();
SaveFileCommand saveFile = new SaveFileCommand();
TextEditorMacro macro = new TextEditorMacro();
macro.add(openFile);
macro.add(editFile);
macro.add(saveFile);
// 执行宏命令,依次执行子命令
macro.execute();
}
}
撤销和重做
undo 方法来恢复到之前的状态。Command,包括了 execute 和 undo 方法:void execute();
void undo();
}
AddTextCommand 和 DeleteTextCommand,它们分别执行添加文本和删除文本的操作,并实现了 undo 方法来撤销操作。private TextEditor textEditor;
private String addedText;
public AddTextCommand(TextEditor textEditor, String addedText) {
this.textEditor = textEditor;
this.addedText = addedText;
}
public void execute() {
textEditor.addText(addedText);
}
public void undo() {
textEditor.deleteText(addedText);
}
}
// 类似地实现 DeleteTextCommand
TextEditor,它包含了实际的文本编辑逻辑,包括添加文本、删除文本和显示文本。private StringBuilder text = new StringBuilder();
public void addText(String addedText) {
text.Append(addedText);
}
public void deleteText(String deletedText) {
int start = text.lastIndexOf(deletedText);
if (start != -1) {
text.delete(start, start + deletedText.length());
}
}
public void displayText() {
System.out.println(text.toString());
}
}
public static void main(String[] args) {
TextEditor textEditor = new TextEditor();
Command addCommand1 = new AddTextCommand(textEditor, "Hello, ");
Command addCommand2 = new AddTextCommand(textEditor, "Design Patterns!");
Command deleteCommand = new DeleteTextCommand(textEditor, "Patterns!");
// 执行添加和删除操作
addCommand1.execute();
addCommand2.execute();
deleteCommand.execute();
// 显示当前文本
textEditor.displayText(); // 输出: Hello, Design!
// 撤销删除操作
deleteCommand.undo();
// 显示当前文本
textEditor.displayText(); // 输出: Hello, Design Patterns!
}
}






