Factory Pattern
The Factory pattern is a creational design pattern that delegates object creation to a dedicated method or class, so calling code can obtain new instances without knowing the concrete class being instantiated.
Definition
The Factory pattern is a creational design pattern that delegates object creation to a dedicated method or class, so calling code can obtain new instances without knowing the concrete class being instantiated.
Overview
The Factory pattern family, catalogued in the Gang of Four's Design Patterns, addresses a recurring problem: code that directly instantiates concrete classes with `new` becomes tightly coupled to those specific classes, making it hard to swap implementations, extend the set of creatable types, or test in isolation. The pattern's core idea is to introduce a layer of indirection — a 'factory' — that encapsulates the logic for deciding which concrete class to instantiate and how to construct it, exposing to callers only an abstract interface or base type. The pattern appears in a few related forms. The simple factory (sometimes not considered a formal GoF pattern on its own) is a single method or static function that takes some input and returns an appropriate object type. The Factory Method pattern defines an abstract method in a base class that subclasses override to specify which concrete class to instantiate, letting subclassing decide the product type. The Abstract Factory pattern goes further, defining an interface for creating families of related objects without specifying their concrete classes, useful when a system needs to remain consistent across multiple related products (for example, UI toolkits that must produce buttons, checkboxes, and menus consistent with one visual theme). Factories are pervasive in real-world software: framework and library code frequently exposes factory functions rather than requiring consumers to construct objects directly, because this lets the framework control instantiation details (pooling, caching, validation, dependency wiring) and evolve internal implementations without breaking callers. The pattern is also foundational to many dependency-injection containers, which are essentially generalized factories configured to resolve and construct object graphs. The tradeoff is added indirection and more classes/interfaces, which can be overkill for simple object creation where a plain constructor call is perfectly clear.
Key Concepts
- Encapsulates object-creation logic behind a method or dedicated class
- Decouples calling code from concrete class names, working through interfaces or base types instead
- Factory Method variant: subclasses override a creation method to decide what to instantiate
- Abstract Factory variant: creates families of related objects consistently
- Simplifies swapping implementations without changing calling code
- Centralizes construction logic (validation, caching, pooling, configuration)
- Underpins most dependency-injection containers
- Adds a layer of indirection that can be unnecessary for trivial object creation