行為型模式:命令模式
行為型模式:命令模式
十一大行為型模式之三:命令模式。
簡介
姓名 :命令模式
英文名 :Command Pattern
價值觀 :軍令如山
個人介紹 :
Encapsulate a request as an object,thereby letting you parameterize clients with different requests,queue or log requests,and support undoable operations.
將一個請求封裝成一個對象,從而讓你使用不同的請求把客戶端參數化,對請求排隊或者記錄請求日誌,可以提供命令的撤銷和恢復功能。
你要的故事
作為一個程序猿,我們每天都在經歷著命令模式,技術經理把需求任務分配給工程師開發,有時因為第三方或者其他不可抗拒的因素導致需求停止開發。這種工作模式就是命令模式。好了,開始故事了。小明在 XX 科技公司做一個安靜的程序猿,有一天技術經理給他分配了一個任務:新增黑名單,也就是在他們系統的某個模塊裏面可以手工對電話打黑名單標簽的功能。小明接到任務後就立馬開發,在開發了 2 天之後,因為戰略原因,技術經理大明暫停了這個開發任務,接下來我們通過非命令模式和命令模式 2 種代碼實現來體現這個過程。在這個場景中,為了簡單,我們假定技術經理大明手下只有小明一個開發人員。
非命令模式
非命令模式也就是不使用命令模式的代碼實現。代碼中,我們出現了 Developer 開發人,開發同學是接受技術經理傳達的任務,技術經理讓他開發哪個需求就開發哪個需求,如果項目有問題需要中斷,也需要技術經理評估後傳達給開發同學,所以 Developer 有 2 個方法,分別是 develop() 開發需求和 suspend() 暫停需求。 Requirement 則為需求類,TechnicalManager1 則為技術經理類,他有一個方法 action(),通過這個方法來指定開發同學開發任務或者暫停任務。
public class NoCommandTest { public static void main(String[] args) { Developer xiaoMing = new Developer("小明"); Requirement requirement = new Requirement("新增黑名單"); TechnicalManager1 technicalManager2 = new TechnicalManager1("大明"); technicalManager2.setDeveloper(xiaoMing); technicalManager2.action(requirement, "develop"); System.out.println("開發了 2 天,需求變故,需要暫停。。。"); technicalManager2.action(requirement, "suspend"); } } /** * 開發人員 */ class Developer { private String name; public Developer(String name) { this.name = name; } public void develop(Requirement requirement) { System.out.println(this.name + " 開始開發需求:" + requirement.getName()); } public void suspend(Requirement requirement) { System.out.println(this.name + " 停止開發需求:" + requirement.getName()); } public String getName() { return name; } } /** * 需求 */ class Requirement { private String name; public Requirement(String name) { this.name = name; } public String getName() { return name; } } /** * 技術經理 */ class TechnicalManager1 { private String name; private Developer developer; public TechnicalManager1(String name) { this.name = name; } public void setDeveloper(Developer developer) { this.developer = developer; } public void action(Requirement requirement, String type) { if ("develop".equals(type)) { this.developer.develop(requirement); } else if ("suspend".equals(type)) { this.developer.suspend(requirement); } } } 打印結果: 小明 開始開發需求:新增黑名單 開發了 2 天,需求變故,需要暫停。。。 小明 停止開發需求:新增黑名單
通過代碼,我們可以發現技術經理和開發同學是強依賴關系。如果技術經理下達了一個任務,要求小明寫一下周報,這時候得怎麽寫?是不是小明需要一個寫周報的方法,大明也需要新增一個處理事務類型?有沒有更好的方法讓技術經理不需要做任何改變?命令模式就來解決這個問題。
命令模式
在這個例子中,不管大明叫小明做什麽事情,其實都是一樣的,就是下達任務命令,讓小明去執行命令。我們可以利用命令模式把下達任務這個抽象起來,當做父類,下達開發命令、下達暫停命令、下達寫周報等等都是不同的子命令。代碼如下。
public class CommandTest {
public static void main(String[] args) {
Developer xiaoMing = new Developer("小明");
Command developCommand = new DevelopCommand(xiaoMing);
Command suspendCommand = new SuspendCommand(xiaoMing);
Requirement requirement = new Requirement("新增黑名單");
TechnicalManager2 technicalManager = new TechnicalManager2("大明");
technicalManager.setCommand(developCommand);
technicalManager.action(requirement);
System.out.println("開發了 2 天,需求變故,需要暫停。。。");
technicalManager.setCommand(suspendCommand);
technicalManager.action(requirement);
}
}
/**
* 命令
*/
abstract class Command {
protected Developer developer;
public Command(Developer developer) {
this.developer = developer;
}
public abstract void execute(Requirement requirement);
}
/**
* 開始開發
*/
class DevelopCommand extends Command {
public DevelopCommand(Developer developer) {
super(developer);
}
@Override
public void execute(Requirement requirement) {
this.developer.develop(requirement);
}
}
/**
* 開發中斷
*/
class SuspendCommand extends Command {
public SuspendCommand(Developer developer) {
super(developer);
}
@Override
public void execute(Requirement requirement) {
this.developer.suspend(requirement);
}
}
/**
* 技術經理
*/
class TechnicalManager2 {
private String name;
private Command command;
public TechnicalManager2(String name) {
this.name = name;
}
public void action(Requirement requirement) {
this.command.execute(requirement);
}
public void setCommand(Command command) {
this.command = command;
}
}
打印結果:
小明 開始開發需求:新增黑名單
開發了 2 天,需求變故,需要暫停。。。
小明 停止開發需求:新增黑名單
代碼中用 Command 來抽象下達任務,而技術經理 TechnicalManager2 並沒有和 Developer 有直接的關系,而是 TechnicalManager2 和 Command 建立的聯系,Command 和 Developer 建立了聯系。這樣子把大明和小明的強依賴關系給剝離開,而新增一個下達寫周報的任務也很簡單,在 Developer 中新增一個處理寫周報的方法,新增一個寫周報的 Command 子類,就可以了,TechnicalManager2 如上面所願不用修改。這就是完整的一個命令模式代碼。
代碼:
Command Pattern
總結
從文章中我們就可以看到,利用命令模式能夠進行類的解耦,讓調用者和接受者沒有任何關系,也通過對行為的抽象,讓新增其他行為變得清晰容易,也就是可擴展性大大增加。
推薦閱讀:
創建型模式:單例模式(小明就只有 1 輛車)
創建型模式:工廠方法(小明家的車庫)
創建型模式:抽象工廠(寶馬車就得用寶馬輪胎和寶馬方向盤)
創建型模式:建造者模式(湯這麽煲)
創建型模式:原型模式(復印書籍)
行為型模式:模板方法(運動鞋制造過程)
行為型模式:中介者模式(租房找中介)
公眾號後臺回復『大禮包』獲取 Java、Python、IOS 等教程
加個人微信備註『教程』獲取架構師、機器學習等教程
行為型模式:命令模式