首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >java中的Memento。可变状态

java中的Memento。可变状态
EN

Stack Overflow用户
提问于 2018-06-13 05:44:29
回答 1查看 122关注 0票数 1

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

这是memento类。

代码语言:javascript
复制
package memento;

public class Memento
{
    private final boolean[] vectorState;

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

    boolean[] getMemento() { return vectorState;}

}

发起者只需要将布尔值的向量左移即可。(TRUE,FALSE,FALSE)左移位返回:(FALSE,FALSE,TRUE)。这就是实现。

代码语言:javascript
复制
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);}

}

看门人:

代码语言:javascript
复制
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();}

}

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

代码语言:javascript
复制
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");
        }

    }

}

控制台输出:

代码语言:javascript
复制
shiftleft working
Exception in thread "main" java.lang.IllegalStateException: [true, false, false]
    at memento.Main.main(Main.java:26)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-13 07:03:45

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

要解决此问题,请在Memento对象的构造函数中创建数组的副本:

代码语言:javascript
复制
public Memento(boolean[] vector) {
    vectorState = Arrays.copyOf(vector, vector.length);
}

除此之外,你似乎把你的课程搞混了。BooleanVectorShilftLeftOriginator是发起人,而Main是管理人。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50826255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档