Factory Design Pattern Explained
The Factory pattern delegates the creation of objects to a dedicated method or class, instead of calling a constructor directly, so calling code doesn't need...
Published October 6, 2026
The Factory pattern delegates the creation of objects to a dedicated method or class, instead of calling a constructor directly, so calling code doesn't need to know exactly which concrete class to instantiate.
Why it exists
- Sometimes the exact type of object needed depends on runtime conditions, like configuration or user input
- Hardcoding a specific class's constructor everywhere it's needed makes it hard to change or extend which types exist later
How to apply it
- Define a factory method that takes some input (like a type string) and returns the appropriate object
- Use it when object creation involves meaningful logic or when the concrete class needed varies at runtime
FAQ
Isn't this just calling "new" somewhere else?
The point isn't to avoid "new" entirely, but to centralize the decision of which class to instantiate in one place, so callers depend on an abstraction rather than a specific concrete class.