首页 > 其他 > 详细

设计模式7-命令模式

时间:2019-04-17 16:45:15      阅读:148      评论:0      收藏:0      [点我收藏+]
package DesignPattern;

public class CommondPattern {
    //命令模式的要点:分离发令者,命令,与接令者
    //将接令者对外暴露的接口包装成为一致的命令
    //发令者拥有命令的实例,发令者调用命令实例,命令实例调用接令者接口从而实现命令动作
    //优点:接令者的暴露在外的接口被一致化,增加命令功能,只需要继承Command接口,实现自定义功能,
    // 完成了命令双向的解耦
    //常见的命令模式:工作队列,日志请求
    public static class Light{
        private String name;
        Light(String name){
            this.name=name;
        }
        void on(){
            System.out.println(name+" is on!");
        }
        void off(){
            System.out.println(name+" is off!");
        }
    }
    public interface Command{
        void execute();
        void undo();
    }
    public static class LightOnCommand implements Command{
        Light light;
        LightOnCommand(Light light){
            this.light=light;
        }
        @Override
        public void execute() {
            light.on();
        }

        @Override
        public void undo() {
            light.off();
        }
    }
    public static class LightOffCommand implements Command{
        Light light;
        LightOffCommand(Light light){
            this.light=light;
        }
        @Override
        public void execute() {
            light.off();
        }
        @Override
        public void undo() {
            light.on();
        }
    }
    //甚至可以实现宏命令
    public static class MacroCommand implements Command{
        Command[] commands;
        MacroCommand(Command[] commands){
            this.commands = commands;
        }

        @Override
        public void execute() {
            for(Command command:commands){
                command.execute();
            }
        }

        @Override
        public void undo() {
        }
    }

    public static class SimpleRemoteControl{
        Command slot;
        SimpleRemoteControl(){}

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

        public void buttonWasPressed(){
            slot.execute();
        }
    }
    public static class noCommand implements Command{
        @Override
        public void execute() {}
        @Override
        public void undo() {}
    }
    public static 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;
        }
        /*
        @Param slot 插口
         */
        public void setCommands(int slot,Command onCommand,Command offCommand){
            onCommands[slot]=onCommand;
            offCommands[slot]=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 undoButtonWasPushed(){
            undoCommand.undo();
        }
        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();
        }
    }
    public static void main(String[] args) {
        /*简单命令模式测试代码
        SimpleRemoteControl control = new SimpleRemoteControl();
        control.setCommand(new LightOnCommand(new Light()));
        control.buttonWasPressed();
         */
        RemoteControl remoteControl = new RemoteControl();
        Light livingRoomLight = new Light("living room light");
        Light kitchenLight = new Light("kitchen light");
        LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
        LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
        LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);
        LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);

        remoteControl.setCommands(0,livingRoomLightOn,livingRoomLightOff);
        remoteControl.setCommands(1,kitchenLightOn,kitchenLightOff);

        remoteControl.onButtonWasPushed(0);
        remoteControl.offButtonWasPushed(1);
        remoteControl.undoButtonWasPushed();
        System.out.println();
        //实现宏命令
        Command[] commands1 ={livingRoomLightOff,kitchenLightOn};
        Command[] commands2 ={livingRoomLightOn,kitchenLightOff};

        MacroCommand macroCommand1=new MacroCommand(commands1);
        MacroCommand macroCommand2 = new MacroCommand(commands2);
        remoteControl.setCommands(2,macroCommand1,macroCommand2);
        remoteControl.onButtonWasPushed(2);
    }
}

设计模式7-命令模式

原文:https://www.cnblogs.com/zhouyu0-0/p/10724380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!