Files
design-patterns/03-behavioral/memento/EditorMemento.java

29 lines
949 B
Java

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 + "]";
}
}