关于命令模式个人的理解 撤销 重做的实现

  这是在实际项目中遇到的需求,项目中使用了Java Swing画界面,开始时没有实现撤销重做,后期要求加入撤销重做功能。在网上查找到资料说这种撤销重做的操作一般通过Command模式来实现,在实现过程中参考了http://blog.csdn.net/turbochen/article/details/8087文章中的内容。

        命令模式把一个请求或者操作封装到一个对象中,把发出命令的责任和执行命令的责任分割开,委派给不同的对象,可降低行为请求者与行为实现者之间耦合度。从使用角度来看就是请求者把接口实现类作为参数传给使用者,使用者直接调用这个接口的方法,而不用关心具体执行的那个命令。本文重点不是讲解命令模式,本人研究不多,里面各个角色也没分清楚,不多介绍,可参考http://www.uml.org.cn/sjms/200904151.asp

        使用命令模式当然要定义一个命令接口:

 

[java] view plain copy
 
  1. public interface Command {    
  2.     public void execute(); // 执行命令和重做    
  3.     public void undo();  // 执行撤销操作    
  4. }   

         下一步是对于各个操作实现Command接口,对于不同操作实现方式也千差万别,有的操作可能只需要记录一个状态或记录修改的文字,有的可能要记录当前所有数据,有的execute方法里没有分支,首次执行命令和redo操作相同,有的则不同,需要分支判断。

        现在说一下实现多次撤销重做的原理:维护undo和redo两个盛放Command的栈(用List实现),首次执行一个Command时,执行execute()并将其放入undo栈内,同时要清空redo栈;当执行撤销操作时把undo栈内最上面一个Command拿出来执行undo(),然后将其放入redo栈内;执行重做操作时把redo栈内最上面一个Command拿出来执行execute(),然后将其放入undo栈内。

 

[java] view plain copy
 
  1. public class CommandManager {  
  2.   
  3.     private List undoList = new ArrayList();  
  4.     private List redoList = new ArrayList();  
  5.       
  6.     // 可撤销的步数,-1时无限步  
  7.     private int undoCount = -1;  
  8.       
  9.     public CommandManager() {  
  10.           
  11.         // 可通过配置文件配置撤销步数  
  12.         undoCount = 5;  
  13.     }  
  14.   
  15.     /** 
  16.      * 执行新操作 
  17.      */  
  18.     public void executeCommand(Command cmd) {  
  19.           
  20.         // 执行操作  
  21.         cmd.execute();  
  22.           
  23.         undoList.add(cmd);  
  24.           
  25.         // 保留最近undoCount次操作,删除最早操作  
  26.         if (undoCount != -1 && undoList.size() > undoCount) {  
  27.             undoList.remove(0);  
  28.         }  
  29.           
  30.         // 执行新操作后清空redoList,因为这些操作不能恢复了  
  31.         redoList.clear();  
  32.     }  
  33.       
  34.     /** 
  35.      * 执行撤销操作 
  36.      */  
  37.     public void undo() {  
  38.         if (undoList.size() <= 0) {  
  39.             return;  
  40.         }  
  41.           
  42.         Command cmd = ((Command)(undoList.get(undoList.size() - 1)));  
  43.         cmd.undo();  
  44.           
  45.         undoList.remove(cmd);  
  46.         redoList.add(cmd);  
  47.     }  
  48.   
  49.     /** 
  50.      * 执行重做 
  51.      */  
  52.     public void redo() {  
  53.         if (redoList.size() <= 0) {  
  54.             return;  
  55.         }  
  56.           
  57.         Command cmd = ((Command)(redoList.get(redoList.size() - 1)));  
  58.         cmd.execute();  
  59.           
  60.         redoList.remove(cmd);  
  61.         undoList.add(cmd);  
  62.     }  
  63. }  

        当点击按钮执行操作时,可在其ActionListener内执行commandManager.executeCommand(newXXXCommand());把命令加入栈中。由于每执行一个命令时都会生成一个新的Command对象放到栈中,如果Command太多、Command记录的临时数据量大时,可能会占用大量内存,所以上面代码中有一个限制撤销步数的参数undoCount。

posted @ 2017-09-26 14:26  郝二驴  阅读(1289)  评论(0编辑  收藏  举报