Add all 23 GoF design pattern implementations (2026-06-13)

This commit is contained in:
Ankur
2026-06-13 21:44:56 +05:30
commit a5beb61425
106 changed files with 2977 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package memento;
/**
* Originator — the text editor whose state we're saving and restoring.
* Creates mementos and restores from them; no history management here.
*/
public class Editor {
private String content = "";
private int cursorPosition = 0;
private String selectedText = "";
public void type(String text) {
content = content.substring(0, cursorPosition) + text + content.substring(cursorPosition);
cursorPosition += text.length();
}
public void selectText(int start, int end) {
this.selectedText = content.substring(start, end);
System.out.println(" Selected: \"" + selectedText + "\"");
}
public void deleteSelection() {
content = content.replace(selectedText, "");
selectedText = "";
}
/** Create a snapshot of current state */
public EditorMemento save() {
return new EditorMemento(content, cursorPosition, selectedText);
}
/** Restore state from a snapshot */
public void restore(EditorMemento memento) {
this.content = memento.getContent();
this.cursorPosition = memento.getCursorPosition();
this.selectedText = memento.getSelectedText();
}
@Override
public String toString() {
return "Editor[\"" + content + "\", cursor=" + cursorPosition + "]";
}
}

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

View 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(); }
}

View File

@@ -0,0 +1,38 @@
package memento;
/**
* Memento Design Pattern — Runnable Demo
* Run: javac memento/*.java -d out/memento && java -cp out/memento memento.Main
* Article: https://ankurm.com/memento-design-pattern-java/
*/
public class Main {
public static void main(String[] args) {
System.out.println("=== Memento Design Pattern Demo ===\n");
Editor editor = new Editor();
History history = new History();
System.out.println("Initial: " + editor);
editor.type("Hello");
history.save(editor.save());
System.out.println("After type: " + editor);
editor.type(", World");
history.save(editor.save());
System.out.println("After type: " + editor);
editor.type("! How are you?");
System.out.println("After type: " + editor);
System.out.println("\n-- Undo --");
editor.restore(history.undo());
System.out.println("After undo: " + editor);
editor.restore(history.undo());
System.out.println("After undo: " + editor);
System.out.println("\nHistory empty: " + (history.size() == 0));
System.out.println("\n=== Demo complete ===");
}
}