Abstract Factory
Create families of related objects
Overview
The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It's like a factory of factories.
Use when you need to create multiple objects that work together (like UI components for different operating systems).
Key Concepts
Creates families of related objects
Factory of factories
Ensures products from same family work together
Client works with abstract interfaces
Follows Open/Closed Principle
Code Example
1// Abstract Products
2interface Button {
3 void render();
4 void onClick();
5}
6
7interface TextField {
8 void render();
9 String getValue();
10}
11
12interface Checkbox {
13 void render();
14 boolean isChecked();
15}
16
17// Concrete Products - Windows Family
18class WindowsButton implements Button {
19 public void render() { System.out.println("Windows-style button"); }
20 public void onClick() { System.out.println("Windows click"); }
21}
22
23class WindowsTextField implements TextField {
24 public void render() { System.out.println("Windows text field"); }
25 public String getValue() { return "Windows input"; }
26}
27
28class WindowsCheckbox implements Checkbox {
29 public void render() { System.out.println("Windows checkbox"); }
30 public boolean isChecked() { return true; }
31}
32
33// Concrete Products - Mac Family
34class MacButton implements Button {
35 public void render() { System.out.println("Mac-style button"); }
36 public void onClick() { System.out.println("Mac click"); }
37}
38
39class MacTextField implements TextField {
40 public void render() { System.out.println("Mac text field"); }
41 public String getValue() { return "Mac input"; }
42}
43
44class MacCheckbox implements Checkbox {
45 public void render() { System.out.println("Mac checkbox"); }
46 public boolean isChecked() { return false; }
47}
48
49// Abstract Factory
50interface UIFactory {
51 Button createButton();
52 TextField createTextField();
53 Checkbox createCheckbox();
54}
55
56// Concrete Factories
57class WindowsUIFactory implements UIFactory {
58 public Button createButton() { return new WindowsButton(); }
59 public TextField createTextField() { return new WindowsTextField(); }
60 public Checkbox createCheckbox() { return new WindowsCheckbox(); }
61}
62
63class MacUIFactory implements UIFactory {
64 public Button createButton() { return new MacButton(); }
65 public TextField createTextField() { return new MacTextField(); }
66 public Checkbox createCheckbox() { return new MacCheckbox(); }
67}
68
69// Client - works with abstractions
70class Application {
71 private Button button;
72 private TextField textField;
73 private Checkbox checkbox;
74
75 public Application(UIFactory factory) {
76 button = factory.createButton();
77 textField = factory.createTextField();
78 checkbox = factory.createCheckbox();
79 }
80
81 public void render() {
82 button.render();
83 textField.render();
84 checkbox.render();
85 }
86}
87
88// Usage
89public class Main {
90 public static void main(String[] args) {
91 // Determine OS and create appropriate factory
92 UIFactory factory;
93 String os = System.getProperty("os.name").toLowerCase();
94
95 if (os.contains("windows")) {
96 factory = new WindowsUIFactory();
97 } else {
98 factory = new MacUIFactory();
99 }
100
101 Application app = new Application(factory);
102 app.render(); // Renders OS-specific UI components
103 }
104}The application works with UIFactory interface. Switching between Windows and Mac UI requires only changing the factory.
When to Use
Creating families of related objects
Platform-specific implementations (Windows/Mac/Linux UI)
When products must be used together
Decoupling client from concrete implementations
Real-World Example
- Java AWT: Different look-and-feel for OS
- Cross-platform UI frameworks
- Database connection factories (MySQL, PostgreSQL families)
- Document generators (PDF, HTML with styles, fonts, etc.)
💡 Interview Tips
Explain it creates families of products
Show UI components for different OS example
Compare with Factory Method (single vs family)
Discuss how it ensures compatibility within family
Know when to use over Factory Method