Python Factory Method Design Pattern

1. Definition

The Factory Method Design Pattern defines an interface for creating an object but allows subclasses to alter the type of objects that will be created. In other words, the Factory Method lets a class delegate the responsibility of instantiating its objects to its subclasses.

2. Problem Statement

Consider a scenario where you're designing a user interface library. You want to provide a way to create buttons, but the exact appearance and behavior of the buttons should be customizable for different operating systems or themes.

3. Solution

The Factory Method Pattern allows defining a method for creating an object, but it's the responsibility of the derived class to implement this method and decide which class to instantiate. It provides flexibility and promotes loose coupling between classes.

4. Real-World Use Cases

1. GUI libraries where each OS provides a different implementation of a button, scrollbar, or window.

2. Payment gateway integration where each provider has its own method of processing payments.

5. Implementation Steps

1. Define an interface with a factory method.

2. Let concrete classes implement this factory method and decide which class to instantiate.

6. Implementation in Python

from abc import ABC, abstractmethod
# Step 1: Creator class
class ButtonFactory(ABC):
    @abstractmethod
    def create_button(self):
        pass
# Concrete Creator
class WindowsButtonFactory(ButtonFactory):
    def create_button(self):
        return WindowsButton()
# Concrete Creator
class LinuxButtonFactory(ButtonFactory):
    def create_button(self):
        return LinuxButton()
# Product interface
class Button(ABC):
    @abstractmethod
    def render(self):
        pass
# Concrete Product
class WindowsButton(Button):
    def render(self):
        return "Windows Button Rendered"
# Concrete Product
class LinuxButton(Button):
    def render(self):
        return "Linux Button Rendered"
# Client Code
factory = WindowsButtonFactory()
button = factory.create_button()
print(button.render())

Output:

Windows Button Rendered

Explanation:

1. We start by defining an abstract class ButtonFactory which declares an abstract factory method create_button.

2. WindowsButtonFactory and LinuxButtonFactory are concrete implementations of ButtonFactory. They implement the create_button method and decide which button to instantiate.

3. The Button abstract class represents the product interface, with a method render.

4. WindowsButton and LinuxButton are concrete implementations of the Button interface, providing their render logic.

5. In the client code, we use WindowsButtonFactory to create a Windows button and then render it.

7. When to use?

Use the Factory Method Pattern when:

1. A class cannot anticipate the type of objects it is supposed to create.

2. When subclasses are responsible for instantiating their objects.

3. To promote loose coupling by avoiding direct object creation in the application and instead delegating this responsibility to factory methods.


Comments