【问题标题】:Java: Undo and Redo methods for a Rectangle class [duplicate]Java:矩形类的撤消和重做方法[重复]
【发布时间】:2021-01-30 15:11:33
【问题描述】:

我有一个自定义的 Rectangle 类:

public class Rectangle () {
      private int height, width, x, y;
      private Color color;
      
      public Rectangle () {
           this.height = null;
           this.width = null;
           this.x = null;
           this.y = null;
           this.color = null;
      }
      
      public void setHeight(int h) { this.height = h; }

      public void setWidth(int w) { this.width = w; }
      
      public void setX(int x) { this.x = x; }
      
      public void setY(int y) { this.y = y; }
       
      public void setColor(Color c) { this.color = c; }

      public int getWidth() { return this.width; }
      
      public int getHeight() { return this.height; }

      public int getX() { return this.x; }
      
      public int getY() { return this.y; }
      
      public Color getColor() { return this.color; }
      
      public void undo() {   }

      public void redo() {   }

}

我将如何为这个类实现撤消和重做功能,使其能够将矩形恢复到以前的状态,而无需用户提及上次使用的方法。我有一个模糊的想法,涉及使用堆栈,但我被困在如何实际编码它上。我的第二个问题是我不确定我的构造函数是否正确,我将所有内容初始化为 null 而不提供任何参数,因为我希望人们改用 getter/setter。请帮忙。

【问题讨论】:

  • 好吧,至于你的第一个问题 - 是的,这涉及某种堆栈,可能其中两个(第二个用于重做)。至少在概念上,因为您需要后进先出机制。
  • 您的第二个问题无法回答——“正确”取决于要求,我们不知道您的要求。

标签: java


【解决方案1】:

您可以将状态保存在数组中并从中恢复矩形

public class Rectangle () {
    
    private states: List<Rectangle> = new ArrayList(); // we save the state of rectangle on every update to any property
    private int stateIndex = 0; // this is the index of the state which is active on this rectangle
    
    private int height, width, x, y;
    private Color color;
    
    public Rectangle () {
       this.height = null;
       this.width = null;
       this.x = null;
       this.y = null;
       this.color = null;
    }
    
    public void setHeight(int h) { 
        this.stateIndex = states.size() + 1; // we ll increase one in state index as new state is added to the states
        this.height = h; 
        this.states.add(this); // add the state after you set the value of any property
    }
    
    public void undo() { 
        if(this.stateIndex > 0){ // only undo when there is state available before the current state
            this.stateIndex--; // reduce the current index by one
            this.height = this.states.get(stateIndex).height; // set the properties from state
            ...
        }
    }
    
    public void redo() {    
        if(this.stateIndex < states.size()){ // can go more the available states in cache
            this.stateIndex++; // increase the current state index
            this.height = this.states.get(stateIndex).height; // update the values of the properties
            ...
        }
    }

}

【讨论】:

  • 这是可能的,但您也可以使用ArrayDeque,它具有通常的push()pop() 堆栈方法。
  • @Hulk 感谢您为我添加新知识。我不知道那东西存在:)
  • 好吧,如果你使用它,你需要第二个堆栈用于重做部分。所以它不仅在这里有优势。但是当用户在撤消后执行一些其他操作时,您可能需要清除重做部分 - 当它们分开时这会更容易一些。
  • @Hulk 我明白了 ;) 那是开发人员要问的另一个问题....哈哈
猜你喜欢
  • 2011-11-14
  • 2012-09-05
  • 2011-06-14
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 2014-04-15
  • 2021-03-27
  • 2014-02-04
相关资源
最近更新 更多