国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

《HeadFirst設(shè)計模式(第二版)》第六章代碼——命令模式

這篇具有很好參考價值的文章主要介紹了《HeadFirst設(shè)計模式(第二版)》第六章代碼——命令模式。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

代碼文件目錄:

《HeadFirst設(shè)計模式(第二版)》第六章代碼——命令模式,HeadFirst設(shè)計模式(第二版)源碼,設(shè)計模式,命令模式,java文章來源地址http://www.zghlxwxcb.cn/news/detail-631583.html

Command
package Chapter6_CommandPattern.Command;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public interface Command {
    public void execute();
    public void undo();//撤銷該指令
}
CeilingFan
package Chapter6_CommandPattern.ElectricAppliance;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

//吊扇
public class CeilingFan {

    public static final int HIGH = 3;
    public static final int MEDIUM = 2;
    public static final int LOW = 1;
    public static final int OFF = 0;
    String location;
    int speed;

    public CeilingFan(String location){
        this.location = location;
        this.speed = OFF;
    }

    public void high(){
        this.speed = HIGH;
        System.out.println(this.location+" ceilingFan is on High");
    }

    public void medium(){
        this.speed = MEDIUM;
        System.out.println(this.location+" ceilingFan is on Medium");
    }

    public void low(){
        this.speed = LOW;
        System.out.println(this.location+" ceilingFan is on Low");
    }

    public void off(){
        this.speed = OFF;
        System.out.println(this.location+" ceilingFan is on Off");
    }

    public int getSpeed(){
        return this.speed;
    }

}
CeilingFanHighCommand
package Chapter6_CommandPattern.Command;

import Chapter6_CommandPattern.ElectricAppliance.CeilingFan;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class CeilingFanHighCommand implements Command{
    CeilingFan ceilingFan;
    int prevSpeed;//保存上一個擋位

    public CeilingFanHighCommand(CeilingFan ceilingFan){
        this.ceilingFan = ceilingFan;
    }

    @Override
    public void execute() {
        this.prevSpeed = this.ceilingFan.getSpeed();
        this.ceilingFan.high();
    }

    @Override
    public void undo() {
        if(this.prevSpeed==CeilingFan.HIGH){
            this.ceilingFan.high();
        }else if(this.prevSpeed == CeilingFan.MEDIUM){
            this.ceilingFan.medium();
        }else if(this.prevSpeed == CeilingFan.LOW){
            this.ceilingFan.low();
        }else if(this.prevSpeed == CeilingFan.OFF){
            this.ceilingFan.off();
        }
    }
}
Light
package Chapter6_CommandPattern.ElectricAppliance;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class Light {
    String name;

    public Light(String name){
        this.name = name;
    }

    public void on(){
        System.out.println(this.name+" light is on!");
    }
    public void off(){
        System.out.println(this.name+" light is off!");
    }
}
LightOnCommand
package Chapter6_CommandPattern.Command;

import Chapter6_CommandPattern.Command.Command;
import Chapter6_CommandPattern.ElectricAppliance.Light;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class LightOnCommand implements Command {
    Light light;

    public LightOnCommand(Light light){
        this.light = light;
    }

    @Override
    public void execute() {
        //當(dāng)遙控器運行這個指令的時候,并不知道燈是如何實現(xiàn)打開開關(guān)的
        //實現(xiàn)遙控器與電器之間的解耦合
        this.light.on();
    }

    @Override
    public void undo() {
        this.light.off();
    }
}
LightOffCommand
package Chapter6_CommandPattern.Command;

import Chapter6_CommandPattern.ElectricAppliance.Light;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class LightOffCommand implements Command{
    Light light;

    public LightOffCommand(Light light){
        this.light = light;
    }

    @Override
    public void execute() {
        this.light.off();
    }

    public void undo() {
        this.light.on();
    }
}
GarageDoor
package Chapter6_CommandPattern.ElectricAppliance;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class GarageDoor {
    Light light;

    public GarageDoor(Light light){
        this.light = light;
    }

    public void up(){
        this.lightOn();
        System.out.println("the garage door is open!");
    }

    public void down(){
        this.lightOff();
        System.out.println("the garage door is closed");
    }

    private void lightOn(){
        this.light.on();
    }

    private void lightOff(){
        this.light.off();
    }
}
GarageDoorOpenCommand
package Chapter6_CommandPattern.Command;

