Creational patterns
Control how objects are created — decouple clients from concrete classes and manage instance lifecycles.
Singleton
The Singleton ensures a class has only one instance and provides a single point of access to it. Common uses: configuration managers, logging services, connection pools, caches.
The classic implementation hides the constructor (private), stores the sole instance in a static field, and exposes it through a static method like `getInstance()`. Modern languages often prefer module-level exports or dependency injection containers over hand-rolled singletons.
What is the main criticism of the Singleton pattern in modern software design?
Factory Method
The Factory Method defines an interface for creating an object but lets subclasses decide which class to instantiate. The creator class doesn't know the concrete product type — it delegates that decision.
This decouples the client from concrete classes. Adding a new product type means adding a new subclass, not modifying existing code (Open/Closed Principle).
In the Factory Method pattern, who decides which concrete product to instantiate?
Abstract Factory
The Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes. Think of it as a factory of factories — each concrete factory produces a consistent set of products.
Classic example: a UI toolkit that must produce buttons, text fields, and checkboxes for either Windows or macOS. The client codes against the abstract factory interface; swapping themes means swapping the factory instance.
| Aspect | Factory Method | Abstract Factory |
|---|---|---|
| Creates | One product | Family of related products |
| Mechanism | Subclass overrides method | Client receives factory object |
| Variation axis | Per-subclass | Per-concrete-factory |
| Complexity | Lower | Higher (more interfaces) |
What distinguishes Abstract Factory from Factory Method?