命令模式

1.定义
命令模式:命令模式将“请求”封装成对象,以便于使用不同的请求、队列或者日志来参数化其他对象。
2.案例分析
这里写图片描述

package org.sxd.command;

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

package org.sxd.command;

public class GarageDoor {

public void up(){
    System.out.println("GarbageDoor up");
}

public void down(){
    System.out.println("GarbageDoor down");
}

public void stop(){
    System.out.println("GarbageDoor stop");
}

public void lightOn(){
    System.out.println("GarbageDoor lightOn");
}

public void lightOff(){
    System.out.println("GarbageDoor lightOff");
}

}

package org.sxd.command;

public class GarageDoorOpenCommand implements Command{
    GarageDoor garageDoor;

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

    @Override
    public void execute() {
        // TODO Auto-generated method stub
        garageDoor.up();
        garageDoor.lightOn();
    }

}
package org.sxd.command;

public class Light {

    public void on(){
        System.out.println("light is on");
    }

    public void off(){
        System.out.println("light is off");
    }

}
package org.sxd.command;

public class LightOffCommand implements Command{
    Light light;

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

    @Override
    public void execute() {
        // TODO Auto-generated method stub
        light.off();
    }

}
package org.sxd.command;

public class LightOnCommand implements Command{
    Light light;

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

    @Override
    public void execute() {
        // TODO Auto-generated method stub
        light.on();
    }

}
package org.sxd.command;

public class NoCommand implements Command{

    @Override
    public void execute() {
        // TODO Auto-generated method stub
        System.out.println("no command");
    }

}
package org.sxd.command;

public class RemoteControl {
    Command[] onCommands;
    Command[] offCommands;

    public RemoteControl(){
        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;
        }
    }

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

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

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

    public String toString(){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("\n------ Remote Control ------\n");
        for(int i = 0; i < onCommands.length; i++){
            stringBuffer.append("[slot " + i + "] " + onCommands[i].getClass().getName()
                    + "     " + offCommands[i].getClass().getName() + "\n");
        }
        return stringBuffer.toString();
    }

}
package org.sxd.command;

public class RemoteControlTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SimpleRemoteControl remote = new SimpleRemoteControl();
        Light light = new Light();
        GarageDoor garageDoor = new GarageDoor();
        LightOnCommand lightOn = new LightOnCommand(light);
        GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor);

        remote.setCommand(lightOn);
        remote.buttonWasPressed();
        remote.setCommand(garageOpen);
        remote.buttonWasPressed();
    }

}
package org.sxd.command;

public class SimpleRemoteControl {
    Command slot;

    public SimpleRemoteControl(){}

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

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

}
package org.sxd.command;

public class Stereo {

    public void On(){
        System.out.println("stereo is on");
    }

    public void Off(){
        System.out.println("stereo is off");
    }

    public void setCd(){
        System.out.println("stereo  setCd");
    }

    public void setDvd(){
        System.out.println("stereo setDvd");
    }

    public void setRadio(){
        System.out.println("stereo setRadio");
    }

    public void setVolume(int volume){
        System.out.println("stereo volume is " + volume);
    }

}
package org.sxd.command;

public class StereoOnWithCDCommand implements Command{
    Stereo stereo;

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

    @Override
    public void execute() {
        // TODO Auto-generated method stub
        stereo.On();
        stereo.setCd();
        stereo.setVolume(11);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值