zoukankan      html  css  js  c++  java
  • 命令模式

    模式介绍

    命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。

    模式优点

    1、降低了请求者与行为实现者的系统耦合度。
    2、新的命令可以很容易添加到系统中去。

    模式缺点

    1、使用命令模式可能会导致某些系统有过多的具体命令类。

    使用场景

    1、认为是命令的地方都可以使用命令模式,比如:GUI 中每一个按钮都是一条命令。
    2、系统需要支持命令的执行(Execute)操作和恢复(Undo)操作,也可以考虑使用命令模式。

    系统建模

    1、智能遥控器项目,通过遥控器可以打开或关闭家电,比如:灯泡。

    系统实现

    /**
     * 电灯家电
     */
    public class LightReceiver {
        public void on(){
            System.out.println("电灯打开了!");
        }
    
        public void off(){
            System.out.println("电灯关闭了!");
        }
    }
    
    /**
     * 命令接口
     */
    public interface Command {
        public void execute();
        public void undo();
    }
    
    /**
     * 空命令对象
     */
    public class NoCommand implements Command {
        @Override
        public void execute() {
    
        }
    
        @Override
        public void undo() {
    
        }
    }
    
    /**
     * 电灯打开命令
     */
    public class LightOnCommand implements Command {
        private LightReceiver lighter;
    
        public LightOnCommand(LightReceiver lighter){
            this.lighter = lighter;
        }
    
        @Override
        public void execute() {
            lighter.on();
        }
    
        @Override
        public void undo() {
            lighter.off();
        }
    }
    
    /**
     * 电灯关闭命令
     */
    public class LightOffCommand implements Command{
        private LightReceiver lighter;
    
        public LightOffCommand(LightReceiver lighter){
            this.lighter = lighter;
        }
        @Override
        public void execute() {
            lighter.off();
        }
    
        @Override
        public void undo() {
            lighter.on();
        }
    }
    
    /**
     * 智能遥控器
     */
    public class RemoteControllar {
        private Command[] onCommand;
        private Command[] offCommand;
        private Command undoCommand;
    
        // 遥控器只能操作5个家电
        public RemoteControllar(){
            onCommand = new Command[5];
            offCommand = new Command[5];
            for(int i=0 ; i<5; i++){
                onCommand[i] = new NoCommand();
                offCommand[i] = new NoCommand();
            }
        }
    
        // 设定命令
        public void setCommand(int NO, Command onCommand, Command offCommand){
            this.onCommand[NO] = onCommand;
            this.offCommand[NO] = offCommand;
        }
    
        // 打开命令
        public void onButtonWasPushed(int NO){
            onCommand[NO].execute();
            undoCommand = onCommand[NO];
        }
    
        // 关闭命令
        public void offButtonWasPushed(int NO){
            offCommand[NO].execute();
            undoCommand = offCommand[NO];
        }
    
        //撤消命令
        public void undoButtonWasPushed(){
            undoCommand.execute();
        }
    }
    
    /**
     * 客户端
     */
    public class Client {
        public static void main(String args[]){
            LightReceiver ligher = new LightReceiver();
            LightOnCommand lightOnCommand = new LightOnCommand(ligher);
            LightOffCommand lightOffCommand = new LightOffCommand(ligher);
            RemoteControllar remoteControllar = new RemoteControllar();
            remoteControllar.setCommand(0, lightOnCommand, lightOffCommand);
            remoteControllar.onButtonWasPushed(0);
            remoteControllar.offButtonWasPushed(0);
            remoteControllar.undoButtonWasPushed();
        }
    }
    结果:
    电灯打开了!
    电灯关闭了!
    电灯打开了!
    
  • 相关阅读:
    lumen简单使用exel组件
    VIM 批量注释的两种方法 (转)
    linux时间校准 设置时间为上海时区
    lumen发送邮件配置
    centos 下安装redis 通过shell脚本
    shell 脚本接收参数
    linux设置系统变量
    linux通配符
    UCCI协议[转]
    一种编程范式:对拍编程
  • 原文地址:https://www.cnblogs.com/feiqiangsheng/p/12229391.html
Copyright © 2011-2022 走看看