标签搜索

命令模式

Pop.Kite
2022-02-06 / 0 评论 / 122 阅读 / 正在检测是否收录...

将动作封装成对象,执行动作的时候传递对象即可无需关注其内部实现。从而实现成动作请求和执行方的解耦。

如使用遥控器控制家电,传递Command对象,具体的实现由Command完成。

//定义设备接口
public interface BaseDevice {
    void open();

    void off();
}

//定义一些设备

/**
* ComputerDevice
*/
public class ComputerDevice implements BaseDevice {
    private boolean status = false;

    @Override
    public void open() {
        if (status) {
            System.out.println("电脑已经打开了");
        } else {
            status = true;
            System.out.println("打开电脑");
        }
    }

    @Override
    public void off() {
        if (!status) {
            System.out.println("电脑已经关闭了");
        } else {
            status = false;
            System.out.println("关闭电脑");
        }
    }
}

/**
* LightDevice
*/
public class LightDevice implements BaseDevice{
    private boolean status = false;

    @Override
    public void open() {
        if (status) {
            System.out.println("灯已经打开了");
        } else {
            status = true;
            System.out.println("打开电灯");
        }
    }

    @Override
    public void off() {
        if (!status) {
            System.out.println("灯已经关闭了");
        } else {
            status = false;
            System.out.println("关闭电灯");
        }
    }
}

//略去一些类
//定义命令Base
public abstract class BaseCommand {
    public BaseCommand() {

    }

    public abstract void execute(boolean operation);
}

//定义一些命令类

/**
* ComputerCommand
*/
public class ComputerCommand extends BaseCommand {

    private ComputerDevice device;

    public ComputerCommand(ComputerDevice device) {
        super();
        this.device = device;
    }

    @Override
    public void execute(boolean operation) {
        if (operation) {
            device.open();
        } else {
            device.off();
        }
    }
}

/**
* ComputerCommand
*/
public class LightCommand extends BaseCommand {

    private LightDevice device;

    public LightCommand(LightDevice device) {
        super();
        this.device = device;
    }

    @Override
    public void execute(boolean operation) {
        if (operation) {
            device.open();
        } else {
            device.off();
        }
    }
}

/**
* 可以执行一组命令
*/
public class QucikCommand extends BaseCommand {

    private BaseCommand[] commands;

    public QucikCommand(BaseCommand[] commands) {
        this.commands = commands;
    }

    @Override
    public void execute(boolean operation) {
        for (BaseCommand command : commands) {
            command.execute(operation);
        }
    }
}

//略去一些类
//定义Controller
public class Controller {
    private static final int Button_Size = 5;
    private BaseCommand[] commands;

    public Controller() {
        this.commands = new BaseCommand[Button_Size];
    }

    public Controller setCommand(int index, BaseCommand command) {
        commands[index] = command;
        return this;
    }

    public void press(int index, boolean operation) {
        commands[index].execute(operation);
    }
}
//执行
    public static void CommandExample() {
        Controller controller = new Controller();
        ComputerDevice computerDevice = new ComputerDevice();
        TelevisionDevice televisionDevice = new TelevisionDevice();
        LightDevice lightDevice = new LightDevice();

        ComputerCommand computerCommand = new ComputerCommand(computerDevice);
        TelevisionCommand televisionCommand = new TelevisionCommand(televisionDevice);
        LightCommand lightCommand = new LightCommand(lightDevice);
        EmptyCommand emptyCommand = new EmptyCommand();

        QucikCommand qucikCommand = new QucikCommand(new BaseCommand[]{
                computerCommand, televisionCommand, lightCommand
        });

        controller.setCommand(0, computerCommand)
                .setCommand(1, televisionCommand)
                .setCommand(2, lightCommand)
                .setCommand(3, emptyCommand)
                .setCommand(4, qucikCommand);

        controller.press(1,true);
        controller.press(1,false);
        controller.press(1,false);

        controller.press(2,true);
        controller.press(4,true);
    }
#输出
打开电视
关闭电视
电视已经关闭了
打开电灯
打开电脑
打开电视
灯已经打开了
0

评论 (0)

取消