当前位置:编程学习 > asp >>

设计模式读书笔记-命令模式

命令模式,将请求封装成对象,这可以让你使用不同的请求\队列\或日志请求来参数化对象,命令模式也可以支持撤销操作. 当发出请求和执行请求的对象需要解耦时,使用命令对象.
我们来看一个例子:遥控器例子,我们想通过遥控器控制家中的各种电器;实现这样的功
能要求:.
  1 各种电器提供控制命令接口;
  2 遥控器对象
  3 遥控器上各个插槽对应的命令,命令对象,
  4 各种电器
定义命令接口:
public interface Command {
       public void execute();
}
各种电器对象,我们将各种电器对象单独定义并将对象命令封装进来.也就是各种电器对象的命令方法;
public class Light {
       public Light() {
       }
       public void on() {
              System.out.println("Light is on");
       }
       public void off() {
              System.out.println("Light is off");
       }
}
定义各种电器各种命令的对象实现COMMAND的接口;对应遥控器的各个操作;
public class LightOnCommand implements Command {
       Light light;
      public LightOnCommand(Light light) {
              this.light = light;
       }
      public void execute() {
              light.on();
       }
}
public class LightOffCommand implements Command {
       Light light;
      public LightOffCommand(Light light) {
              this.light = light;
       }
      public void execute() {
              light.off();
       }
}
定义遥控器对象;
public class SimpleRemoteControl {
       Command slot;
      public SimpleRemoteControl() {}
      public void setCommand(Command command) {
              slot = command;
       }
      public void buttonWasPressed() {
              slot.execute();
       }
}
下面实现一个简单的测试类:
public class RemoteControlTest {
       public static void main(String[] args) {
              SimpleRemoteControl remote = new SimpleRemoteControl();
              Light light = new Light();
              LightOnCommand lightOn = new LightOnCommand(light);
              remote.setCommand(lightOn);
              remote.buttonWasPressed();
        remote.setCommand(lightOff);
              remote.buttonWasPressed();
 
    }
}
遥控器不需要命令到底是如何执行的,只需要在调用的时候调用就可以;命令都是动态传入的。实现了请求者和执行者的完全解耦,通过命令接口把两部分连接起来。
 
空对象应用:
public class NoCommand implements Command {
       public void execute() { }
}
在初始化命令时,比如遥控器中可能某个插槽无对应命令,我们可以为空,返回空对象;这个对象什么都不做,这种方式被常用,也可以作为设计模式的一种。
 
我们重新实现遥控器(有些电器对象代码这里没给出,可以去51CTO下载。
 
public class RemoteControl {
       Command[] onCommands;
       Command[] offCommands;
 
       public RemoteControl() {
              onCommands = new Command[7];
              offCommands = new Command[7];
 
              Command noCommand = new NoCommand();
              for (int i = 0; i < 7; i++) {
                     onCommands[i] = noCommand;
                     offCommands[i] = noCommand;
              }
       }
 
       public void setCommand(int slot, Command onCommand, Command offCommand) {
              onCommands[slot] = onCommand;
              offCommands[slot] = offCommand;
       }
 
       public void onButtonWasPushed(int slot) {
              onCommands[slot].execute();
       }
 
       public void offButtonWasPushed(int slot) {
              offCommands[slot].execute();
       }
 
       public String toString() {
              StringBuffer stringBuff = new StringBuffer();
              stringBuff.append("\n------ Remote Control -------\n");
              for (int i = 0; i < onCommands.length; i++) {
                     stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
                            + "    " + offCommands[i].getClass().getName() + "\n");
              }
              return stringBuff.toString();补充:Web开发 , ASP.Net ,

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,