25 lines
620 B
Java
25 lines
620 B
Java
package memento;
|
|
|
|
import java.util.ArrayDeque;
|
|
import java.util.Deque;
|
|
|
|
/**
|
|
* Caretaker — manages the stack of mementos.
|
|
* It does NOT open or inspect the mementos; it just stores and returns them.
|
|
*/
|
|
public class History {
|
|
private final Deque<EditorMemento> snapshots = new ArrayDeque<>();
|
|
|
|
public void save(EditorMemento memento) {
|
|
snapshots.push(memento);
|
|
System.out.println(" [History] Saved: " + memento);
|
|
}
|
|
|
|
public EditorMemento undo() {
|
|
if (snapshots.isEmpty()) return null;
|
|
return snapshots.pop();
|
|
}
|
|
|
|
public int size() { return snapshots.size(); }
|
|
}
|