Provide an interface for creating families of related objects without specifying their concrete classes. You need consistent object families (for
Abstract Factory
Overview
Provide an interface for creating families of related objects without specifying their concrete classes.
When to use
- You need consistent object families (for example, UI theme widgets).
- You want to switch product families at runtime.
Java example
TypeScript example
Pros and cons
Pros:
- Ensures consistent product families.
- Swappable factories improve flexibility.
Cons:
- More interfaces and classes.
- Harder to add new product types.
Common pitfalls
- Mixing products from different families.
- Introducing too many factory layers.