Factory Pattern Explained
Encapsulate object creation behind a clean interface — letting calling code request objects without knowing or depending on their concrete classes.
Factory Pattern
The Factory pattern is a creational design pattern that provides an interface for creating objects without specifying their exact classes, delegating instantiation logic to factory methods or classes.
Explanation
The Factory pattern encapsulates object creation logic, allowing the calling code to request objects by type or configuration without knowing the concrete class being instantiated. This is useful when the creation process is complex, when different implementations share a common interface, or when the specific class depends on runtime conditions. There are two main variants: Factory Method (a method in a base class that subclasses override to create different objects) and Abstract Factory (a class that creates families of related objects without specifying concrete classes). Both decouple the code that uses objects from the code that creates them. Real-world examples include database connection factories (creating MySQL, PostgreSQL, or SQLite connections from a config string), UI component factories (creating platform-specific buttons and dialogs), and parser factories (creating JSON, XML, or CSV parsers based on file extension).
Bookuvai Implementation
Bookuvai uses Factory patterns for database connection management, payment processor integration (creating Stripe, PayPal, or custom processor instances from configuration), and notification service creation (email, SMS, push). Factories centralize creation logic and make adding new implementations straightforward.
Key Facts
- Encapsulates object creation without exposing concrete classes
- Factory Method: subclasses override creation in a base class
- Abstract Factory: creates families of related objects
- Decouples object usage from object creation logic
- Enables adding new implementations without modifying existing code
Related Terms
Frequently Asked Questions
- When should I use a Factory instead of calling new directly?
- Use a Factory when creation logic is complex, when multiple implementations share an interface, or when the concrete class depends on runtime configuration. For simple objects with straightforward construction, calling new directly is fine.
- What is the difference between Factory Method and Abstract Factory?
- Factory Method uses a single method (often in a base class) to create one product. Abstract Factory uses a class with multiple methods to create families of related products. Use Abstract Factory when you need to create multiple related objects that must be compatible.
- How does Factory relate to dependency injection?
- Factories and DI both decouple creation from usage, but DI takes it further: the caller does not even choose the implementation. With a Factory, the caller requests a specific type; with DI, the framework provides the implementation based on configuration.