import Chapter6_CommandPattern.ElectricAppliance.GarageDoor;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class GarageDoorOpenCommand implements Command{
    GarageDoor garageDoor;

    public GarageDoorOpenCommand(GarageDoor garageDoor){
        this.garageDoor = garageDoor;
    }

    @Override
    public void execute() {
        this.garageDoor.up();
    }

    public void undo() {
        this.garageDoor.down();
    }
}
GarageDoorCloseCommand
package Chapter6_CommandPattern.Command;

import Chapter6_CommandPattern.ElectricAppliance.GarageDoor;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class GarageDoorCloseCommand implements Command{
    GarageDoor garageDoor;

    public GarageDoorCloseCommand(GarageDoor garageDoor){
        this.garageDoor = garageDoor;
    }

    @Override
    public void execute() {
        this.garageDoor.down();
    }

    public void undo() {
        this.garageDoor.down();
    }
}
Stereo
package Chapter6_CommandPattern.ElectricAppliance;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class Stereo {
    int volume;
    //音響類
    public void on(){
        System.out.println("the stereo is on!");
    }
    public void off(){
        System.out.println("the stereo is off!");
    }

    public void setCD(){
        System.out.println("the stereo can work with CD now!");
    }

    public void setDVD(){
        System.out.println("the stereo can work with DVD now!");
    }

    public void setVolume(int volume){//設(shè)置音量
        this.volume = volume;
    }
}
StereoOnWithCDCommand
package Chapter6_CommandPattern.Command;

import Chapter6_CommandPattern.ElectricAppliance.Stereo;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class StereoOnWithCDCommand implements Command{
    Stereo stereo;

    public StereoOnWithCDCommand(Stereo stereo){
        this.stereo = stereo;
    }

    @Override
    public void execute() {
        this.stereo.on();
        this.stereo.setCD();
        this.stereo.setVolume(6);
    }

    @Override
    public void undo() {
        this.stereo.off();
    }
}
StereoOff
package Chapter6_CommandPattern.Command;

import Chapter6_CommandPattern.ElectricAppliance.Stereo;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class StereoOff implements  Command{
    Stereo stereo;

    public StereoOff(Stereo stereo){
        this.stereo = stereo;
    }

    @Override
    public void execute() {
        this.stereo.off();
    }

    public void undo() {
        this.stereo.on();
        this.stereo.setCD();
        this.stereo.setVolume(6);
    }
}
MacroCommand
package Chapter6_CommandPattern.Command;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

//宏指令
public class MacroCommand implements Command{
    Command[] commands;
    public MacroCommand(Command[] commands){
        this.commands = commands;
    }

    @Override
    public void execute() {
        for(int i=0;i<this.commands.length;++i){
            this.commands[i].execute();
        }
    }

    @Override
    public void undo() {
        for(int i=0;i<this.commands.length;++i){
            this.commands[i].undo();
        }
    }
}
NoCommand
package Chapter6_CommandPattern.Command;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class NoCommand implements Command{
    @Override
    public void execute() {
        //this command is useless!
    }

    @Override
    public void undo() {

    }
}
RemoteControl
package Chapter6_CommandPattern;

import Chapter6_CommandPattern.Command.Command;
import Chapter6_CommandPattern.Command.NoCommand;

import java.util.Arrays;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

//遙控器類
public class RemoteControl {
    Command[] onCommands;
    Command[] offCommands;
    Command undoCommand;

    public RemoteControl(){
        //為簡潔起見,這里遙控器只有3個槽位
        this.onCommands = new Command[7];
        this.offCommands = new Command[7];

        Command noCommand = new NoCommand();
        //這里使用空對象,是為了后面不用寫 if(onCommand[i]==null){...}
        for(int i =0;i<7;++i){
            this.onCommands[i] = noCommand;
            this.offCommands[i] = noCommand;
        }
        this.undoCommand = noCommand;
    }

    //設(shè)置某個電器的開關(guān)命令
    public void setCommand(int slot, Command onCommand, Command offCommand){
        this.onCommands[slot] = onCommand;
        this.offCommands[slot] = offCommand;
    }

    public void onButtonWasPushed(int slot){
        this.onCommands[slot].execute();
        this.undoCommand = this.onCommands[slot];
    }

    public void offButtonWasPushed(int slot){
        this.offCommands[slot].execute();
        this.undoCommand = this.offCommands[slot];
    }

