Cover photo credits to: Refactoring GURU my go to website for design patterns!
Before we start going deep in the factory method pattern, let's first define it. The Factory Method pattern is a creational design pattern that provides a way to create objects without specifying the exact class of object that will be created.
Problems
Imagine you're building an insurance application that currently offers Life Insurance. As the application gains popularity, users start requesting additional types of insurance, such as Death Insurance. However, the existing codebase is tightly coupled to the Life Insurance class, making it difficult to add new insurance types without significant changes. For instance, you might need to add conditional statements or switch statements to determine which insurance type to use, leading to a complex and inflexible codebase. This approach not only makes it harder to maintain and extend the application but also limits its ability to adapt to changing user needs. A better approach would be to decouple the insurance types from the application's core logic, allowing you to add new insurance types without modifying the existing codebase.
Solution
The Factory Method pattern enables you to decouple object creation from the specific class of object being created. Instead of directly instantiating objects using the new operator, you can use a factory method that returns the desired object. This abstraction allows you to encapsulate the object creation process within the factory method, making it easier to change or extend the creation process without affecting the rest of the code. The factory method returns an object, often referred to as a 'product', which can be an instance of a specific class or a subclass of a base class. By using the Factory Method pattern, you can create a more flexible and maintainable codebase that is less coupled to specific object implementations.
Key Components
- Product - Interface or abstract class that represents the objects that will be created by the factory
- Concrete product - The specific implementation of the product interface
- Factory/Concrete creator - The class responsible for creating objects that implement the product interface. It abstracts the object creation process from the client code
Code Example
interface Insurance {
// Product
createInsurance();
}
class LifeInsurance implements Insurance {
// Concrete Product
createInsurance() {
// Apply creation logic
console.log("Life Insurance Created");
}
}
class DeathInsurance implements Insurance {
// Concrete Product
createInsurance() {
// Apply creation logic
console.log("Death Insurance Created");
}
}
class InsuranceFactory {
// Factory
getInsurance(insuranceType) {
switch (insuranceType) {
case "LIFE":
return new LifeInsurance();
case "DEATH":
return new DeathInsurance();
default:
throw new Error("Insurance type not found");
}
}
function main() {
const insurance = new InsuranceFactory();
insurance.getInsurance("LIFE").createInsurance();
insurance.getInsurance("DEATH").createInsurance();
}
main();
Top comments (0)