Command Palette

Search for a command to run...

PHASE 2Beginner ~6 min· topic 2 of 22Creational

Creational 2 — Abstract Factory

In one line

Provides an interface for creating a family of related objects without specifying their concrete classes — e.g. a UI kit that produces LightButton + LightDialog together.

0/22 · 0%

Think of it like this

IKEA furniture collections. If you pick the 'Modern' collection, the table, chair and lamp all match. You never end up with a Modern table and a Vintage chair by mistake.

Key ideas

  1. 01

    Solves: consistency of a product family (don't ship a dark dialog with a light button).

  2. 02

    Each concrete factory builds one full family; swapping the factory swaps the whole family.

  3. 03

    Code density is high — interviews rarely need full Abstract Factory; use for multi-tier configurations.

Java / Spring map

  • →

    Spring @Profile-based config is a runtime abstract factory of beans.

Code & diagrams

AbstractFactory.javajava

UI kit family — both products change together.

public interface Button { void render(); }
public interface Dialog { void open(); }

public interface UiKit {                 // abstract factory
  Button createButton();
  Dialog createDialog();
}

public class LightKit implements UiKit {
  public Button createButton() { return () -> System.out.println("[btn light]"); }
  public Dialog createDialog() { return () -> System.out.println("[dlg light]"); }
}
public class DarkKit implements UiKit {
  public Button createButton() { return () -> System.out.println("[btn dark]"); }
  public Dialog createDialog() { return () -> System.out.println("[dlg dark]"); }
}

// App boots with one kit:  UiKit kit = theme.equals("dark") ? new DarkKit() : new LightKit();

Explain without notes

01

What consistency guarantee does the family enforce?

Practice

01

Add a 'terminal' kit (green phosphor) with two more products.

Trade-offs

  • ↔

    Adding a new product type means editing every factory.

Completion checklist

  • I can explain why Abstract Factory guarantees family consistency and what it costs to add a new product type.

Back to phase