国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Java設計模式之命令模式詳解

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:在下木子李 欄目: 編程語(yǔ)言 歡迎投稿:712375056

命令模式

定義:將請求封裝成對象,這可以讓你使用不同的請求、隊列、或者日志來(lái)參數化其他對象。

何時(shí)使用命令模式?當需要將發(fā)出請求的對象和執行請求的對象解耦的時(shí)候,使用命令模式。

在被解耦的兩者之間是通過(guò)命令對象進(jìn)行溝通的。命令對象封裝了接收者和一個(gè)或一組動(dòng)作。

調用者通過(guò)調用命令對象的execute()方法發(fā)出請求,這會(huì )使接收者的動(dòng)作被調用。

調用者可以接收命令當作參數,甚至在運行時(shí)動(dòng)態(tài)地進(jìn)行。

優(yōu)點(diǎn):

1、降低了系統耦合度。

2、新的命令可以很容易添加到系統中去。

缺點(diǎn):使用命令模式可能會(huì )導致某些系統有過(guò)多的具體命令類(lèi)。

實(shí)現案例

需求:實(shí)現一個(gè)遙控器類(lèi),遙控器有多組開(kāi)/關(guān)按鈕,每組按鈕控制一個(gè)電器對象的開(kāi)和關(guān)。使用命令模式實(shí)現整個(gè)功能。

1、創(chuàng )建命令接口

package com.example.designpatternsdemo.commandPattern;

/**
 * 命令接口
 */
public interface Command {
    public void execute();
}

2、創(chuàng )建電燈、電視類(lèi)(命令接收者)

package com.example.designpatternsdemo.commandPattern;

/**
 * 電燈
 */
public class Light {
	//on/off動(dòng)作
    public void on(){
        System.out.println("開(kāi)啟電燈");
    }
    public void off(){
        System.out.println("關(guān)閉電燈");
    }
}
package com.example.designpatternsdemo.commandPattern;

/**
 * 電視
 */
public class TV {
    public void on(){
        System.out.println("開(kāi)啟電視");
    }

    public void off(){
        System.out.println("關(guān)閉電視");
    }
}

3、創(chuàng )建具體命令類(lèi)

package com.example.designpatternsdemo.commandPattern;

/**
 * 開(kāi)啟電燈命令類(lèi)
 */
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;

/**
 * 關(guān)閉電燈命令類(lèi)
 */
public class LightOffCommand implements Command{
    //電燈對象
    private Light mLight;

    public LightOffCommand(Light light){
        this.mLight = light;
    }
    @Override
    public void execute() {
        mLight.off();
    }
}

這里省略電視的開(kāi)關(guān)命令類(lèi)

4、創(chuàng )建遙控器類(lèi)

package com.example.designpatternsdemo.commandPattern;

/**
 * 命令調用者
 * 遙控器
 */
public class RemoteControl {
    //保存開(kāi)關(guān)命令對象的數組
    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;
    }
    //開(kāi)按鈕
    public void onButtonPress(int btnIndex){
        onCommands[btnIndex].execute();
    }

    //關(guān)按鈕
    public void offButtonPress(int btnIndex){
        offCommands[btnIndex].execute();
    }
}

5、創(chuàng )建客戶(hù)類(lèi)(測試類(lèi))

package com.example.designpatternsdemo.commandPattern;

public class Client {

    public static void main(String[] args) {
        //創(chuàng  )建電燈對象(命令接收者)
        Light myLight = new Light();
        //創(chuàng  )建電燈開(kāi)關(guān)命令對象
        LightOnCommand lightOnCommand = new LightOnCommand(myLight);
        LightOffCommand lightOffCommand = new LightOffCommand(myLight);
        //創(chuàng  )建遙控器對象(命令調用者)
        RemoteControl remoteControl = new RemoteControl();
        //設置命令
        remoteControl.setCommand(0,lightOnCommand,lightOffCommand);
        //發(fā)起命令執行請求
        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);
    }
}

將開(kāi)/關(guān)電燈、電視的請求封裝成命令類(lèi)對象,在類(lèi)中的execute()方法調用具體電燈/電視的開(kāi)關(guān)動(dòng)作,調用時(shí),遙控器類(lèi)(命令調用者)只需調用具體某個(gè)命令對象的execute()方法,那他就會(huì )通知具體的電器來(lái)執行動(dòng)作,這樣,就實(shí)現了命令調用者和執行者之間的解耦。調用者不需要知道是誰(shuí)執行動(dòng)作,只需要調用命令對象的execute()方法去通知執行就可以。

到此這篇關(guān)于Java設計模式之命令模式詳解的文章就介紹到這了,更多相關(guān)Java命令模式內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

亚洲综合在线视频自拍| 无码熟妇人妻AV影音先锋| 夜夜国产亚洲视频香蕉| 在线观看欧美一区二区三区| 噼里啪啦的在线观看免费| 日产精品久久久久久久蜜臀|