命令模式

  • 意图

    把方法调用封装起来或将一个请求封装成一个对象从而实现“对象请求者”和“对象执行者”的解耦。对请求进行排队、记录请求日志,以及支持可撤销的操作。

  • 别名

动作(Action),事物(Transaction)

  • 定义命令模式的类图

Alt text
  • 简单例子

//请求接口
public interface Command { 
      public void execute();
      public void undo();
}
//具体请求
 public class LightOnCommand implements Command {
       Light light;  
       public LightOnCommand(Light light) {
           this.light = light;
       }
       public void execute() {
          light.on();
      }
      public void undo() {
          light.off();
      }
 }
//使用命令对象
 public class SimpleRemoteControl {
       Command slot;  
       public SimpleRemoteControl() {}
       public void setCommand(Command command) {
          slot = command
       }
       public void buttonWasPressed() {
          slot.execute();
       }
 }
//简单测试
 public class SimpleRemoteControlTest {
      public static void main(String[] args) {
          SimpleRemoteControl remote = new SimpleRemoteControl();
          Light light = new Light();
          LightOnCommand lightOn = new LightOnCommand(light);
          
          remote.setCommand(lightOn);
          remote.buttonWasPressed();
      }
 }

这样我们就将方法调用给封装了起来,实现了对象发起者与对象执行者的分离以及一个简单的撤销,当需要新的命令时,我们只需要实现一个Command接口。修改一下客户端,而不需要改动原有的代码。

//简单测试,添加了新的Command
 public class SimpleRemoteControlTest {
      public static void main(String[] args) {
          SimpleRemoteControl remote = new SimpleRemoteControl();

          Light light = new Light();
          LightOnCommand lightOn = new LightOnCommand(light);          
          remote.setCommand(lightOn);
          remote.buttonWasPressed();

          Stereo stereo = new Stereo();
          StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereoOnWithCD);
          remote.setCommand()
          remote.buttonWasPressed();
      }
 }

要实现意图中所说的对请求进行排队则只需要在客户端中修改一下Command的出场顺序。当然我们也可以在RemoteControl中维护一个Command数组,通过传入数组下标顺序来调整排队顺序,未传入的则是一个空对象(noCommand)。

//当你不想返回一个有意义的对象时可以考虑空对象,空对象本身也被视为一种设计模式
 public class NoCommand implements Command {
        public void execute(){};
        public void undo() {};
}
  • 实现undo和redo

实现简单的undo可以使用一个Command对象保存住上一次执行方法的Command,客户端发起undo请求时调用保存的Command对象的undo方法时,redo同理。<br />
要实现一个多重的undo、redo则可以使用一个List实现一个栈,客户端发起undo请求时,出栈一个Command对象执行其undo方法。redo也是一样。

  • 命令模式的用途

1.队列请求

在队列的某一端添加命令,另一端则是线程-----从队列中取出一个命令,调用它的execute()方法,调用完成后丢弃执行下一个。工作队列和进行计算的对象之间是完全解耦的,只要实现了Command接口就可以放入到队列中,只要线程可用,就可以调用此对象的execute()方法。

2.日志请求、事务

当执行命令的时候将历史记录存储到磁盘中,一旦系统死机、强制重启我们就可以将命令对象重新加载,并成批的依次调用这些对象的execute()方法,使系统恢复到之前的一个状态。事务也是如此,这也是为什么命令模式也叫作事务模式。

  • 相关模式

Composite模式:可被用来实现宏命令
Memento模式:可用来保持某个状态,命令用这一状态来取消它的效果。

  • 真实的命令模式

java.lang.Runnable
javax.swing.Action

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容