• 软件设计命令模式


    某系统需要提供一个命令集合(注:可以使用链表,栈等集合对象实现),用于存储一系列命令对象,并通过该命令集合实现多次undo()redo()操作,可以使用加法运算来模拟实现。

    package 实验16命令模式;
    
     public class Adder {
         private int num =0;
         public int add(int value) {
             num+=value;
             return num;
         }
        
     }
    Adder
    package 实验16命令模式;
    
    public abstract class AbstractCommand {
         public abstract int execute(int value);
         
         public abstract int undo();
         
         public abstract int redo();
     }
    AbstractCommand
    package 实验16命令模式;
    
     import java.util.Stack;
     
     
     
     public class AddCommand extends AbstractCommand {
         private Adder adder = new Adder();
         private Stack<Integer> unStack = new Stack<Integer>();// 返回栈,用来记录所做的每一步操作,用于撤回
         private Stack<Integer> reStack = new Stack<Integer>();// 重复栈,用来存储返回栈弹出的数据,由于重复
     
         /**
          * 撤回
          */
         public int undo() {
             int i=0;
             if (unStack.isEmpty()) {
                 
                 i=-1;
             }else{
                 Integer pop = unStack.pop();
                 reStack.push(pop);
                 if(!unStack.isEmpty()){//判断弹出数据后是否为空,如果为空,说明已撤回到最原始状态
                     i=unStack.peek();
                 }
             }
             return i;
         }
     
         /**
          * 恢复
          */
         public int redo() {
             int i=0;
             if (reStack.isEmpty()) {
                 i=-1;
             }else{//撤回时只要可以可以撤回,则返回栈一定有数据
                 Integer pop = reStack.pop();
                 unStack.push(pop);
                 i=pop;
             }
             return i;
         }
     
         /**
          * 执行计算,并进行栈的更新
          */
         public int execute(int value) {
             int v = 0;
             if (unStack.isEmpty()) {// 说明还没有数据
                 v = adder.add(value);
                 unStack.push(v);
             } else {// 需要更新两个栈中的内容,并计算结果,其中返回栈应该更新,重复栈应该清空
                 v = adder.add(value);
                 unStack.push(v);
                 if (!reStack.isEmpty()) {
                     for (int i = 0; i < reStack.size(); i++) {
                         reStack.pop();
                     }
                 }
             }
             return v;
         }
    }
    AddCommand
    package 实验16命令模式;
    
     public class CalculatorForm {
              private AbstractCommand command;
              public void setCommand(AbstractCommand command) {
                  this.command =command;
              }
              /**
               * 执行运算
               * @param value
               */
         public void compute(int value) {
             command.execute(value);
             }
             /**
              * 撤回
              */
             public void undo() {
                 int i = command.undo();
                 if(i==-1){
                     System.out.println("缓存中已不存在数据");
             }else{
                     System.out.println("执行成功,运算结果是:"+i);
                 }
             }
             /**
              * 恢复
              */
             public void redo() {
                  int i = command.redo();
                 if(i==-1){
                     System.out.println("已恢复至最新数据");
                 }
                 else{
                     System.out.println("执行成功,运算结果是:"+i);
                 }
             }
         }
        
    CalculatorForm
    package 实验16命令模式;
    
    public class client {
              public static void main(String[] args) {
                  CalculatorForm form = new CalculatorForm();
                  AddCommand command = new AddCommand();
                  form.setCommand(command);
                 //计算
                 System.out.println("------计算过程-----");
                  form.compute(1);
                  form.compute(2);
                  form.compute(3);
                  form.compute(4);
                 //多次撤回
                  System.out.println("------撤回过程-----");
                  form.undo();
                  form.undo();
                  form.undo();
                  form.undo();
                  form.undo();
                 //多次恢复
                 System.out.println("------恢复过程-----");
                 form.redo();
                 form.redo();
                 form.redo();
                 form.redo();
                 form.redo();
             }
         }
    client
  • 相关阅读:
    通过ip找mac
    python开发总结
    iptables 通用语句
    运行pyqt4生成py文件增加代码
    re正则match、search、findall、finditer函数方法使用
    python list 字符串排序
    读取yaml文件小方法
    递归解析字典小方法
    assert 断言
    格式化字符串——初级% 和format
  • 原文地址:https://www.cnblogs.com/feng747/p/15574866.html
Copyright © 2020-2023  润新知