The Factory Method Design Pattern
The Factory Method pattern defines an interface for creating an object but allows subclasses to alter the type of objects that will be created.
Use the Factory Method pattern when you want to provide a method for creating an object, but allow subclasses to alter the type of created objects. This is particularly useful when a class can’t anticipate the class of objects it must create.
- TypeCreational
- Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
- Efficiency7
-
Learning Effort
- Average5
- Conceptual5
- Debug and Maintain5
- Implementation4
- Prerequisites4
- Versatility6
Things you should know before you start
- Polymorphism Understanding of polymorphism in object-oriented programming.
- Inheritance Understanding of inheritance and how subclasses work.
Minimal Example
Creating different types of payment methods in an e-commerce application.
interface PaymentMethod { processPayment(amount: number): void; } class CreditCardPayment implements PaymentMethod { processPayment(amount: number) { console.log(`Processing credit card payment of $${amount}`); } } const payment: PaymentMethod = PaymentFactory.createPayment('CreditCard'); payment.processPayment(100);
What’s happening in thie example
In this example, the PaymentFactory is the Creator class that defines a factory method for creating objects implementing the PaymentMethod interface. The `clientCode` demonstrates using the factory method to create a CreditCardPayment object.
How the Factory Method Works
The Factory Method pattern involves a method for creating an object in a super class but allowing a subclass to alter the type of objects that will be created.
The pattern allows a class to delegate responsibility to one of its subclasses.
Entities in the Factory Method Design Pattern
-
CreatorThe Creator class declares the factory method that returns new product objects.
- Creator has a one-to-many instantiates relationship with Product
-
ProductThe Product class is the interface for objects the factory method creates.
- ConcreteProduct has a one-to-one implements relationship with Product
What to watch out for when using or considering this design pattern.
Avoid using the Factory Method pattern when the class hierarchy is not conducive to subclassing or when the factory method would add unnecessary complexity.
Design patterns often confused with the Factory Method Design Pattern
The following design patterns are often confused with the Factory Method
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
Abstract Factory – The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes. It often involves multiple Factory Methods, one for each type of object to be created.
The Abstract Factory pattern is similar to the Factory Method in the following ways.
- Both are creational design patterns.
- Both encapsulate object creation.
- Abstract Factory creates families of related objects, while Factory Method deals with the instantiation of a single object.
- Factory Method relies on inheritance and is usually implemented with a single method, while Abstract Factory often involves multiple methods.
-
Singleton – The Singleton pattern ensures that a class has only one instance and provides a global point to access it.
The Singleton pattern is similar to the Factory Method in the following ways.
- Both are creational design patterns.
- Both provide controlled access to object creation.
- Singleton restricts object creation to one instance, while Factory Method allows multiple instances but controls the creation process.
- Factory Method is more flexible and extensible as it allows subclasses to decide the type of object to create, whereas Singleton does not.