Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
24
03-behavioral/memento/History.java
Normal file
24
03-behavioral/memento/History.java
Normal file
@@ -0,0 +1,24 @@
|
||||
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(); }
|
||||
}
|
||||
Reference in New Issue
Block a user