The Factory Method design pattern is a creational pattern that provides an interface for creating objects in a super class, but allows subclasses to alter the type of objects that will be created. It helps promote loose coupling in code by abstracting the object creation process.
Key Concepts:
- Creator (Factory Method): A class that declares the method responsible for creating the objects, but leaves the actual instantiation to the subclasses.
- Product: The object created by the Factory Method. It can be a concrete class or an interface that defines the object type.
Components:
- Product: Interface or abstract class that defines the type of object the Factory will create.
- ConcreteProduct: A class that implements the Product interface.
- Creator: An abstract class or interface that defines the factory method, which returns a Product.
- ConcreteCreator: A class that implements the Creator interface and instantiates concrete Products.
Example:
Let’s consider a scenario where we have different types of vehicles. We want to create vehicles based on a given type, but the exact type of vehicle should be decided at runtime.
# Product
class Vehicle:
def create(self):
pass
# ConcreteProduct
class Car(Vehicle):
def create(self):
return "Car Created"
class Bike(Vehicle):
def create(self):
return "Bike Created"
# Creator
class VehicleFactory:
def create_vehicle(self):
pass
# ConcreteCreator
class CarFactory(VehicleFactory):
def create_vehicle(self):
return Car()
class BikeFactory(VehicleFactory):
def create_vehicle(self):
return Bike()
# Client code
def get_vehicle(factory: VehicleFactory):
vehicle = factory.create_vehicle()
print(vehicle.create())
car_factory = CarFactory()
bike_factory = BikeFactory()
get_vehicle(car_factory) # Output: Car Created
get_vehicle(bike_factory) # Output: Bike Created
Explanation:
Vehicle
is the Product interface.Car
andBike
are the ConcreteProduct classes that implement theVehicle
interface.VehicleFactory
is the Creator interface with thecreate_vehicle
method.CarFactory
andBikeFactory
are the ConcreteCreator classes that implement thecreate_vehicle
method to create the corresponding vehicle type.
Benefits:
- Loose Coupling: The client doesn’t need to know the specific class of the object being created. It relies on the factory to return an object of the appropriate type.
- Single Responsibility Principle: The object creation logic is encapsulated within the factory classes, allowing for better organization and maintainability.
- Flexibility: It allows for flexibility in terms of which type of object is created without changing the client code.
When to Use:
- When the exact type of object to create isn’t known until runtime.
- When the creation process is complex or involves many steps that need to be isolated from the client code.
- When you want to provide a central point for object creation that can be modified or extended without affecting the rest of the codebase.