Java設計模式之命令模式詳解
命令模式
定義:將請求封裝成對象,這可以讓你使用不同的請求、隊列、或者日志來參數化其他對象。
何時使用命令模式?當需要將發(fā)出請求的對象和執(zhí)行請求的對象解耦的時候,使用命令模式。
在被解耦的兩者之間是通過命令對象進行溝通的。命令對象封裝了接收者和一個或一組動作。
調用者通過調用命令對象的execute()方法發(fā)出請求,這會使接收者的動作被調用。
調用者可以接收命令當作參數,甚至在運行時動態(tài)地進行。
優(yōu)點:
1、降低了系統(tǒng)耦合度。
2、新的命令可以很容易添加到系統(tǒng)中去。
缺點:使用命令模式可能會導致某些系統(tǒng)有過多的具體命令類。
實現案例
需求:實現一個遙控器類,遙控器有多組開/關按鈕,每組按鈕控制一個電器對象的開和關。使用命令模式實現整個功能。
1、創(chuàng)建命令接口
package com.example.designpatternsdemo.commandPattern; /** * 命令接口 */ public interface Command { public void execute(); }
2、創(chuàng)建電燈、電視類(命令接收者)
package com.example.designpatternsdemo.commandPattern; /** * 電燈 */ public class Light { //on/off動作 public void on(){ System.out.println("開啟電燈"); } public void off(){ System.out.println("關閉電燈"); } }
package com.example.designpatternsdemo.commandPattern; /** * 電視 */ public class TV { public void on(){ System.out.println("開啟電視"); } public void off(){ System.out.println("關閉電視"); } }
3、創(chuàng)建具體命令類
package com.example.designpatternsdemo.commandPattern; /** * 開啟電燈命令類 */ public class LightOnCommand implements Command{ //電燈對象 private Light mLight; public LightOnCommand(Light light){ this.mLight = light; } @Override public void execute() { mLight.on(); } }
package com.example.designpatternsdemo.commandPattern; /** * 關閉電燈命令類 */ public class LightOffCommand implements Command{ //電燈對象 private Light mLight; public LightOffCommand(Light light){ this.mLight = light; } @Override public void execute() { mLight.off(); } }
這里省略電視的開關命令類
4、創(chuàng)建遙控器類
package com.example.designpatternsdemo.commandPattern; /** * 命令調用者 * 遙控器 */ public class RemoteControl { //保存開關命令對象的數組 Command[] onCommands; Command[] offCommands; public RemoteControl(){ //初始化數組 onCommands = new Command[2]; offCommands = new Command[2]; } //存儲命令 public void setCommand(int index,Command onCommand,Command offCommand){ onCommands[index] = onCommand; offCommands[index] = offCommand; } //開按鈕 public void onButtonPress(int btnIndex){ onCommands[btnIndex].execute(); } //關按鈕 public void offButtonPress(int btnIndex){ offCommands[btnIndex].execute(); } }
5、創(chuàng)建客戶類(測試類)
package com.example.designpatternsdemo.commandPattern; public class Client { public static void main(String[] args) { //創(chuàng)建電燈對象(命令接收者) Light myLight = new Light(); //創(chuàng)建電燈開關命令對象 LightOnCommand lightOnCommand = new LightOnCommand(myLight); LightOffCommand lightOffCommand = new LightOffCommand(myLight); //創(chuàng)建遙控器對象(命令調用者) RemoteControl remoteControl = new RemoteControl(); //設置命令 remoteControl.setCommand(0,lightOnCommand,lightOffCommand); //發(fā)起命令執(zhí)行請求 remoteControl.onButtonPress(0); remoteControl.offButtonPress(0); TV myTv = new TV(); TvOnCommand tvOnCommand = new TvOnCommand(myTv); TvOffCommand tvOffCommand = new TvOffCommand(myTv); remoteControl.setCommand(1,tvOnCommand,tvOffCommand); remoteControl.onButtonPress(1); remoteControl.offButtonPress(1); } }
將開/關電燈、電視的請求封裝成命令類對象,在類中的execute()方法調用具體電燈/電視的開關動作,調用時,遙控器類(命令調用者)只需調用具體某個命令對象的execute()方法,那他就會通知具體的電器來執(zhí)行動作,這樣,就實現了命令調用者和執(zhí)行者之間的解耦。調用者不需要知道是誰執(zhí)行動作,只需要調用命令對象的execute()方法去通知執(zhí)行就可以。
到此這篇關于Java設計模式之命令模式詳解的文章就介紹到這了,更多相關Java命令模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot+webmagic實現java爬蟲jdbc及mysql的方法
今天小編就為大家分享一篇springboot+webmagic實現java爬蟲jdbc及mysql的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08MybatisPlus自動填充創(chuàng)建(更新)時間問題
在開發(fā)數據庫相關應用時,手動設置創(chuàng)建和更新時間會導致代碼冗余,MybatisPlus提供了自動填充功能,通過實現MetaObjectHandler接口并重寫insertFill、updateFill方法,可以自動維護創(chuàng)建時間、更新時間等字段,極大簡化了代碼,這不僅提高了開發(fā)效率,也保證了數據的可追溯性2024-09-09Spring事務@Transactional注解四種不生效案例場景分析
這篇文章主要為大家介紹了Spring事務@Transactional注解四種不生效的案例場景示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07