🏗️Factory Pattern
Also known as Factory Method, Factory Function, Factory
One function or class makes the objects. The callers need not know how.
Think of it like
Think of it like ordering a pizza. You say which kind, and the kitchen works out how to make it.
Example
Emma's app sends alerts by email, text or push. Her code asks the factory for the user's choice. It gets back the right sender.
Why it matters
It keeps the hard parts of making objects in one place. So new kinds are easy to add.
Definition
Common mistake
Key takeaways
- 1One place to make. All objects of a family.
- 2Ask by kind. Not by class name.
- 3Easy to extend. New kind, one change.