命令模式

来源:互联网 发布:vasp 5.3.5 软件下载 编辑:程序博客网 时间:2024/06/04 00:49

命令模式:将“请求”封装成对象,以便使用不同的请求队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。

这里写图片描述
下面我们以一个多功能遥控器为例,遥控器可以控制厨房灯的开关、客厅灯的开关、以及音响的开关,同时也支持撤销操作和宏命令。

1. 灯

public interface Light {    /**     * 打开     */    void on();    /**     * 关闭     */    void off();}public class KitchenLight implements Light{    public void on() {        System.out.println("Kitchen Light is on");    }    public void off() {        System.out.println("Kitchen Light is off");    }}public class LivingRoomLight implements Light{    public void on() {        System.out.println("Living Room light is on");    }    public void off() {        System.out.println("Living Room light is off");    }}

2. 音响

public interface Stereo {    /**     * 打开     */    void on();    /**     * 关闭     */    void off();    /**     * 设置CD     */    void setCD();    /**     * 设置音量     * @param volume     */    void setVolume(int volume);}public class LivingRoomStereo implements Stereo{    public void on() {        System.out.println("Living Room Stereo is on");    }    public void off() {        System.out.println("Living Room Stereo is off");    }    public void setCD() {        System.out.println("Living Room Stereo set cd");    }    public void setVolume(int volume) {        System.out.println("Living Room Stereo set volume");    }}

3. 命令接口

public interface Command {    /***     * 执行     */    void execute();    /**     * 撤销     */    void undo();}

4. 空命令

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

5. 灯命令

public class LightOnCommand implements Command{    private Light light;    public LightOnCommand(Light light) {        super();        this.light = light;    }    public void execute() {        light.on();    }    public void undo() {        light.off();    }}public class LightOffCommand implements Command{    private Light light;    public LightOffCommand(Light light) {        super();        this.light = light;    }    public void execute() {        light.off();    }    public void undo() {        light.on();    }}

6. 音响命令

public class StereoOnWithCDCommand implements Command{    private Stereo stereo;    public StereoOnWithCDCommand(Stereo stereo) {        super();        this.stereo = stereo;    }    public void execute() {        stereo.on();        stereo.setCD();        stereo.setVolume(11);    }    public void undo() {        stereo.off();    }}public class StereoOffWithCDCommand implements Command{    private Stereo stereo;    public StereoOffWithCDCommand(Stereo stereo) {        super();        this.stereo = stereo;    }    public void execute() {        stereo.off();    }    public void undo() {        stereo.on();        stereo.setCD();        stereo.setVolume(11);    }}

7. 宏命令

public class MacroCommand implements Command{    Command[] commands;    public MacroCommand(Command[] commands) {        super();        this.commands = commands;    }    public void execute() {        for (Command command : commands) {            command.execute();        }    }    public void undo() {        for (Command command : commands) {            command.undo();        }    }}

8. 遥控器

public class RemoteControl {    Command[] onCommands;    Command[] offCommands;    Command undoCommand;    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;        }        undoCommand = noCommand;    }    public void setCommand(int solt, Command onCommand, Command offCommand){        onCommands[solt] = onCommand;        offCommands[solt] = offCommand;    }    public void onButtonWasPushed(int slot){        onCommands[slot].execute();        undoCommand = onCommands[slot];    }    public void offButtonWasPushed(int slot){        offCommands[slot].execute();        undoCommand = offCommands[slot];    }    public void undo(){        undoCommand.undo();;    }}

9. 测试

public class CommandTest {    public static void main(String[] args) {        RemoteControl control = new RemoteControl();        Light livingRoomLight = new LivingRoomLight();        KitchenLight kitchenLight = new KitchenLight();        LivingRoomStereo livingRoomStereo = new LivingRoomStereo();        control.setCommand(0, new LightOnCommand(livingRoomLight), new LightOffCommand(livingRoomLight));        control.setCommand(1, new LightOnCommand(kitchenLight), new LightOffCommand(kitchenLight));        control.setCommand(2, new StereoOnWithCDCommand(livingRoomStereo), new StereoOffWithCDCommand(livingRoomStereo));        control.onButtonWasPushed(0);        control.onButtonWasPushed(1);        control.onButtonWasPushed(2);        System.out.println("--------------------------------");        control.offButtonWasPushed(0);        control.offButtonWasPushed(1);        control.offButtonWasPushed(2);        System.out.println("--------------------------------");        control.undo();        System.out.println("--------------------------------");        MacroCommand lightOn = new MacroCommand(new Command[]{new LightOnCommand(livingRoomLight),new LightOnCommand(kitchenLight)});        MacroCommand lightOff = new MacroCommand(new Command[]{new LightOffCommand(livingRoomLight), new LightOffCommand(kitchenLight)});        lightOn.execute();        lightOff.execute();    }}

10. 运行结果

Living Room light is onKitchen Light is onLiving Room Stereo is onLiving Room Stereo set cdLiving Room Stereo set volume--------------------------------Living Room light is offKitchen Light is offLiving Room Stereo is off--------------------------------Living Room Stereo is onLiving Room Stereo set cdLiving Room Stereo set volume--------------------------------Living Room light is onKitchen Light is onLiving Room light is offKitchen Light is off