intermediate
Factory Method
Move variant selection into a factory operation so callers depend on a product contract rather than concrete construction branches.
Factory Method lets subclasses or dedicated creators choose which product to instantiate while callers depend on a shared interface. It removes sprawling `if/else` construction logic from business workflows and localizes the impact when a new variant appears. It differs from Simple Factory by keeping creation extensible through polymorphism.
On interviews: contrast Factory Method with direct `new` calls and with Abstract Factory families. Give a payment processor or notification channel example.
Common pitfalls: factories that know too many concrete types; returning concrete classes when callers need a stable interface; untestable static factory methods.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Callers depend on product interface, not concrete class.
- Variant selection lives in one factory hierarchy.
- New variants add a class, not scattered conditionals.
- Tests inject factories to control product behavior.