Command: sync post code blocks to repo exactly, Java 25, split merged commands, add H6 file labels and README mapping

This commit is contained in:
2026-06-24 16:38:35 +05:30
parent 6aaee03ecb
commit 967d490323
2 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
# Command Design Pattern — Java Example
**Pattern:** Behavioral → Command
**Article:** https://ankurm.com/command-design-pattern-java/
## What this example shows
A text editor with full undo support. `Command` declares `execute()`/`undo()`/`getDescription()`. `InsertCommand` and `DeleteCommand` are concrete commands that call back into the receiver, `TextEditor`. `DeleteCommand` captures the text it's about to delete *before* deleting it, so `undo()` can restore it. `CommandHistory` is the invoker: it pushes executed commands onto a stack and pops/undoes them on request, without knowing what any command actually does.
## How to run
```bash
javac command/*.java -d out/command
java -cp out/command command.Main
```
Requires Java 25.
## Post Section ↔ File Mapping
| Post Section | File(s) |
|---|---|
| Step 1 — Command Interface | `Command.java` |
| Step 2 — The Receiver (TextEditor) | `TextEditor.java` |
| Step 3 — Concrete Commands | `InsertCommand.java`, `DeleteCommand.java` |
| Step 4 — The Invoker (CommandHistory) | `CommandHistory.java` |
| Demo | `Main.java` |
Note: the `MacroCommand` snippet (Composite applied to Command) and the `Runnable`/`Callable`/`CompletableFuture` JDK snippets are illustrative only — they are not part of this repository's runnable example.
Article: https://ankurm.com/command-design-pattern-java/
All patterns: https://ankurm.com/design-patterns-java/

View File

@@ -322,6 +322,22 @@ javac 03-behavioral/chain-of-responsibility/*.java -d out/chain
java -cp out/chain chain.Main java -cp out/chain chain.Main
``` ```
### Command (`03-behavioral/command/`)
| Post Section | File(s) |
|---|---|
| Step 1 — Command Interface | `Command.java` |
| Step 2 — The Receiver (TextEditor) | `TextEditor.java` |
| Step 3 — Concrete Commands | `InsertCommand.java`, `DeleteCommand.java` |
| Step 4 — The Invoker (CommandHistory) | `CommandHistory.java` |
| Demo | `Main.java` |
Run it:
```bash
javac 03-behavioral/command/*.java -d out/command
java -cp out/command command.Main
```
## Reference ## Reference
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides - *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides