zoukankan      html  css  js  c++  java
  • 设计模式——命令模式(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();
    }
    }











  • 相关阅读:
    ZROI2018提高day9t1
    p2114 起床困难综合症
    EZOJ #78
    Linux JDK配置
    jps命令
    虚拟机 网卡模式配置
    redhat下yum命令安装(替换为centos yum命令)
    URL编码
    Query DSL(2)----Full text queries
    Query DSL(1)
  • 原文地址:https://www.cnblogs.com/tanlon/p/2189843.html
Copyright © 2011-2022 走看看