Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
28
03-behavioral/memento/EditorMemento.java
Normal file
28
03-behavioral/memento/EditorMemento.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package memento;
|
||||
|
||||
/**
|
||||
* Memento — a snapshot of the editor's state.
|
||||
* Immutable: once created, the state cannot be changed.
|
||||
* The Caretaker holds these; the Originator creates and restores from them.
|
||||
*/
|
||||
public final class EditorMemento {
|
||||
private final String content;
|
||||
private final int cursorPosition;
|
||||
private final String selectedText;
|
||||
|
||||
EditorMemento(String content, int cursorPosition, String selectedText) {
|
||||
this.content = content;
|
||||
this.cursorPosition = cursorPosition;
|
||||
this.selectedText = selectedText;
|
||||
}
|
||||
|
||||
// Package-private: only the Editor (Originator) should read the state back
|
||||
String getContent() { return content; }
|
||||
int getCursorPosition() { return cursorPosition; }
|
||||
String getSelectedText() { return selectedText; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Snapshot[content=\"" + content + "\", cursor=" + cursorPosition + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user