    public void undoButtonWasPushed(){
        this.undoCommand.undo();
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("\n---------- Remote Control ----------\n");
        for(int i = 0;i<this.onCommands.length;i++){
            stringBuffer.append("[slot "+ i +"] "+
                    String.format("%-30s",this.onCommands[i].getClass().getSimpleName())+
                    String.format("%-30s",this.offCommands[i].getClass().getSimpleName())+"\n"
            );
        }
        stringBuffer.append("[undo] "+this.undoCommand.getClass().getSimpleName());
        return stringBuffer.toString();
    }
}
RemoteLoader
package Chapter6_CommandPattern;

import Chapter6_CommandPattern.Command.*;
import Chapter6_CommandPattern.ElectricAppliance.CeilingFan;
import Chapter6_CommandPattern.ElectricAppliance.GarageDoor;
import Chapter6_CommandPattern.ElectricAppliance.Light;
import Chapter6_CommandPattern.ElectricAppliance.Stereo;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class RemoteLoader {
    public static void main(String[] args) {
        RemoteControl remoteControl = new RemoteControl();

        //創(chuàng)建設(shè)備
        Light LivingRoomLight = new Light("LivingRoom");
        Light KitchenLight = new Light("Kitchen");
        Light GarageLight = new Light("Garage");
        GarageDoor garageDoor = new GarageDoor(GarageLight);
        Stereo stereo = new Stereo();

        //創(chuàng)建命令對象
        LightOnCommand livingRoomLightOn =new LightOnCommand(LivingRoomLight);
        LightOnCommand kitchenLightOn = new LightOnCommand(KitchenLight);
        GarageDoorOpenCommand garageDoorOpenCommand =
                new GarageDoorOpenCommand(garageDoor);
        StereoOnWithCDCommand stereoOnWithCDCommand =
                new StereoOnWithCDCommand(stereo);

        LightOffCommand lightOffCommand = new LightOffCommand(LivingRoomLight);
        LightOffCommand kitchenLightOff = new LightOffCommand(KitchenLight);
        GarageDoorCloseCommand garageDoorCloseCommand =
                new GarageDoorCloseCommand(garageDoor);
        StereoOff stereoOff = new StereoOff(stereo);

        //將命令加載進(jìn)入槽位
        remoteControl.setCommand(0,livingRoomLightOn,lightOffCommand);
        remoteControl.setCommand(1,kitchenLightOn,kitchenLightOff);
        remoteControl.setCommand(2,garageDoorOpenCommand,garageDoorCloseCommand);
        remoteControl.setCommand(3,stereoOnWithCDCommand,stereoOff);

        //展示
//        System.out.println(remoteControl);

        remoteControl.onButtonWasPushed(0);
        remoteControl.onButtonWasPushed(1);
        remoteControl.onButtonWasPushed(2);
        remoteControl.onButtonWasPushed(3);

        remoteControl.offButtonWasPushed(0);
        remoteControl.offButtonWasPushed(1);
        remoteControl.offButtonWasPushed(2);
        remoteControl.offButtonWasPushed(3);

        //使用lambda簡潔編寫代碼
        //增加撤銷按鈕后就不能使用了
        //這樣就不用寫一大堆命令類了
//        remoteControl.setCommand(4,()->LivingRoomLight.on(),
//                ()->LivingRoomLight.off());

        //測試撤銷功能
        System.out.println("\n測試撤銷:");
        remoteControl.onButtonWasPushed(0);
        remoteControl.undoButtonWasPushed();

        //測試風(fēng)扇
        System.out.println("test fan");
        CeilingFan ceilingFan = new CeilingFan("livingRoom");
        CeilingFanHighCommand command = new CeilingFanHighCommand(ceilingFan);
        remoteControl.setCommand(4,command,command);

        System.out.println(remoteControl);

        remoteControl.onButtonWasPushed(4);
        System.out.println(remoteControl);

        remoteControl.undoButtonWasPushed();

        //測試宏指令
        System.out.println("\n宏指令");
        Light light1 = new Light("Room1");
        Light light2 = new Light("Room2");
        Light light3 = new Light("Room3");
        Light light4 = new Light("Room4");

        LightOnCommand lightOnCommand1 = new LightOnCommand(light1);
        LightOnCommand lightOnCommand2 = new LightOnCommand(light2);
        LightOnCommand lightOnCommand3 = new LightOnCommand(light3);
        LightOnCommand lightOnCommand4 = new LightOnCommand(light4);

        LightOffCommand lightOffCommand1 = new LightOffCommand(light1);
        LightOffCommand lightOffCommand2 = new LightOffCommand(light2);
        LightOffCommand lightOffCommand3 = new LightOffCommand(light3);
        LightOffCommand lightOffCommand4 = new LightOffCommand(light4);

        Command[] onCommands = {lightOnCommand1,lightOnCommand2,lightOnCommand3,lightOnCommand4};
        Command[] offCommand = {lightOffCommand1,lightOffCommand2,lightOffCommand3,lightOffCommand4};
        MacroCommand partyOn = new MacroCommand(onCommands);
        MacroCommand partyOff = new MacroCommand(offCommand);

        remoteControl.setCommand(5,partyOn,partyOff);

        System.out.println(remoteControl);
        remoteControl.onButtonWasPushed(5);
        remoteControl.offButtonWasPushed(5);
        remoteControl.undoButtonWasPushed();


    }
}
SimpleRemoteControl
package Chapter6_CommandPattern;

