Memento无法在Java中使用undo更新状态

时间:2019-02-04 11:17:49

标签: java design-patterns memento

我有一个graph,其中包含verticesedges,我有一个originator类,其中包含应该用于{{1 }}或graph

中的insert vertices

界面get the list of vertices

graph

界面vertex

public interface Vertex<V> {
    public V element();
}

接口edge与插入新顶点,其他方法是hide()

public interface Edge<E, V> {
    public E element();
    public Vertex<V>[] vertices();
}

ADT graph实现,用于存储public interface Graph<V, E> { public void insertVertex(V vElement); public Iterable<Vertex<V>> vertices(); } (和边,但不需要)的集合

Graph

vertices

public class GraphEdgeList<V, E> implements Graph<V, E> {
    private Map<V, Vertex<V>> vertices;

    public GraphEdgeList() {
        this.vertices = new HashMap<>();
    }

    @Override
    public void insertVertex(V vElement) {
     //method to insert new vertex element
     //not need to return nothing
    }

    @Override
    public Iterable<Vertex<V>> vertices() {
    //return a list of vertices in graph
    }
}

Memento

public class Memento {
    private Graph graph;

    public Memento(Originator originator) {
        graph = originator.getGraph();
    }

    public Graph getGraph() {
        return graph;
    }
}

界面Originator

public class Originator<V,E> {
    private Graph<V,E> graph;
    private Caretaker caretaker;

    public Originator(Caretaker caretaker) {
      this.graph = new GraphEdgeList();
      this.caretaker = caretaker;
    }

    public Memento createMemento() {//create new memento
        return new Memento(this);
    }

    public void setMemento(Memento memento) {//set memento
       graph = memento.getGraph();
    }

    public Graph getGraph() {
      return graph;
    }

    public Caretaker getCaretaker() {
     return caretaker;
    }
}

IMemento实现接口public interface IMemento { public void save(Originator originator); public void restore(Originator originator); }

CareTaker

我的疑问是在保存状态后开始的,尝试执行撤消IMemento不会更新为先前的状态

public class Caretaker implements IMemento {

    private final Stack<Memento> undoMemento;//stack explicit

    public Caretaker() {
        this.undoMemento = new StackDynamic();
    }

    @Override
    public void save(Originator originator) {
        Memento memento = originator.createMemento();
        undoMemento.push(memento);
    }

    @Override
    public void restore(Originator originator) {
        if (undoMemento.isEmpty() != true) {
            Memento memento = undoMemento.pop();
            originator.setMemento(memento);
       }
    }
}

但是当我恢复de graph时仍然有2个顶点

public class Main {
    public static void main(String[] args) {
        Caretaker caretaker = new Caretaker();
        Originator<String, String> originator = new Originator(caretaker);

        //create new string and insert in graph
        originator.getGraph.insertVertex("A");
        //caretaker save state
        caretaker.save(originator);

       //create another string and insert in graph
        originator.getGraph.insertVertex("B");
        //caretaker save state
        caretaker.save(originator);      
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您引用的是Memento内部相同的顶点集合。

尝试更改以下类别:

public class Originator<V,E> {
    private Graph<V,E> graph;
    private Caretaker caretaker;

    public Originator(Caretaker caretaker) {
      this.graph = new GraphEdgeList();
      this.caretaker = caretaker;
    }

    public Originator(Originator<V, E> originator) {
         this.graph = new GraphEdgeList((GraphEdgeList) originator.getGraph());
         this.caretaker = originator.getCaretaker();
    }

    public Memento createMemento() {//create new memento
        return new Memento(new Originator(this));
    }

    public void setMemento(Memento memento) {//set memento
       graph = memento.getGraph();
    }

    public Graph getGraph() {
      return graph;
    }

    public Caretaker getCaretaker() {
     return caretaker;
    }
}

查看新的构造函数。

public class GraphEdgeList<V, E> implements Graph<V, E> {
    private Map<V, Vertex<V>> vertices;

    public GraphEdgeList() {
        this.vertices = new HashMap<>();
    }

    public GraphEdgeList(GraphEdgeList graph) {
         this.vertices = new HashMap<>();
         this.vertices.putAll(graph.getVertices());
    }

    @Override
    public void insertVertex(V vElement) {
        this.vertices.put(vElement, null);
    }

    public Map<V, Vertex<V>> getVertices() {
        return vertices;
    }

    public void setVertices(Map<V, Vertex<V>> vertices) {
        this.vertices = vertices;
    }

    @Override
    public Iterable<Vertex<V>> vertices() {
        return this.vertices.values();
    }

    @Override
    public String toString() {
        return "GraphEdgeList [vertices=" + vertices + "]";
    } 
}