Add all 23 GoF design pattern implementations (2026-06-13)

This commit is contained in:
Ankur
2026-06-13 21:44:56 +05:30
commit a5beb61425
106 changed files with 2977 additions and 0 deletions

View 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; }
}

View 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
}
}

View 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.");
}
}

View 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 ===");
}
}