intermediateFactory Method Pattern

When is introducing a Factory pattern unnecessary, and what is the risk of overusing it?

A Factory earns its keep specifically when which concrete class to construct depends on some runtime condition, and that decision needs to be made consistently from more than one place in the codebase. If an application only ever constructs one concrete type, from exactly one place, a factory is pure unnecessary indirection — it adds a layer of abstraction with no corresponding benefit. Overusing it also has a real cost: even inside a legitimate factory, a switch statement handling many types can itself become unwieldy as the type count grows, though at least that growth is confined to one sanctioned location; for a very large number of types, some codebases replace the switch with a map from type identifier to constructor function to avoid editing the factory's method body for every new addition.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to How does the Builder pattern differ from the Factory pattern, given that both are considered creational patterns?← Back to all Low-Level Design & Design Patterns questions