zoukankan      html  css  js  c++  java
  • 设计模式学习笔记十五:命令模式

    介绍

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

    UML结构图:

    Command

    场景模拟

    本例是模拟实现一个遥控器。

    代码实现

    1.先模拟两个真正干活的对象,即invoker:

    public class Light{
        public void on() {
            System.out.println("light is on");
        }
        public void off() {
            System.out.println("light is off");
        }
    }
    
    public class TV {
        public void on() {
            System.out.println("TV is on");
        }
        public void off() {
            System.out.println("TV is off");
        }
    }
    
    

    2.定义统一命令调用接口,即Command(刚开始用Runnable来着,偷懒:]):

    public interface Command {
        public void execute();
    }
    

    3.定义开关命令,统一实现Command接口,即ConcreteCommand:

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

    4.定义操作控制对象,即Receiver:

    public class RemoteControl {
        Command[] onCommands;
        Command[] offCommands;
        public RemoteControl() {
            onCommands = new Command[2];
            offCommands = new Command[2];
        }
        public void setOnCommand(int slot, Command onCommand, Command offCommand) {
            onCommands[slot] = onCommand;
            offCommands[slot] = offCommand;
        }
        public void onButtonWasPushed(int slot) {
            onCommands[slot].execute();
        }
        public void offButtonWasPushed(int slot) {
            offCommands[slot].execute();
        }
    
    }
    
    

    5.调用,即Client:

    public class RemoteLoader {
        public static void main(String[] args) {
            RemoteControl control = new RemoteControl();
            Light light = new Light();
            TV tv = new TV();
    
            // 加载命令
            control.setOnCommand(0, new LightOnCommand(light), new LightOffCommand(light));
            control.setOnCommand(1, new TVOnCommand(tv), new TVOffCommand(tv));
    
            // 执行命令
            control.onButtonWasPushed(0);
            control.offButtonWasPushed(0);
    
            System.out.println();
    
            control.onButtonWasPushed(1);
            control.offButtonWasPushed(1);
        }
    }
    
  • 相关阅读:
    创建文本编辑输入框1:
    表likp新增第一次过账输入日期字段,vl02n/vl01n/vl03n/vl06o的增强
    如何获得控件的属性
    使用OVS
    【Vue】安装(NPM 方法)
    【Webpack】学习随笔
    【node】安装
    【CSS-flex】圣杯布局(Holy Grail Layout)、输入框的布局、悬挂式布局、固定的底栏
    【CSS】关于flex
    解决MySQL新建用户后无法登录问题
  • 原文地址:https://www.cnblogs.com/liushijie/p/4759032.html
Copyright © 2011-2022 走看看