design patterns · creational
Abstract Factory
Provides an interface for creating families of related objects without specifying their concrete classes.
Pattern
Overview
Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Real-World Analogy
IKEA sells furniture families - you buy all pieces from the same collection to ensure they match, without knowing which factory produced each.
When you need to create families of related objects (e.g., Windows Button + Windows Checkbox, or Mac Button + Mac Checkbox) and ensure they are always used together, Abstract Factory provides a single interface that guarantees consistency.
An abstract factory interface declares create methods for each product type. Concrete factories implement these for a specific family. The client uses only the abstract factory interface - it never references concrete products directly.
When to use
Cross-platform UI toolkits, database drivers (MySQL vs PostgreSQL), any scenario requiring consistent families of related objects.
When not to
When you only have one product type. When product families are unlikely to change - the complexity is not justified.
Participants
Key Insights
- Abstract Factory is a factory of factories - each concrete factory is itself a Factory Method collection
- The client is completely decoupled from concrete product classes
- Switching product families requires only replacing the concrete factory - one change point
- Extension cost: adding a new product type forces changes in every concrete factory