Template Method: sync post code blocks to repo exactly (DataMigration/CsvMigration/ApiMigration were entirely different from repo), Java 25, H6 file labels, fix stale console output, README mapping
This commit is contained in:
29
03-behavioral/template-method/README.md
Normal file
29
03-behavioral/template-method/README.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# Template Method Design Pattern — Java Example
|
||||||
|
|
||||||
|
**Pattern:** Behavioral → Template Method
|
||||||
|
**Article:** https://ankurm.com/template-method-design-pattern-java/
|
||||||
|
|
||||||
|
## What this example shows
|
||||||
|
|
||||||
|
A data migration pipeline with a fixed skeleton and variable steps. `DataMigration` declares `migrate()` as `final` — the sequence (connect, validate, read, transform, write, notify, disconnect) can never be reordered or skipped by a subclass. `getSourceName()`, `connect()`, `readData()`, and `transformData()` are abstract steps each subclass must supply; `validate()`, `writeData()`, and `disconnect()` are concrete steps shared by everyone; `sendNotification()` is a hook with a default of `true` that a subclass can override. `CsvMigration` uses every default unchanged. `ApiMigration` overrides the hook to stay silent. `Main` runs both through the identical skeleton.
|
||||||
|
|
||||||
|
## How to run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
javac template-method/*.java -d out/template-method
|
||||||
|
java -cp out/template-method template.Main
|
||||||
|
```
|
||||||
|
|
||||||
|
Requires Java 25.
|
||||||
|
|
||||||
|
## Post Section ↔ File Mapping
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| Implementation: Data Migration Pipeline — the abstract class & template method | `DataMigration.java` |
|
||||||
|
| Implementation: Data Migration Pipeline — CsvMigration | `CsvMigration.java` |
|
||||||
|
| Implementation: Data Migration Pipeline — ApiMigration | `ApiMigration.java` |
|
||||||
|
| Implementation: Data Migration Pipeline — wiring it together | `Main.java` |
|
||||||
|
|
||||||
|
Article: https://ankurm.com/template-method-design-pattern-java/
|
||||||
|
All patterns: https://ankurm.com/design-patterns-java/
|
||||||
15
README.md
15
README.md
@@ -445,6 +445,21 @@ javac 03-behavioral/strategy/*.java -d out/strategy
|
|||||||
java -cp out/strategy strategy.Main
|
java -cp out/strategy strategy.Main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Template Method (`03-behavioral/template-method/`)
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| Implementation: Data Migration Pipeline — the abstract class & template method | `DataMigration.java` |
|
||||||
|
| Implementation: Data Migration Pipeline — CsvMigration | `CsvMigration.java` |
|
||||||
|
| Implementation: Data Migration Pipeline — ApiMigration | `ApiMigration.java` |
|
||||||
|
| Implementation: Data Migration Pipeline — wiring it together | `Main.java` |
|
||||||
|
|
||||||
|
Run it:
|
||||||
|
```bash
|
||||||
|
javac 03-behavioral/template-method/*.java -d out/template-method
|
||||||
|
java -cp out/template-method template.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