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:
32
03-behavioral/command/README.md
Normal file
32
03-behavioral/command/README.md
Normal 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/
|
||||||
16
README.md
16
README.md
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user