• 设计模式——命令模式(Command)


    最近做项目用到了命令模式,自己却浑然不知。项目中是这样的,同一个按钮,在不同的界面中点击的时候要实现不同的方法。于是只需要在不同的界面传递不同的命令就行了。后来仔细去看了下命令模式,然后记录下来了。

    命令模式(Command),将一个请求封装成一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。

     

    用遥控器来控制空调的开和关、以及温度的设置。

    空调的实体类(包含空调的开、关和温度的设置)

     

    package com.tanlon.command;
    /**
    * 定义空调,用于测试给遥控器添新控制类型
    *
    @author Administrator
    *
    */
    public class AirCondition {
    /**
    * 定义实现开空调的方法
    */
    public void start(){
    System.out.println("The AirCondition is turned on.");
    }
    /**
    * 定义实现设置温度的方法
    *
    *
    @param i 温度的度数
    */
    public void setTemperature(int i){
    System.out.println("The temperature is set to " + i);
    }
    /**
    * 定义实现关空调的方法
    */
    public void stop(){
    System.out.println("The AirCondition is turned off.");
    }
    }
    定义执行命令的接口
    package com.tanlon.command;
    /**
    * 定义Command接口
    *
    *
    @author Administrator
    *
    */
    public interface ICommand {
    void Execute();
    }


    定义关空调命令,实现执行命令的接口

     

    package com.tanlon.command;
    /**
    * 定义关空调命令
    *
    @author Administrator
    *
    */
    public class AirOffCommand implements ICommand{
    //空调实例
    AirCondition airCondition;

    public AirOffCommand(AirCondition airCondition) {
    this.airCondition=airCondition;
    }
    /**
    * 执行命令
    */
    public void Execute() {
    // TODO Auto-generated method stub
    airCondition.stop();
    }

    }

    定义开空调命令 实现执行命令的接口

    package com.tanlon.command;
    /**
    * 定义开空调命令
    *
    @author Administrator
    *
    */
    public class AirOnCommand implements ICommand{
    //空调实例
    AirCondition airCondition;

    public AirOnCommand(AirCondition airCondition) {
    this.airCondition=airCondition;
    }
    /**
    * 执行命令的方法
    */
    public void Execute() {
    // TODO Auto-generated method stub
    airCondition.start();//执行启动空调
    airCondition.setTemperature(24);//设置默认温度为16度
    }

    }

    定义遥控器

    package com.tanlon.command;
    /**
    * 定义遥控器
    *
    *
    @author Administrator
    *
    */
    public class ControlPanel {
    //开命令接口引用
    private ICommand onICommand;
    //关命令接口引用
    private ICommand offICommand;
    //定义执行开的命令,并执行开的命令方法
    public void pressOn(){
    onICommand.Execute();
    }
    //定义执行关的命令,并执行开的命令方法
    public void pressStop(){
    offICommand.Execute();
    }
    //实例化引用
    public void setICommand(ICommand onICommand,ICommand offICommand){
    this.onICommand=onICommand;
    this.offICommand=offICommand;
    }
    }

    程序运行

    package com.tanlon.command;

    public class Program {
    public static void main(String[] args) {
    // 创建遥控器对象
    ControlPanel panel=new ControlPanel();
    //创建空调对象
    AirCondition airCondition=new AirCondition();
    // 创建Command对象,传递空调对象
    ICommand onICommand=new AirOnCommand(airCondition);
    ICommand offICommand=new AirOffCommand(airCondition);
    // 设置遥控器的Command
    panel.setICommand(onICommand, offICommand);
    //按下On按钮,开空调,温度调到16度
    panel.pressOn();
    //按下Off按钮,关空调
    panel.pressStop();
    }
    }











  • 相关阅读:
    Flask把变量注册到模板中
    $.each与$(data).each区别
    【Python备忘】python判断文件和文件夹是否存在
    ISP图像质量自动化测试方法
    使用微软的(how-old.net)构建智能门店管理系统
    在virtualenv中安装libxml2和libxslt
    Python 装饰器学习以及实际使用场景实践
    tensorflow零起点快速入门(4) --入门常用API
    tensorflow零起点快速入门(3)
    树莓派和STM32通过USB和串口通信记录
  • 原文地址:https://www.cnblogs.com/tanlon/p/2189843.html
Copyright © 2020-2023  润新知