I am implementing an undo/redo function which requires me to use memento pattern.
The flow of the partial program : "...the program then store the previous Vector using Memento Pattern, then the newly created object will be added to the Vector. After that, user may choose a show command to show what is inside the Vector, he can also enter undo command to restore, the undo can be repeated until it is restored to the original state..."
From my research, I know there will be an originator, memento and caretaker.
Here's my caretaker program
public class CareTaker {
private Memento m;
private Stack s;
private Vector v;
// Some of the implementation are not shown
public void create() {
// Some of the implementation are not shown
// Assuming Vector is named "v"
// Passing Vector to memento
m = new Memento(v);
s.add(m);
}
public void undo() {
v = s.pop().restore();
}
}
public class Memento {
private Vector _v;
public Memento(Vector v) {
_v = v;
}
public Vector restore() {
return _v;
}
}
Unfortunately , I failed to identify the "Originator" nor I know which one will be. Is this code fragment ever a correct Memento pattern if there is no Originator?
The memento pattern is used to save the state of an object without knowing it's internal data structures.
I try to explain it with an
Iterator
exampleA client can now save any state of the iterator without knowing how the iterator internally manages it's state.