The Factory method pattern is a creational design pattern in object‑oriented software engineering that defines an interface for creating an object, but allows subclasses to alter the type of objects that will be created. The pattern encapsulates object‑creation logic within a dedicated method, thereby decoupling client code from concrete classes and promoting extensibility.
Intent
Provide a way to delegate the instantiation of concrete classes to subclasses, enabling a program to select the appropriate concrete type at runtime without referring directly to those types.
Motivation
Consider a system that must create objects belonging to a family of related classes (e.g., different types of documents, GUI widgets, or database connections). Directly invoking constructors ties the client code to specific classes, making changes difficult and violating the Open/Closed Principle. The Factory method isolates the creation process, allowing the system to evolve by adding new concrete products without modifying existing client code.
Key Participants
| Participant | Responsibility |
|---|---|
| Product | Defines the interface of objects the factory method creates. |
| ConcreteProduct | Implements the Product interface. |
| Creator (or Factory) | Declares the factory method, which returns a Product object. May provide a default implementation that returns a generic Product or may be abstract. |
| ConcreteCreator | Overrides the factory method to return an instance of a ConcreteProduct. |
Structure
A typical UML representation includes:
- An abstract class Creator with a method
factoryMethod(): Product. - Subclasses of Creator (e.g.,
ConcreteCreatorA,ConcreteCreatorB) that implementfactoryMethodto instantiateConcreteProductA,ConcreteProductB, respectively. - The Client works with objects through the Product interface and invokes the factory method via a Creator reference.
Implementation Steps
- Identify the product hierarchy and define a common abstract product type.
- Declare an abstract creator class containing the factory method that returns the abstract product.
- Implement concrete creator subclasses that override the factory method to instantiate specific concrete products.
- Use the creator (often through polymorphic references) in client code to obtain product instances.
Advantages
- Encapsulation of creation: Object‑creation code is isolated from business logic.
- Reduced coupling: Clients depend on abstract product types rather than concrete classes.
- Extensibility: New concrete products can be added by creating new concrete creators, without altering existing client code.
- Adherence to SOLID principles: Particularly the Open/Closed Principle and Dependency Inversion Principle.
Disadvantages
- May increase the number of classes in a system, leading to a more complex class hierarchy.
- If the factory method simply returns a new instance of a concrete class without additional logic, the pattern can be considered over‑engineering.
Typical Use Cases
- When a class cannot anticipate the type of objects it must create.
- When a class wants its subclasses to specify the concrete product to create.
- When object creation involves complex setup, configuration, or conditional logic that should be abstracted away from client code.
Variants and Related Patterns
- Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes. The Factory method can be viewed as a single‑product specialization of the Abstract Factory.
- Prototype: Creates new objects by copying an existing prototype instance rather than instantiating a class directly.
- Builder: Separates the construction of a complex object from its representation, focusing on stepwise assembly rather than selection of concrete types.
Example (simplified Java)
// Product
public interface Button {
void render();
void onClick();
}
// Concrete Products
public class WindowsButton implements Button { … }
public class LinuxButton implements Button { … }
// Creator
public abstract class Dialog {
public void renderWindow() {
Button okButton = createButton(); // Factory method
okButton.render();
}
protected abstract Button createButton(); // Factory method signature
}
// Concrete Creators
public class WindowsDialog extends Dialog {
@Override protected Button createButton() {
return new WindowsButton();
}
}
public class LinuxDialog extends Dialog {
@Override protected Button createButton() {
return new LinuxButton();
}
}
A client can work with Dialog references and obtain platform‑specific Button objects without knowing the concrete classes.
History
The Factory method pattern was popularized by the "Gang of Four" (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) in their 1994 book Design Patterns: Elements of Reusable Object‑Oriented Software. It draws on earlier concepts in class libraries and framework design that sought to isolate construction logic.
References
- Gamma, E.; Helm, R.; Johnson, R.; Vlissides, J. Design Patterns: Elements of Reusable Object‑Oriented Software. Addison‑Wesley, 1994.
- Fowler, M. Patterns of Enterprise Application Architecture. Addison‑Wesley, 2002.
- Larman, C. Applying UML and Patterns: An Introduction to Object‑Oriented Analysis and Design. Prentice Hall, 2004.
See also
- Abstract Factory pattern
- Prototype pattern
- Builder pattern
- Dependency Injection
- SOLID principles (Open/Closed Principle, Dependency Inversion Principle)