What Problem It Solves
When you have FAMILIES of related objects that must be used together (e.g., "dark theme" = DarkButton + DarkPanel + DarkText). You don't want DarkButton mixed with LightPanel. Abstract Factory lets you create entire families through one interface.
Abstract Factory vs Factory Method
| Aspect | Factory Method | Abstract Factory |
|---|---|---|
| Creates | One product type | A family of products |
| Interface | Single create() method | Multiple creation methods |
| Extension | New subclass for new product | New factory for new family |
| Use in LLD | Parking spot, chess piece | UI toolkit, cross-platform widgets |
When Simple Factory Isn't Enough
Simple Factory creates one product. Abstract Factory creates a PRODUCT FAMILY (multiple related products).
Where This Appears in LLD
Rare in typical machine coding. Use it when a problem explicitly asks for "themes," "skins," "cross-platform support," or "multiple rendering styles."
Implementation
// โโโ EXAMPLE 1 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// WHAT WE ARE IMPLEMENTING:
// A cross-platform UI theme manager supporting Dark and Light skins
// consistently.
//
// WHERE THE PATTERN FITS IN:
// ThemeFactory acts as the Abstract Factory. DarkThemeFactory and
// LightThemeFactory are Concrete Factories creating matching Button and
// Checkbox products.
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// --- Product interfaces ---
interface Button { void render(); }
interface Checkbox { void render(); }
interface ThemeFactory {
Button createButton();
Checkbox createCheckbox();
}
// --- Dark theme family ---
class DarkButton implements Button {
public void render() { System.out.println(" [DarkButton] Rendered with dark bg"); }
}
class DarkCheckbox implements Checkbox {
public void render() { System.out.println(" [DarkCheckbox] Rendered dark"); }
}
class DarkThemeFactory implements ThemeFactory {
public Button createButton() { return new DarkButton(); }
public Checkbox createCheckbox() { return new DarkCheckbox(); }
}
// --- Light theme family ---
class LightButton implements Button {
public void render() { System.out.println(" [LightButton] Rendered with light bg"); }
}
class LightCheckbox implements Checkbox {
public void render() { System.out.println(" [LightCheckbox] Rendered light"); }
}
class LightThemeFactory implements ThemeFactory {
public Button createButton() { return new LightButton(); }
public Checkbox createCheckbox() { return new LightCheckbox(); }
}
class Application {
private final Button button;
private final Checkbox checkbox;
public Application(ThemeFactory factory) {
button = factory.createButton();
checkbox = factory.createCheckbox();
}
void render() {
button.render();
checkbox.render();
}
}
public class Main {
public static void main(String[] args) {
// Choose family once โ all products are consistent
Application app = new Application(new DarkThemeFactory());
app.render();
}
}Review
Help Us Improve
How helpful was this walkthrough?
Click a star to rate. We actively use this feedback to refine and update our system design content.
Placeholder
Discussion
Share your thoughts, ask questions, or help others.
Loading comments...