Java设计模式 - 命令模式

目录

命令模式:

命令优点:

命令模式应用:

命令实例:

上述代码 GitHub 地址:https://github.com/baicun/designPatterns


命令模式:

将请求、命令、动作等封装成对象,这样可以让项目使用这些对象来参数化其他对象、使得命令的请求和执行者解耦。

命令优点:

操作的请求和操作的执行灵活的分开,降低了系统耦合度,同时拓展性好

记录操作记录

命令模式应用:

智能家庭电器开关设置

命令实例:

类图:

示例描述 :想象手里握着全家电器的遥控器,每一次按钮都是有效操作,不同的电器有类似的功能(开、关),也有特殊的功能。所有我们把类似的功能进行抽象,即接口Command,所有的电器实现这个接口,并定制个性的操作。再说遥控器,抽象出操作接口即Control,实现这个接口,处理不同的按钮处理。

命令接口:

public interface Command {
    // 执行
    public void execute();
    // 回退
    public void undo();
}

拓展电器功能实现-LightOffCommand.java:

public class LightOffCommand implements Command{
    private Light light;
    public LightOffCommand(Light light)
    {
        this.light=light;
    }
    @Override
    public void execute() {
        light.Off();
    }

    @Override
    public void undo() {
        light.On();
    }
}

拓展电器功能实现-LightOnCommand.java:

public class LightOffCommand implements Command{
    private Light light;
    public LightOffCommand(Light light)
    {
        this.light=light;
    }
    @Override
    public void execute() {
        light.Off();
    }

    @Override
    public void undo() {
        light.On();
    }
}

控制器接口:

public interface Control {
    public void onButton(int slot);

    public void offButton(int slot);

    public void undoButton();
}

控制器实现-CommandModeControl.java

public class CommandModeControl implements Control {
    private Command[] onCommands;
    private Command[] offCommands;
    // 栈对象存储执行的命令
    private Stack<Command> stack=new Stack<Command>();

    public CommandModeControl()
    {
        onCommands=new Command[5];
        offCommands=new Command[5];

        Command noCommand=new NoCommand();

        for(int i=0,len=onCommands.length;i<len;i++)
        {
            onCommands[i]=noCommand;
            offCommands[i]=noCommand;
        }

    }

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

    @Override
    public void onButton(int slot) {
        onCommands[slot].execute();
        stack.push(onCommands[slot]);
    }

    @Override
    public void offButton(int slot) {
        offCommands[slot].execute();
        stack.push(offCommands[slot]);
    }

    @Override
    public void undoButton() {
        stack.pop().undo();
    }
}

 测试类:

public class MainTest {

    public static  void main(String[] args){
        CommandModeControl commandModeControl = new CommandModeControl();
        Light bedlight = new Light("bedRoom");
        Light kitchlight = new Light("Kitch");

        LightOnCommand bedlightOnCommand = new LightOnCommand(bedlight);
        LightOffCommand bedlightOffCommand = new LightOffCommand(bedlight);
        LightOnCommand kitchlightOnCommand = new LightOnCommand(kitchlight);
        LightOffCommand kitchlightOffCommand = new LightOffCommand(kitchlight);

        commandModeControl.setCommand(1,bedlightOnCommand, bedlightOffCommand);
        commandModeControl.setCommand(2,kitchlightOnCommand, kitchlightOffCommand);

        commandModeControl.onButton(1);
        commandModeControl.offButton(1);
        commandModeControl.undoButton();//回退所有操作命令
        commandModeControl.onButton(2);
        commandModeControl.offButton(2);
        commandModeControl.undoButton();//回退所有操作命令

        // 一次性操作所有电器开关
        /*MarcoCommand marcommandON, marcommandOFF;
        Command[] commandON = {bedlightOnCommand,kitchlightOnCommand};
        Command[] commandOFF = {bedlightOffCommand,kitchlightOffCommand};
        marcommandON = new MarcoCommand(commandON);
        marcommandOFF =new MarcoCommand(commandOFF);

        marcommandON.execute();
        marcommandOFF.execute();


        //回退所有操作命令
        marcommandOFF.undo();*/
    }
}

项目demo中,还拓展了宏命令,就是批量批处理实现类。

上述代码 GitHub 地址:https://github.com/baicun/designPatterns

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值