Skip to content

Design Patterns: Factory Pattern

Published: at 07:20 PM

The Factory pattern is a design pattern that falls under the category of creational patterns. It is used to create objects without exposing the logic of the object creation to the client. The Factory pattern provides a way to encapsulate the creation of objects and allows the client to use the objects without knowing how they were created. In this blog post, we will explore the Factory pattern and its implementation in Python.

Overview of the Factory pattern

The Factory pattern is a design pattern that provides an interface for creating objects. It encapsulates the object creation logic and allows the client to use the objects without knowing the implementation details. The Factory pattern is useful when there are multiple objects that need to be created and the creation process is complex or requires specific conditions to be met.

The Factory pattern consists of four main components:

  1. The Creator: This is an abstract class that provides an interface for creating objects. It declares the factory method that returns an object of the ConcreteProduct class.

  2. The ConcreteCreator: This is a subclass of the Creator class. It overrides the factory method and provides its implementation of creating an object.

  3. The Product: This is an abstract class that defines the interface for the objects that are created by the Creator. It contains the common behavior of all the objects that the Creator can create.

  4. The ConcreteProduct: This is a subclass of the Product class. It provides the implementation of the behavior defined in the Product class.

Implementation of the Factory pattern in Python

In Python, the Factory pattern can be implemented using classes and their constructors. Let’s take an example of a car manufacturing company that produces different types of cars. We can implement the Factory pattern to create different types of cars without exposing the implementation details to the client.

First, we will define the abstract Product class that provides an interface for common behaviors of the Car objects.

from abc import ABC, abstractmethod

class Car(ABC):
    @abstractmethod
    def get_car_type(self):
        pass

Next, we will define the ConcreteProduct classes that implement the Car interface.

class Sedan(Car):
    def get_car_type(self):
        return "Sedan"

class SUV(Car):
    def get_car_type(self):
        return "SUV"

Now, we will define the Creator class along with the ConcreteCreator class that overrides the factory method and provides the implementation for creating the Car objects.

class Factory(ABC):
    @abstractmethod
    def create_car(self, car_type):
        pass

class CarFactory(Factory):
    def create_car(self, car_type):
        if car_type == "Sedan":
            return Sedan()
        elif car_type == "SUV":
            return SUV()
        else:
            return None

Finally, we can use the CarFactory class to create different types of cars without knowing the implementation details.

factory = CarFactory()

sedan = factory.create_car("Sedan")
suv = factory.create_car("SUV")

print(sedan.get_car_type())  # Sedan
print(suv.get_car_type())  # SUV

Advantages of the Factory pattern

The Factory pattern allows us to encapsulate the creation logic of objects and provides a way to create objects without exposing the implementation details to the client.

The Factory pattern makes the code more modular and easier to maintain. If we need to add a new type of car, we can simply add a new class and modify the factory method, without affecting the rest of the code.

The Factory pattern promotes loose coupling between the client and the objects that it uses. The client only needs to know the interface of the objects and does not need to know the specific implementation details.

Disadvantages of the Factory pattern

The Factory pattern can increase the complexity of the code, especially if there are many ConcreteProduct classes. The Factory pattern can introduce overhead due to the additional classes and interfaces that need to be defined.

The Factory pattern can lead to a large number of subclasses if there are many variations of the object that need to be created.

When to use the Factory pattern

The Factory pattern is useful when:

Conclusion

In this blog post, we explored the Factory pattern and its implementation in Python. We learned that the Factory pattern provides a way to encapsulate the creation logic of objects and allows the client to use the objects without knowing the implementation details. We also discussed the advantages and disadvantages of using the Factory pattern and when it is appropriate to use it. The Factory pattern is a powerful design pattern that can make code more modular, easier to maintain, and promote loose coupling between objects.