• 设计模式学习笔记十五:命令模式


    介绍

    命令模式,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);
        }
    }
    
  • 相关阅读:
    UVa OJ 102 Ecological Bin Packing (生态学装箱问题)
    最小生成树 zoj1586 QS Network
    最小生成树&并查集 POJ 1861 Network
    最小生成树&并查集POJ 1287 Networking
    2012年亚洲长春区域赛:K Yukari's Birthday
    最小生成树 ZOJ 1203 Swordfish
    HDU 2639 Bone Collector II
    POJ2528 Mayor's posters(区间替换&&线段切割)
    POJ2777 Count Color(区间修改&&懒惰标记&&位运算)
    (转)有关如何入门ACM
  • 原文地址:https://www.cnblogs.com/liushijie/p/4759032.html
Copyright © 2020-2023  润新知