
什么是命令模式?
命令模式允許您將命令發(fā)送者(客戶端)和命令執(zhí)行者(接收者)解耦,使得發(fā)送者無需知道接收者的具體類別。
-
命令(Command):聲明了執(zhí)行操作的接口,通常包括一個(gè) execute方法。 -
具體命令(Concrete Command):實(shí)現(xiàn)了命令接口,包含了實(shí)際的操作邏輯。每個(gè)具體命令對(duì)象都與一個(gè)接收者相關(guān)聯(lián)。 -
接收者(Receiver):執(zhí)行命令實(shí)際操作的對(duì)象。 -
調(diào)用者(Invoker):負(fù)責(zé)調(diào)用命令對(duì)象來執(zhí)行請(qǐng)求。 -
客戶端(Client):創(chuàng)建命令對(duì)象并設(shè)置其接收者,然后將命令對(duì)象傳遞給調(diào)用者。
為什么需要命令模式?
-
解耦:命令模式可以將發(fā)送者和接收者解耦,發(fā)送者無需知道接收者的具體實(shí)現(xiàn),從而提高了系統(tǒng)的靈活性。 -
可擴(kuò)展性:您可以輕松地添加新的命令類,而無需修改已有的代碼。 -
撤銷操作:命令對(duì)象通常會(huì)保存操作的狀態(tài),從而支持撤銷操作。 -
日志記錄和事務(wù)管理:您可以使用命令模式來記錄所有執(zhí)行的命令,以便進(jìn)行事務(wù)管理或撤銷。
命令模式的實(shí)現(xiàn)
Command,它包括了一個(gè) execute 方法:void execute();
}
TurnOnCommand、ChangeChannelCommand 和 AdjustVolumeCommand,它們實(shí)現(xiàn)了 Command 接口,并分別執(zhí)行相應(yīng)的操作。private Television television;
public TurnOnCommand(Television television) {
this.television = television;
}
public void execute() {
television.turnOn();
}
}
// 類似地實(shí)現(xiàn) ChangeChannelCommand 和 AdjustVolumeCommand
Television,它包含了實(shí)際的操作邏輯:public void turnOn() {
System.out.println("電視已打開");
}
public void changeChannel() {
System.out.println("切換頻道");
}
public void adjustVolume() {
System.out.println("調(diào)整音量");
}
}
RemoteControl,它接收并執(zhí)行命令: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,它可以添加和執(zhí)行多個(gè)子命令: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,它們分別執(zhí)行打開、編輯和保存文件的操作。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);
// 執(zhí)行宏命令,依次執(zhí)行子命令
macro.execute();
}
}
撤銷和重做
undo 方法來恢復(fù)到之前的狀態(tài)。Command,包括了 execute 和 undo 方法:void execute();
void undo();
}
AddTextCommand 和 DeleteTextCommand,它們分別執(zhí)行添加文本和刪除文本的操作,并實(shí)現(xiàn)了 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);
}
}
// 類似地實(shí)現(xiàn) DeleteTextCommand
TextEditor,它包含了實(shí)際的文本編輯邏輯,包括添加文本、刪除文本和顯示文本。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!");
// 執(zhí)行添加和刪除操作
addCommand1.execute();
addCommand2.execute();
deleteCommand.execute();
// 顯示當(dāng)前文本
textEditor.displayText(); // 輸出: Hello, Design!
// 撤銷刪除操作
deleteCommand.undo();
// 顯示當(dāng)前文本
textEditor.displayText(); // 輸出: Hello, Design Patterns!
}
}






