Add all 23 GoF design pattern implementations
This commit is contained in:
31
03-behavioral/template-method/ApiMigration.java
Normal file
31
03-behavioral/template-method/ApiMigration.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package template;
|
||||
|
||||
public class ApiMigration extends DataMigration {
|
||||
|
||||
private final String endpoint;
|
||||
|
||||
public ApiMigration(String endpoint) { this.endpoint = endpoint; }
|
||||
|
||||
@Override protected String getSourceName() { return "API:" + endpoint; }
|
||||
|
||||
@Override
|
||||
protected void connect() {
|
||||
System.out.println(" Authenticating with API at " + endpoint + "...");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int readData() {
|
||||
System.out.println(" Paginating through API responses...");
|
||||
return 320;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int transformData(int rawCount) {
|
||||
System.out.println(" Flattening JSON, deduplicating (" + rawCount + " records)...");
|
||||
return rawCount - 15; // 15 duplicates removed
|
||||
}
|
||||
|
||||
// Override hook: silent migrations from APIs, no email spam
|
||||
@Override
|
||||
protected boolean sendNotification() { return false; }
|
||||
}
|
||||
27
03-behavioral/template-method/CsvMigration.java
Normal file
27
03-behavioral/template-method/CsvMigration.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package template;
|
||||
|
||||
public class CsvMigration extends DataMigration {
|
||||
|
||||
private final String filePath;
|
||||
|
||||
public CsvMigration(String filePath) { this.filePath = filePath; }
|
||||
|
||||
@Override protected String getSourceName() { return "CSV:" + filePath; }
|
||||
|
||||
@Override
|
||||
protected void connect() {
|
||||
System.out.println(" Opening CSV file: " + filePath);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int readData() {
|
||||
System.out.println(" Parsing CSV rows...");
|
||||
return 1_500; // simulated row count
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int transformData(int rawCount) {
|
||||
System.out.println(" Mapping CSV columns to target schema (" + rawCount + " rows)...");
|
||||
return rawCount; // no rows lost
|
||||
}
|
||||
}
|
||||
50
03-behavioral/template-method/DataMigration.java
Normal file
50
03-behavioral/template-method/DataMigration.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package template;
|
||||
|
||||
/**
|
||||
* Abstract Class with Template Method.
|
||||
* The overall migration algorithm is fixed here; subclasses fill in
|
||||
* the source-specific steps (connect, read, transform).
|
||||
*/
|
||||
public abstract class DataMigration {
|
||||
|
||||
// THE TEMPLATE METHOD — defines the fixed algorithm skeleton
|
||||
public final void migrate() {
|
||||
System.out.println("\n[" + getSourceName() + "] Starting migration...");
|
||||
connect();
|
||||
validate();
|
||||
int rowCount = readData();
|
||||
int transformed = transformData(rowCount);
|
||||
writeData(transformed);
|
||||
if (sendNotification()) {
|
||||
notifyStakeholders();
|
||||
}
|
||||
disconnect();
|
||||
System.out.println("[" + getSourceName() + "] Migration complete.\n");
|
||||
}
|
||||
|
||||
// Abstract steps — must be implemented by each subclass
|
||||
protected abstract String getSourceName();
|
||||
protected abstract void connect();
|
||||
protected abstract int readData();
|
||||
protected abstract int transformData(int rawCount);
|
||||
|
||||
// Concrete steps — common to all migrations
|
||||
protected void validate() {
|
||||
System.out.println(" Validating schema compatibility...");
|
||||
}
|
||||
|
||||
protected void writeData(int count) {
|
||||
System.out.println(" Writing " + count + " rows to target...");
|
||||
}
|
||||
|
||||
protected void disconnect() {
|
||||
System.out.println(" Closing source connection.");
|
||||
}
|
||||
|
||||
// Hook — subclasses may override to change behaviour
|
||||
protected boolean sendNotification() { return true; }
|
||||
|
||||
protected void notifyStakeholders() {
|
||||
System.out.println(" Sending completion email to team.");
|
||||
}
|
||||
}
|
||||
24
03-behavioral/template-method/Main.java
Normal file
24
03-behavioral/template-method/Main.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package template;
|
||||
|
||||
/**
|
||||
* Template Method Design Pattern — Runnable Demo
|
||||
* Run: javac template/*.java -d out/template && java -cp out/template template.Main
|
||||
* Article: https://ankurm.com/template-method-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Template Method Design Pattern Demo ===");
|
||||
|
||||
DataMigration csvJob = new CsvMigration("customers_export.csv");
|
||||
csvJob.migrate();
|
||||
|
||||
DataMigration apiJob = new ApiMigration("https://api.partner.com/v2/orders");
|
||||
apiJob.migrate();
|
||||
|
||||
System.out.println("-- Both jobs used the same migrate() skeleton --");
|
||||
System.out.println("-- Each provided its own connect/read/transform implementation --");
|
||||
System.out.println("-- ApiMigration overrode the sendNotification() hook: no email --");
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
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/
|
||||
Reference in New Issue
Block a user