亲宝软件园·资讯

展开

Java命令模式

爱学习的大鱼 人气:0

模式介绍

UML类图

类图解析:

命令模式案例

案例解析:智能家居,通过一个遥控器控制家里的智能设备

Command接口类

public interface Command {

    /**
     * 执行操作
     */
    void execute();

    /**
     * 撤销操作
     */
    void undo();
}

LightReceiver、CurtainReceiver信号接收者

public class LightReceiver {

    public void on() {
        System.out.println("开灯...");
    }
    public void off() {
        System.out.println("关灯....");
    }
}
public class CurtainReceiver {
    public void on() {
        System.out.println("打开窗帘...");
    }

    public void off() {
        System.out.println("关闭窗帘...");
    }
}

Command具体实现子类CurtainOnCommand、CurtainOffCommand、LightOnCommand、LightOffCommand、NoCommand

public class CurtainOnCommand implements Command {
    private CurtainReceiver curtain;

    public CurtainOnCommand(CurtainReceiver curtainReceiver) {
        this.curtain = curtainReceiver;
    }

    @Override
    public void execute() {
        curtain.on();
    }

    @Override
    public void undo() {
        curtain.off();
    }
}
public class CurtainOffCommand implements Command {
    private CurtainReceiver curtainReceiver;

    public CurtainOffCommand(CurtainReceiver curtainReceiver) {
        this.curtainReceiver = curtainReceiver;
    }

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

    @Override
    public void undo() {
        curtainReceiver.on();
    }
}
public class LightOnCommand implements Command {
    // 聚合LightReceiver
    private LightReceiver light;

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

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

    @Override
    public void undo() {
        light.off();
    }
}
public class LightOffCommand implements Command {
    // 聚合LightReceiver
    private LightReceiver light;

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

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

    @Override
    public void undo() {
        light.on();
    }
}
/**
 * 用于初始化每个按钮
 */
public class NoCommand implements Command {
    @Override
    public void execute() {
        System.out.println("do nothing...");
    }

    @Override
    public void undo() {
        System.out.println("do nothing...");
    }
}

RemoteController调用者

public class RemoteController {
    // 开命令数组
    private Command[] onCommands;
    // 关命令数组
    private Command[] offCommands;

    // 撤销命令位置
    private int no;
    // 撤销命令是否为按下命令
    private Boolean isOn;

    /**
     * 初始化
     */
    public RemoteController() {
        this.onCommands = new Command[5];
        this.offCommands = new Command[5];

        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }


    /**
     * 设置一行中的按钮
     * @param no
     * @param onCommand
     * @param offCommand
     */
    public void setCommand(int no,Command onCommand,Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    /**
     * 开按钮按下
     * @param no
     */
    public void onButtonPushed(int no){
        // 调用按钮方法
        onCommands[no].execute();
        // 记录撤销按钮
        this.no = no;
        isOn = true;
    }

    /**
     * 关按钮按下
     * @param no
     */
    public void offButtonPushed(int no) {
        // 调用按钮方法
        offCommands[no].execute();
        // 记录撤销按钮
        this.no = no;
        isOn = false;
    }

    public void undo() {
        if (isOn) {
            onCommands[no].undo();
        } else {
            offCommands[no].undo();
        }
        isOn = !isOn;
    }
}

Client测试类

public class Client {
    public static void main(String[] args) {
        // 接收者
        LightReceiver lightReceiver = new LightReceiver();
        CurtainReceiver curtainReceiver = new CurtainReceiver();

        // 命令
        Command lightOnCommand = new LightOnCommand(lightReceiver);
        Command lightOffCommand = new LightOffCommand(lightReceiver);
        Command curtainOnCommand = new CurtainOnCommand(curtainReceiver);
        Command curtainOffCommand = new CurtainOffCommand(curtainReceiver);
        // 执行者
        RemoteController remoteController = new RemoteController();
        remoteController.setCommand(0,lightOnCommand,lightOffCommand);
        remoteController.setCommand(1,curtainOnCommand,curtainOffCommand);
        System.out.print("开灯按钮\t");
        remoteController.onButtonPushed(0);
        System.out.print("撤销按钮\t");
        remoteController.undo();
        System.out.print("撤销按钮\t");
        remoteController.undo();
        System.out.print("关灯按钮\t");
        remoteController.offButtonPushed(0);

        System.out.println("------------------");
        System.out.print("开窗帘按钮\t");
        remoteController.onButtonPushed(1);
        System.out.print("撤销按钮 \t");
        remoteController.undo();
        System.out.print("撤销按钮 \t");
        remoteController.undo();
        System.out.print("关窗帘按钮\t");
        remoteController.offButtonPushed(1);
    }
}

测试结果:

个人优化方案(仅供参考):

命令模式的注意事项和细节

加载全部内容

相关教程
猜你喜欢
用户评论