想象一下,如果想在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."); } } }