From e9ac2d38ba9d085387514a643b8faeb4bda88732 Mon Sep 17 00:00:00 2001 From: Ankur Date: Wed, 24 Jun 2026 17:33:55 +0530 Subject: [PATCH] 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 --- 03-behavioral/template-method/README.md | 29 +++++++++++++++++++++++++ README.md | 15 +++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 03-behavioral/template-method/README.md diff --git a/03-behavioral/template-method/README.md b/03-behavioral/template-method/README.md new file mode 100644 index 0000000..a8717ac --- /dev/null +++ b/03-behavioral/template-method/README.md @@ -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/ diff --git a/README.md b/README.md index cad3694..4bb012d 100644 --- a/README.md +++ b/README.md @@ -445,6 +445,21 @@ javac 03-behavioral/strategy/*.java -d out/strategy 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 - *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides