design patterns ·
Abstract Factory
Provides an interface for creating families of related objects without specifying their concrete classes.
Theory
Explanation
Intuition first, formal definition second. Skim the bullets if you already know this; read the prose if you don't.
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.
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