Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
8
03-behavioral/command/Command.java
Normal file
8
03-behavioral/command/Command.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package command;
|
||||
|
||||
/** Command interface: every action is an object */
|
||||
public interface Command {
|
||||
void execute();
|
||||
void undo();
|
||||
String getDescription();
|
||||
}
|
||||
28
03-behavioral/command/CommandHistory.java
Normal file
28
03-behavioral/command/CommandHistory.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package command;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
/**
|
||||
* Invoker — holds command history and triggers execute/undo.
|
||||
* It doesn't know what the commands do; it just calls execute() and undo().
|
||||
*/
|
||||
public class CommandHistory {
|
||||
private final Deque<Command> history = new ArrayDeque<>();
|
||||
|
||||
public void execute(Command cmd) {
|
||||
cmd.execute();
|
||||
history.push(cmd);
|
||||
System.out.println(" Executed: " + cmd.getDescription());
|
||||
}
|
||||
|
||||
public void undo() {
|
||||
if (history.isEmpty()) {
|
||||
System.out.println(" Nothing to undo.");
|
||||
return;
|
||||
}
|
||||
Command cmd = history.pop();
|
||||
cmd.undo();
|
||||
System.out.println(" Undone: " + cmd.getDescription());
|
||||
}
|
||||
}
|
||||
23
03-behavioral/command/DeleteCommand.java
Normal file
23
03-behavioral/command/DeleteCommand.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package command;
|
||||
|
||||
public class DeleteCommand implements Command {
|
||||
private final TextEditor editor;
|
||||
private final int start;
|
||||
private final int length;
|
||||
private String deletedText; // saved for undo
|
||||
|
||||
public DeleteCommand(TextEditor editor, int start, int length) {
|
||||
this.editor = editor;
|
||||
this.start = start;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
deletedText = editor.getText().substring(start, start + length);
|
||||
editor.deleteText(start, length);
|
||||
}
|
||||
|
||||
@Override public void undo() { editor.insertText(deletedText, start); }
|
||||
@Override public String getDescription() { return "Delete " + length + " chars at " + start; }
|
||||
}
|
||||
17
03-behavioral/command/InsertCommand.java
Normal file
17
03-behavioral/command/InsertCommand.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package command;
|
||||
|
||||
public class InsertCommand implements Command {
|
||||
private final TextEditor editor;
|
||||
private final String text;
|
||||
private final int position;
|
||||
|
||||
public InsertCommand(TextEditor editor, String text, int position) {
|
||||
this.editor = editor;
|
||||
this.text = text;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override public void execute() { editor.insertText(text, position); }
|
||||
@Override public void undo() { editor.deleteText(position, text.length()); }
|
||||
@Override public String getDescription() { return "Insert \"" + text + "\" at " + position; }
|
||||
}
|
||||
40
03-behavioral/command/Main.java
Normal file
40
03-behavioral/command/Main.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package command;
|
||||
|
||||
/**
|
||||
* Command Design Pattern — Runnable Demo
|
||||
* Run: javac command/*.java -d out/command && java -cp out/command command.Main
|
||||
* Article: https://ankurm.com/command-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Command Design Pattern Demo ===\n");
|
||||
|
||||
TextEditor editor = new TextEditor();
|
||||
CommandHistory history = new CommandHistory();
|
||||
|
||||
System.out.println("Initial: " + editor);
|
||||
|
||||
history.execute(new InsertCommand(editor, "Hello", 0));
|
||||
System.out.println("After: " + editor);
|
||||
|
||||
history.execute(new InsertCommand(editor, " World", 5));
|
||||
System.out.println("After: " + editor);
|
||||
|
||||
history.execute(new DeleteCommand(editor, 5, 6));
|
||||
System.out.println("After: " + editor);
|
||||
|
||||
System.out.println("\n-- Undo sequence --");
|
||||
history.undo();
|
||||
System.out.println("After undo: " + editor);
|
||||
|
||||
history.undo();
|
||||
System.out.println("After undo: " + editor);
|
||||
|
||||
history.undo();
|
||||
System.out.println("After undo: " + editor);
|
||||
|
||||
history.undo(); // nothing left
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
21
03-behavioral/command/TextEditor.java
Normal file
21
03-behavioral/command/TextEditor.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package command;
|
||||
|
||||
/**
|
||||
* Receiver — contains the actual text editing logic.
|
||||
* The commands call methods on this object.
|
||||
*/
|
||||
public class TextEditor {
|
||||
private final StringBuilder text = new StringBuilder();
|
||||
|
||||
public void insertText(String content, int position) {
|
||||
text.insert(position, content);
|
||||
}
|
||||
|
||||
public void deleteText(int start, int length) {
|
||||
text.delete(start, start + length);
|
||||
}
|
||||
|
||||
public String getText() { return text.toString(); }
|
||||
|
||||
@Override public String toString() { return "Editor[\"" + text + "\"]"; }
|
||||
}
|
||||
Reference in New Issue
Block a user