每天一个设计模式之command

想象一下,如果想在dos中列出当前目录下的所有文件,我们会输入dir并回车。为了达到这个目的,我们必须知道“dir”这个命令,以及一些dir常用的参数。

因此我们(client)就和命令的执行者耦合在一起了。


command模式其实和其他的常见模式一样,在client和对象间,加入一个中间层,从而达到解耦合的目的。command模式的另外一个作用是可以记录执行的command的历史,从而实现事务,多线程,撤销等应用。

GoF《设计模式》中说道:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。


在这个模式中有这几个部分:

1,client

2,invoker 这个对象决定什么时候执行command。同时存储了通过它执行的所有command,从而实现上面说的事务,撤销等操作。

client直接调用的是这个对象,且传入的参数是下面的command对象。

3,command 这个对象将receiver包装了一下。包装的时候可以自由的修改接口,增减命令的参数等。

4,receiver 这个对象包含了真正执行的命令的代码。


从下面的示例中可以很清楚的看到这几部分。

这个示例是关于开关灯的命令的,非常简单。


/*the Command interface*/
public interface Command {
   void execute();
}
 
 
 
import java.util.List;
import java.util.ArrayList;
 
/*the Invoker class*/
public class Switch {
   private List<Command> history = new ArrayList<Command>();
 
   public Switch() {
   }
 
   public void storeAndExecute(Command cmd) {
      this.history.add(cmd); // optional 
      cmd.execute();        
   }
}
 
 
/*the Receiver class*/
public class Light {
   public Light() {
   }
 
   public void turnOn() {
      System.out.println("The light is on");
   }
 
   public void turnOff() {
      System.out.println("The light is off");
   }
}
 
 
/*the Command for turning on the light - ConcreteCommand #1*/
public class FlipUpCommand implements Command {
   private Light theLight;
 
   public FlipUpCommand(Light light) {
      this.theLight = light;
   }
 
   public void execute(){
      theLight.turnOn();
   }
}
 
 
/*the Command for turning off the light - ConcreteCommand #2*/
public class FlipDownCommand implements Command {
   private Light theLight;
 
   public FlipDownCommand(Light light) {
      this.theLight = light;
   }
 
   public void execute() {
      theLight.turnOff();
   }
}
 
 
/*The test class or client*/
public class PressSwitch {
   public static void main(String[] args){
      Light lamp = new Light();
      Command switchUp = new FlipUpCommand(lamp);
      Command switchDown = new FlipDownCommand(lamp);
 
      Switch s = new Switch();
 
      try {
         if (args[0].equalsIgnoreCase("ON")) {
            s.storeAndExecute(switchUp);
            System.exit(0);
         }
         if (args[0].equalsIgnoreCase("OFF")) {
            s.storeAndExecute(switchDown);
            System.exit(0);
         }
         System.out.println("Argument \"ON\" or \"OFF\" is required.");
      } catch (Exception e) {
         System.out.println("Arguments required.");
      }
   }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值