import Chapter6_CommandPattern.Command.Command;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class SimpleRemoteControl {
    Command slot;//目前該遙控器只支持一個槽位

    public SimpleRemoteControl(){}

    public void setCommand(Command command){
        this.slot = command;
    }

    public void buttonWasPressed(){
        this.slot.execute();
    }

}
RemoteControlTest
package Chapter6_CommandPattern;

import Chapter6_CommandPattern.Command.GarageDoorOpenCommand;
import Chapter6_CommandPattern.ElectricAppliance.GarageDoor;
import Chapter6_CommandPattern.ElectricAppliance.Light;

/**
 * @Author 竹心
 * @Date 2023/8/6
 **/

public class RemoteControlTest {
    public static void main(String[] args) {
        SimpleRemoteControl remote = new SimpleRemoteControl();
        Light light = new Light("garage light");
        GarageDoor garageDoor = new GarageDoor(light);
        GarageDoorOpenCommand garageDoorOpen = new GarageDoorOpenCommand(garageDoor);

        remote.setCommand(garageDoorOpen);
        remote.buttonWasPressed();
    }
}
notes.txt
命令模式:
    把請求封裝成為對象,以便對不同的請求、隊列或者日志請求來參數(shù)化對象,
    并支持可撤銷的操作

命令對象只暴露一個方法:execute(),命令的請求者不會知道執(zhí)行者具體如何實現(xiàn)指令

當(dāng)需要將發(fā)出請求的對象和能夠?qū)嵭姓埱蟮膶ο蠼怦詈系臅r候可以使用命令模式

