...
The purpose of this Editor
class is to serve as the Originator, which is responsible for creating Memento objects (EditorState
) that capture the state of the editor, as well as restoring the state of the editor from a given Memento. It encapsulates the state within the content
attribute and provides methods to create and restore Memento objects.
Code Block | ||
---|---|---|
| ||
package MementoPattern; import java.util.ArrayList; import java.util.List; //This is out Caretaker public class History { //We could actually use a stack here - but I have chosen to use a LIST private List<EditorState> states = new ArrayList<>(); public void push(EditorState state) { states.add(state); } public EditorState pop() { var lastIndex = states.size() - 1; var lastState = states.get(lastIndex); states.remove(lastState); return lastState; } } |
...