Java?設(shè)計(jì)模式中的命令模式詳情
模式介紹
- 命令模式(Command Pattern) :在軟件設(shè)計(jì)中,我們經(jīng)常需要向某些對(duì)象發(fā)送請(qǐng)求,但是并不知道請(qǐng)求的接收者是誰(shuí),也不知道被請(qǐng)求的操作是哪個(gè),我們只需在程序運(yùn)行時(shí)指定具體的請(qǐng)求接收者即可,此時(shí),可以使用命令模式來(lái)進(jìn)行設(shè)計(jì)。
- 命名模式使得請(qǐng)求發(fā)送者與請(qǐng)求接收者消除彼此之間的耦合,讓對(duì)象之間的調(diào)用關(guān)系更加靈活,實(shí)現(xiàn)解耦。
- 在命名模式中,會(huì)將一個(gè)請(qǐng)求封裝為一個(gè)對(duì)象,以便使用不同參數(shù)來(lái)表示不同的請(qǐng)求(即命名),同時(shí)命令模式也支持可撤銷(xiāo)的操作。
UML類圖

類圖解析:
- Invoker:是調(diào)用者角色。
- Command:是命令角色,需要執(zhí)行的所有命令都在這里,可以是接口或抽象類
- ConcreteCommand:將一個(gè)接受者對(duì)象與一個(gè)動(dòng)作綁定,調(diào)用接受者相應(yīng)的操作,實(shí)現(xiàn)execute、undo方法
- Receiver:接受者角色,知道如何實(shí)施和執(zhí)行一個(gè)請(qǐng)求相關(guān)的操作
命令模式案例
案例解析:智能家居,通過(guò)一個(gè)遙控器控制家里的智能設(shè)備


Command接口類
public interface Command {
/**
* 執(zhí)行操作
*/
void execute();
/**
* 撤銷(xiāo)操作
*/
void undo();
}LightReceiver、CurtainReceiver信號(hào)接收者
public class LightReceiver {
public void on() {
System.out.println("開(kāi)燈...");
}
public void off() {
System.out.println("關(guān)燈....");
}
}public class CurtainReceiver {
public void on() {
System.out.println("打開(kāi)窗簾...");
}
public void off() {
System.out.println("關(guān)閉窗簾...");
}
}Command具體實(shí)現(xiàn)子類CurtainOnCommand、CurtainOffCommand、LightOnCommand、LightOffCommand、NoCommand
public class CurtainOnCommand implements Command {
private CurtainReceiver curtain;
public CurtainOnCommand(CurtainReceiver curtainReceiver) {
this.curtain = curtainReceiver;
}
@Override
public void execute() {
curtain.on();
}
@Override
public void undo() {
curtain.off();
}
}public class CurtainOffCommand implements Command {
private CurtainReceiver curtainReceiver;
public CurtainOffCommand(CurtainReceiver curtainReceiver) {
this.curtainReceiver = curtainReceiver;
}
@Override
public void execute() {
curtainReceiver.off();
}
@Override
public void undo() {
curtainReceiver.on();
}
}public class LightOnCommand implements Command {
// 聚合LightReceiver
private LightReceiver light;
public LightOnCommand(LightReceiver light) {
this.light = light;
}
@Override
public void execute() {
light.on();
}
@Override
public void undo() {
light.off();
}
}public class LightOffCommand implements Command {
// 聚合LightReceiver
private LightReceiver light;
public LightOffCommand(LightReceiver light) {
this.light = light;
}
@Override
public void execute() {
light.off();
}
@Override
public void undo() {
light.on();
}
}/**
* 用于初始化每個(gè)按鈕
*/
public class NoCommand implements Command {
@Override
public void execute() {
System.out.println("do nothing...");
}
@Override
public void undo() {
System.out.println("do nothing...");
}
}RemoteController調(diào)用者
public class RemoteController {
// 開(kāi)命令數(shù)組
private Command[] onCommands;
// 關(guān)命令數(shù)組
private Command[] offCommands;
// 撤銷(xiāo)命令位置
private int no;
// 撤銷(xiāo)命令是否為按下命令
private Boolean isOn;
/**
* 初始化
*/
public RemoteController() {
this.onCommands = new Command[5];
this.offCommands = new Command[5];
for (int i = 0; i < 5; i++) {
onCommands[i] = new NoCommand();
offCommands[i] = new NoCommand();
}
}
/**
* 設(shè)置一行中的按鈕
* @param no
* @param onCommand
* @param offCommand
*/
public void setCommand(int no,Command onCommand,Command offCommand) {
onCommands[no] = onCommand;
offCommands[no] = offCommand;
}
/**
* 開(kāi)按鈕按下
* @param no
*/
public void onButtonPushed(int no){
// 調(diào)用按鈕方法
onCommands[no].execute();
// 記錄撤銷(xiāo)按鈕
this.no = no;
isOn = true;
}
/**
* 關(guān)按鈕按下
* @param no
*/
public void offButtonPushed(int no) {
// 調(diào)用按鈕方法
offCommands[no].execute();
// 記錄撤銷(xiāo)按鈕
this.no = no;
isOn = false;
}
public void undo() {
if (isOn) {
onCommands[no].undo();
} else {
offCommands[no].undo();
}
isOn = !isOn;
}
}
Client測(cè)試類
public class Client {
public static void main(String[] args) {
// 接收者
LightReceiver lightReceiver = new LightReceiver();
CurtainReceiver curtainReceiver = new CurtainReceiver();
// 命令
Command lightOnCommand = new LightOnCommand(lightReceiver);
Command lightOffCommand = new LightOffCommand(lightReceiver);
Command curtainOnCommand = new CurtainOnCommand(curtainReceiver);
Command curtainOffCommand = new CurtainOffCommand(curtainReceiver);
// 執(zhí)行者
RemoteController remoteController = new RemoteController();
remoteController.setCommand(0,lightOnCommand,lightOffCommand);
remoteController.setCommand(1,curtainOnCommand,curtainOffCommand);
System.out.print("開(kāi)燈按鈕\t");
remoteController.onButtonPushed(0);
System.out.print("撤銷(xiāo)按鈕\t");
remoteController.undo();
System.out.print("撤銷(xiāo)按鈕\t");
remoteController.undo();
System.out.print("關(guān)燈按鈕\t");
remoteController.offButtonPushed(0);
System.out.println("------------------");
System.out.print("開(kāi)窗簾按鈕\t");
remoteController.onButtonPushed(1);
System.out.print("撤銷(xiāo)按鈕 \t");
remoteController.undo();
System.out.print("撤銷(xiāo)按鈕 \t");
remoteController.undo();
System.out.print("關(guān)窗簾按鈕\t");
remoteController.offButtonPushed(1);
}
}
測(cè)試結(jié)果:

