您好,登錄后才能下訂單哦!
如何在Java中使用命令模式?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
Java的基本數(shù)據(jù)類型分為:1、整數(shù)類型,用來表示整數(shù)的數(shù)據(jù)類型。2、浮點(diǎn)類型,用來表示小數(shù)的數(shù)據(jù)類型。3、字符類型,字符類型的關(guān)鍵字是“char”。4、布爾類型,是表示邏輯值的基本數(shù)據(jù)類型。
定義:將請(qǐng)求封裝成對(duì)象,這可以讓你使用不同的請(qǐng)求、隊(duì)列、或者日志來參數(shù)化其他對(duì)象。
何時(shí)使用命令模式?當(dāng)需要將發(fā)出請(qǐng)求的對(duì)象和執(zhí)行請(qǐng)求的對(duì)象解耦的時(shí)候,使用命令模式。
在被解耦的兩者之間是通過命令對(duì)象進(jìn)行溝通的。命令對(duì)象封裝了接收者和一個(gè)或一組動(dòng)作。
調(diào)用者通過調(diào)用命令對(duì)象的execute()方法發(fā)出請(qǐng)求,這會(huì)使接收者的動(dòng)作被調(diào)用。
調(diào)用者可以接收命令當(dāng)作參數(shù),甚至在運(yùn)行時(shí)動(dòng)態(tài)地進(jìn)行。
優(yōu)點(diǎn):
1、降低了系統(tǒng)耦合度。
2、新的命令可以很容易添加到系統(tǒng)中去。
缺點(diǎn):使用命令模式可能會(huì)導(dǎo)致某些系統(tǒng)有過多的具體命令類。
需求:實(shí)現(xiàn)一個(gè)遙控器類,遙控器有多組開/關(guān)按鈕,每組按鈕控制一個(gè)電器對(duì)象的開和關(guān)。使用命令模式實(shí)現(xiàn)整個(gè)功能。
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動(dòng)作 public void on(){ System.out.println("開啟電燈"); } public void off(){ System.out.println("關(guān)閉電燈"); } }
package com.example.designpatternsdemo.commandPattern; /** * 電視 */ public class TV { public void on(){ System.out.println("開啟電視"); } public void off(){ System.out.println("關(guān)閉電視"); } }
3、創(chuàng)建具體命令類
package com.example.designpatternsdemo.commandPattern; /** * 開啟電燈命令類 */ public class LightOnCommand implements Command{ //電燈對(duì)象 private Light mLight; public LightOnCommand(Light light){ this.mLight = light; } @Override public void execute() { mLight.on(); } }
package com.example.designpatternsdemo.commandPattern; /** * 關(guān)閉電燈命令類 */ public class LightOffCommand implements Command{ //電燈對(duì)象 private Light mLight; public LightOffCommand(Light light){ this.mLight = light; } @Override public void execute() { mLight.off(); } }
這里省略電視的開關(guān)命令類
4、創(chuàng)建遙控器類
package com.example.designpatternsdemo.commandPattern; /** * 命令調(diào)用者 * 遙控器 */ public class RemoteControl { //保存開關(guān)命令對(duì)象的數(shù)組 Command[] onCommands; Command[] offCommands; public RemoteControl(){ //初始化數(shù)組 onCommands = new Command[2]; offCommands = new Command[2]; } //存儲(chǔ)命令 public void setCommand(int index,Command onCommand,Command offCommand){ onCommands[index] = onCommand; offCommands[index] = offCommand; } //開按鈕 public void onButtonPress(int btnIndex){ onCommands[btnIndex].execute(); } //關(guān)按鈕 public void offButtonPress(int btnIndex){ offCommands[btnIndex].execute(); } }
5、創(chuàng)建客戶類(測(cè)試類)
package com.example.designpatternsdemo.commandPattern; public class Client { public static void main(String[] args) { //創(chuàng)建電燈對(duì)象(命令接收者) Light myLight = new Light(); //創(chuàng)建電燈開關(guān)命令對(duì)象 LightOnCommand lightOnCommand = new LightOnCommand(myLight); LightOffCommand lightOffCommand = new LightOffCommand(myLight); //創(chuàng)建遙控器對(duì)象(命令調(diào)用者) RemoteControl remoteControl = new RemoteControl(); //設(shè)置命令 remoteControl.setCommand(0,lightOnCommand,lightOffCommand); //發(fā)起命令執(zhí)行請(qǐng)求 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); } }
將開/關(guān)電燈、電視的請(qǐng)求封裝成命令類對(duì)象,在類中的execute()方法調(diào)用具體電燈/電視的開關(guān)動(dòng)作,調(diào)用時(shí),遙控器類(命令調(diào)用者)只需調(diào)用具體某個(gè)命令對(duì)象的execute()方法,那他就會(huì)通知具體的電器來執(zhí)行動(dòng)作,這樣,就實(shí)現(xiàn)了命令調(diào)用者和執(zhí)行者之間的解耦。調(diào)用者不需要知道是誰執(zhí)行動(dòng)作,只需要調(diào)用命令對(duì)象的execute()方法去通知執(zhí)行就可以。
關(guān)于如何在Java中使用命令模式問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。