🏭Factory Pattern
🧠 Concept
🧱 Key Roles
Role
Description
💻 Java Example – Report Generator Factory
// Product interface
interface Report {
void generate();
}
// Concrete products
class PDFReport implements Report {
public void generate() {
System.out.println("Generating PDF report...");
}
}
class ExcelReport implements Report {
public void generate() {
System.out.println("Generating Excel report...");
}
}
// Factory class
class ReportFactory {
public static Report createReport(String type) {
switch (type) {
case "PDF": return new PDFReport();
case "EXCEL": return new ExcelReport();
default: throw new IllegalArgumentException("Unknown report type");
}
}
}🧠 Flow Summary (Step-by-Step)
Step
Action
Factory Role
Output
🪜 Summary
🧰 Variants
Variant
Description
🧾 Real-world Analogy
🧭 TL;DR
Last updated