Java:矩形类的Undo和Redo方法

Java:矩形类的Undo和Redo方法,java,Java,我有一个自定义矩形类: 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 = nu

我有一个自定义矩形类:

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() {   }

}

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

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

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
            ...
        }
    }

}
公共类矩形(){
private states:List=new ArrayList();//每次更新任何属性时,我们都会保存矩形的状态
private int stateIndex=0;//这是此矩形上活动状态的索引
专用整数的高度、宽度、x、y;
私人色彩;
公共矩形(){
this.height=null;
this.width=null;
这个.x=null;
这个.y=null;
this.color=null;
}
公共空间设置高度(inth){
this.stateIndex=states.size()+1;//在向状态添加新状态时,我们将在状态索引中增加一个
这个高度=h;
this.states.add(this);//在设置任何属性的值后添加状态
}
public void undo(){
如果(this.stateIndex>0){//仅当当前状态之前有可用状态时才撤消
this.stateIndex-->//将当前索引减少一个
this.height=this.states.get(stateIndex).height;//从state设置属性
...
}
}
公共无效重做(){
如果(this.stateIndex
好吧,关于你的第一个问题——是的,这涉及到某种堆栈,可能有两个(第二个用于重做)。至少在概念上,因为您需要后进先出机制。您的第二个问题无法回答-“正确”取决于需求,我们不知道您的需求。或者这是可能的,但您也可以使用,它具有通常的
push()
pop()
堆栈的方法。@Hulk感谢您为我的知识添加了新内容。我不知道这个东西的存在:)好吧,如果你使用它,你需要第二个堆栈用于重做部分。所以它不仅在这里有优势。但是,当用户在撤消后执行其他操作时,您可能需要清除重做部分,而当它们分开时,这就容易多了。@Hulk,我明白了;)这是开发者要问的另一个问题。。。。英雄联盟