命令模式

实现命令接口

public interface Command() {
    public void execute();
    public void undo();
}

实现一个打开点灯的命令

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

    public void execute() {
        light.on();
    }

    public void undo() {
        light.off();
    }
}

实现一个关灯的命令

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

    public void execute() {
        light.off();
    }

    public void undo() {
        light.on();
    }
}

遥控器RemoteControlWithUndo的实现如下:

public class RemoteControlWithUndo{
    Command[] onCommands;
    Command[] offCommands;
    Command undoCommand;

    public RemoteControlWithUndo() {
        onCommands=new Command[7];
        offCommands=new Command[7];

        Command noCommand=new NoCommand();
        for(int i=0;i<7;i++) {
            onCommands[i]=noCommand;
            offCommands[i]=noCommand;
        }
        undoCommand=noCommand;
    }

    public void setCommand(int slot,Command onCommand,Command offCommand) {
        onCommand[slot]=onCommand;
        offCommanad[slot]=offCommand;
    }

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

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

    public void undoCommandWasPushed() {
        undoCommand.undo();
    }

    public String toString() {
        //这里是toString代码
    }
}

在遥控器中,我们不想每次都检查是否某个插槽都加载了命令。比方说,在这个onButtonWasPushed()方法中,我们可能需要这样的代码:

public void onButtonWasPushed(int slot) {
    if(onCommands[slot]!=null) {
        onCommands[slot].execute();
    }
}

所以,我们要如何避免上述的做法?实现一个不做事情的命令!

public class NoCommand implements Command{
    public void execute() {}
}

逐步测试遥控器

public class RemoteLoader{

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

        Light livingRommLight=new Light("Living Room");
        Light kitchenLight=new Light("Kitchen");

        LightOnCommand livingRoomLightOn=new LightOnCommand(livingRoomLight);
        LightOffCommand livingRoomLightOff=new LightOffCommand(livingRoomLight);
        LightOnCommand kitchenLightOn=new LightOnCommand(kitchenLight);
        LightOffCommand kitchenLightOff=new LightOffCommand(kitchenLight);

        remoteControl.setCommand(0,livingRoomLightOn,livingRoomLightOff);
        remoteControl.setCommand(1,kitchenLightOn,kitchenLightOff);

        System.out.println(remoteControl);

        remoteControl.onButtonWasPushed(0);
        remoteControl.offButtonWasPushed(0);
        remoteControl.undoButtonWashPushed(0);
        remoteControl.onButtonWasPushed(1);
        remoteControl.offButtonWasPushed(1);
        remoteControl.undoButtonWashPushed(1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值