1.5 KiB
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
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/