Wednesday, January 22, 2025
HomeProgrammingFactory method Design Pattern

Factory method Design Pattern

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:

  1. Product: Interface or abstract class that defines the type of object the Factory will create.
  2. ConcreteProduct: A class that implements the Product interface.
  3. Creator: An abstract class or interface that defines the factory method, which returns a Product.
  4. ConcreteCreator: A class that implements the Creator interface and instantiates concrete Products.
See also  What is PseudoCode?

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 and Bike are the ConcreteProduct classes that implement the Vehicle interface.
  • VehicleFactory is the Creator interface with the create_vehicle method.
  • CarFactory and BikeFactory are the ConcreteCreator classes that implement the create_vehicle method to create the corresponding vehicle type.
See also  What is Network Layer Protocols?

Benefits:

  1. 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.
  2. Single Responsibility Principle: The object creation logic is encapsulated within the factory classes, allowing for better organization and maintainability.
  3. Flexibility: It allows for flexibility in terms of which type of object is created without changing the client code.
See also  SQL - How do I interpret the Precision and Scale of a Number in the Context of a Database Column?

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.
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x