javadesign-patternsmemento

Memento not update state with undo in Java


I have a graph that contains vertices and edges and I have an originator class that contains the graph that should be used to insert vertices or get the list of vertices in the graph

interface vertex

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

interface edge

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

interface graph with Inserts a new vertex and other method is hide()

public interface Graph<V, E> {
  public void insertVertex(V vElement);
  public Iterable<Vertex<V>> vertices();
}

ADT Graph implementation that stores a collection of vertices (and edges, but not need)

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

class Memento

public class Memento {
    private Graph graph;

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

    public Graph getGraph() {
        return graph;
    }
}

class 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;
    }
}

Interface IMemento

public interface IMemento {
    public void save(Originator originator);
    public void restore(Originator originator);
}

class CareTaker implements interface 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);
       }
    }
}

my doubt starts after saving the state and when trying to do undo the graph does not update to the previous state

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);      
    }
}

but when I do restore de graph still with 2 vertices

caretaker.restore(originator);

any suggestion?


Solution

  • You are referencing the same collection of vertices inside the Memento.

    Try to change the following classes:

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

    See the new constructors.

    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 + "]";
        } 
    }