個(gè)人優(yōu)化方案(僅供參考):

命令模式的注意事項(xiàng)和細(xì)節(jié)
- 將發(fā)起請(qǐng)求的對(duì)象與執(zhí)行請(qǐng)求的對(duì)象解耦。發(fā)起請(qǐng)求的對(duì)象是調(diào)用者,調(diào)用者只要調(diào)用命令對(duì)象的execute(方法就可以讓接收者工作,而不必知道具體的接收者對(duì)象是誰(shuí)、是如何實(shí)現(xiàn)的,命令對(duì)象會(huì)負(fù)責(zé)讓接收者執(zhí)行請(qǐng)求的動(dòng)作,也就是說(shuō):”請(qǐng)求發(fā)起者”和“請(qǐng)求執(zhí)行者”之間的解耦是通過(guò)命令對(duì)象實(shí)現(xiàn)的,命令對(duì)象起到了紐帶橋梁的作用。
- 容易設(shè)計(jì)一個(gè)命令隊(duì)列。只要把命令對(duì)象放到列隊(duì),就可以多線程的執(zhí)行命令。
- 容易實(shí)現(xiàn)對(duì)請(qǐng)求的撤銷(xiāo)和重做。
- 命令模式不足:可能導(dǎo)致某些系統(tǒng)有過(guò)多的具體命令類,增加了系統(tǒng)的復(fù)雜度,這點(diǎn)在在使用的時(shí)候要注意。
- 空命令也是一種設(shè)計(jì)模式,它為我們省去了判空的操作。在上面的實(shí)例中,如果沒(méi)有用空命令,我們每按下一個(gè)按鍵都要判空,這給我們編碼帶來(lái)一定的麻煩。
- 命令模式經(jīng)典的應(yīng)用場(chǎng)景:界面的一個(gè)按鈕都是一條命令、模擬CMD (DOS命令)訂單的撤銷(xiāo)/恢復(fù)、觸發(fā)-反饋機(jī)制。
相關(guān)文章
java動(dòng)態(tài)口令登錄實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了java動(dòng)態(tài)口令登錄實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Spring boot整合Springfox生成restful的在線api文檔
這篇文章主要為大家介紹了Spring boot整合Springfox生成restful在線api文檔,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
mybatis-plus團(tuán)隊(duì)新作mybatis-mate實(shí)現(xiàn)數(shù)據(jù)權(quán)限
本文主要介紹了mybatis-plus 團(tuán)隊(duì)新作 mybatis-mate 輕松搞定數(shù)據(jù)權(quán)限,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
將Java程序的輸出結(jié)果寫(xiě)到txt文件中的方法
今天小編就為大家分享一篇將Java程序的輸出結(jié)果寫(xiě)到txt文件中的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Spring Boot ActiveMQ如何設(shè)置訪問(wèn)密碼
這篇文章主要介紹了Spring Boot ActiveMQ如何設(shè)置訪問(wèn)密碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java?多線程并發(fā)?ReentrantReadWriteLock詳情
這篇文章主要介紹了Java多線程并發(fā)ReentrantReadWriteLock詳情,ReentrantReadWriteLock可重入讀寫(xiě)鎖。實(shí)際使用場(chǎng)景中,我們需要處理的操作本質(zhì)上是讀與寫(xiě),更多相關(guān)資料,感興趣的小伙伴可以參考一下下面文章內(nèi)容2022-06-06