到了這里,關(guān)于《HeadFirst設(shè)計模式(第二版)》第六章代碼——命令模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 《HeadFirst設(shè)計模式(第二版)》第十一章代碼——代理模式

    《HeadFirst設(shè)計模式(第二版)》第十一章代碼——代理模式

    代碼文件目錄: ?RMI: MyRemote MyRemoteClient MyRemoteImpl 能夠遠(yuǎn)程監(jiān)控的糖果機(jī): 在上一章的代碼的基礎(chǔ)上做一些修改 GumballMachine GumballMachineRemote GumballMachineTestDrive GumballMonitor GumballMonitorTestDrive 五個狀態(tài)類: 同樣的修改:

    2024年02月12日
    瀏覽(29)
  • 《HeadFirst設(shè)計模式(第二版)》第五章代碼——單例模式

    《HeadFirst設(shè)計模式(第二版)》第五章代碼——單例模式

    代碼文件目錄: ?初始版本: 三種解決多線程問題的方法: Notes:

    2024年02月13日
    瀏覽(22)
  • 《HeadFirst設(shè)計模式(第二版)》第八章代碼——模板方法模式

    《HeadFirst設(shè)計模式(第二版)》第八章代碼——模板方法模式

    代碼文件目錄: ? CaffeineBeverage Coffee Tea notes

    2024年02月12日
    瀏覽(25)
  • 《python語言程序設(shè)計基礎(chǔ)》(第二版)第六章課后習(xí)題參考答案

    第六章 組合數(shù)據(jù)類型 6.1 隨機(jī)密碼生成 6.2 重復(fù)元素判定 6.3 重復(fù)元素判定續(xù) 6.4 文本字符分析 6.5 生日悖論分析 6.6 《紅樓夢》人物統(tǒng)計 注:上述代碼僅供參考,若有問題可在評論區(qū)留言! 《紅樓夢》及人物名單TXT (百度云鏈接失效可在評論區(qū)留言) 鏈接:https://pan.baidu.c

    2024年02月05日
    瀏覽(24)
  • 《微服務(wù)架構(gòu)設(shè)計模式》第二章

    《微服務(wù)架構(gòu)設(shè)計模式》第二章

    軟件架構(gòu)的定義 看一下大佬是怎么說的: 計算機(jī)系統(tǒng)的軟件架構(gòu)是構(gòu)建這個系統(tǒng)所需要的一組結(jié)構(gòu),包括軟件元素、它們之間的關(guān)系以及兩者的屬性。 --Bass等著《Documenting Software Architectures:Views and Beyond》 這個定義將軟件分解為元素和元素之間的關(guān)系兩個部分,就像一輛汽車

    2024年02月09日
    瀏覽(22)
  • 二十三種設(shè)計模式第二十篇--備忘錄模式

    二十三種設(shè)計模式第二十篇--備忘錄模式

    備忘錄模式,備忘錄模式屬于行為型模式。它允許在不破壞封裝的情況下捕獲和恢復(fù)對象的內(nèi)部狀態(tài)。 保存一個對象的某個狀態(tài),以便在適當(dāng)?shù)臅r候恢復(fù)對象,該模式通過創(chuàng)建一個備忘錄對象來保存原始對象的狀態(tài),并將其存儲在一個負(fù)責(zé)管理備忘錄的負(fù)責(zé)人對象中。 備忘

    2024年02月14日
    瀏覽(23)
  • 二十三種設(shè)計模式第二十四篇--訪問者模式(完結(jié)撒花)

    二十三種設(shè)計模式第二十四篇--訪問者模式(完結(jié)撒花)

    在訪問者模式(Visitor Pattern)中,我們使用了一個訪問者類,它改變了元素類的執(zhí)行算法。 通過這種方式,元素的執(zhí)行算法可以隨著訪問者改變而改變。 這種類型的設(shè)計模式屬于行為型模式。根據(jù)模式,元素對象已接受訪問者對象,這樣訪問者對象就可以處理元素對象上的

    2024年02月14日
    瀏覽(24)
  • 《微服務(wù)架構(gòu)設(shè)計模式》第二章 服務(wù)的拆分策略

    《微服務(wù)架構(gòu)設(shè)計模式》第二章 服務(wù)的拆分策略

    內(nèi)容總結(jié)自《微服務(wù)架構(gòu)設(shè)計模式》 軟件架構(gòu)的定義:計算機(jī)系統(tǒng)的軟件架構(gòu)是構(gòu)建這個系統(tǒng)所需要的一組結(jié)構(gòu),包括軟件元素、他們之間的關(guān)系以及兩者的屬性(Bass等著) 其實質(zhì)是應(yīng)用程續(xù)的架構(gòu)將軟件分解為元素(element)和這些元素之間的關(guān)系(relation)。由于這兩個

    2024年02月09日
    瀏覽(25)
  • 第六章、代理模式

    代理模式是為一個對象提供一個代用品或占位符,以便控制對它的訪問。 代理模式的關(guān)鍵是,當(dāng)客戶不方便直接訪問一個對象或者不滿足需求時,提供一個替身對象來控制對這個對象的訪問,客戶實際上訪問的是替身對象。 6.1舉個栗子 小明想要對女神A表白,送鮮花,可是他

    2024年02月19日
    瀏覽(26)
  • 第六章:自建MVVM模式的簡易項目框架

    第六章:自建MVVM模式的簡易項目框架

    前面的幾章為我們筑好了MVVM的基礎(chǔ),在這一章中我們將自己搭建一個MVVM模式下的簡易項目框架。 MVVM是一種模式,主要目的是降低界面和業(yè)務(wù)邏輯的耦合。 MVVM是Model-View-ViewModel(模型-視圖-視圖模型)的簡寫。是WPF開發(fā)過程中必不可少的一種開發(fā)模式,MVVM基本結(jié)構(gòu)如下圖所

    2023年04月26日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包