gpt4 book ai didi

java - java 中的纪念品。可变状态

转载 作者:行者123 更新时间:2023-11-30 02:08:19 28 4
gpt4 key购买 nike

我正在尝试理解纪念品模式。为此,我正在尝试实现撤消功能。问题是,当我将发起者的旧状态保存在队列中并运行不同的函数时,保存状态会更改为当前状态。我真的需要帮助来理解我做错了什么。我怎样才能使 vector 不可变。

这是纪念品类。

package memento;

public class Memento
{
private final boolean[] vectorState;

public Memento(boolean[] vector) {vectorState = vector;}

boolean[] getMemento() { return vectorState;}

}

发起者只需将 boolean vector 左移即可。(TRUE,FALSE,FALSE) 左移返回:(FALSE,FALSE,TRUE)。这就是实现。

package memento;

public class ShilftLeftOriginator
{
private boolean[] vector;

public ShilftLeftOriginator(boolean[] vector) {this.vector = vector;}

public void execute()
{
final boolean firstValue = this.vector[0];
for (int i = 1; i < this.vector.length; i++) {
this.vector[i - 1] = this.vector[i];
}
this.vector[vector.length - 1] = firstValue;
}


public Memento saveToMemento() {return new Memento(vector);}

}

看门人:

package memento;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;


public final class BooleanVector {
private boolean[] vector;
private Deque<Memento> mementoList = new LinkedList<>();

public BooleanVector(boolean[] inputValues) {
this.vector = inputValues;
}


@Override
public boolean equals(Object obj)
{
if (obj == null) return false;
if (!(obj instanceof BooleanVector)) return false;
BooleanVector otherVector = (BooleanVector) obj;
return Arrays.equals(this.vector, otherVector.vector);
}

public void shiftLeft()
{
ShilftLeftOriginator shiftLeft = new ShilftLeftOriginator(vector);
mementoList.add(shiftLeft.saveToMemento());
shiftLeft.execute(); // This is my Problem. After execute ist call the value(vector) in mementoList changes
}

public void undo(){ this.vector = mementoList.pop().getMemento();}

}

现在是测试类和我收到的错误。

package memento;

public class Main {

public static void main(String[] args) {
boolean[] inputValues = { false, true, false };
BooleanVector vector = new BooleanVector(inputValues);

vector.shiftLeft();

boolean[] expectedValues = new boolean[] { true, false, false };
BooleanVector expectedVector = new BooleanVector(expectedValues);

if (!vector.equals(expectedVector)) {
throw new IllegalStateException(vector.toString());
} else {
System.out.println("shiftleft working");
}

vector.undo();

expectedValues = new boolean[] { false, true, false };
expectedVector = new BooleanVector(expectedValues);

if (!vector.equals(expectedVector)) {
throw new IllegalStateException(vector.toString());
} else {
System.out.println("undo working");
}

}

}

控制台输出:

shiftleft working
Exception in thread "main" java.lang.IllegalStateException: [true, false, false]
at memento.Main.main(Main.java:26)

最佳答案

问题是你总是在操作同一个数组。因此,如果向左移动,您存储在 Memento 对象中的数组也会向左移动,因为它们都是相同的数组。

要解决这个问题,请在 Memento 对象的构造函数中复制数组:

public Memento(boolean[] vector) {
vectorState = Arrays.copyOf(vector, vector.length);
}

除此之外,你的类(class)似乎很困惑。 BooleanVectorShilftLeftOriginator 是发起者,而 Main 是看护者。

关于java - java 中的纪念品。可变状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50826255/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com