Factory Design Pattern

Personal Object Assembly Line

In a nutshell, the Factory Design Pattern acts as your personal object assembly line, adapting to your needs and delivering the objects you need, right when you need them.

Review

  1. Client orders: You tell the factory what kind of object you need, providing any necessary specifications.
  2. Factory delegation: The factory analyzes your request and chooses the appropriate concrete creator based on specifications.
  3. Concrete creator builds: The chosen craftsperson utilizes their skills and tools to build the object according to your specs.
  4. Product delivery: The factory then presents you with the final, completed object.

Benefits

Example

class Product:
    def use(self):
        raise NotImplementedError("Subclasses must implement the use() method")

class ConcreteProduct1(Product):
    def use(self):
        print("Using Product 1")

class ConcreteProduct2(Product):
    def use(self):
        print("Using Product 2")

class Factory:
    @staticmethod
    def create_product(product_type):
        if product_type == "1":
            return ConcreteProduct1()
        elif product_type == "2":
            return ConcreteProduct2()
        else:
            return None

# Client code
client = Factory.create_product("1")  # Client doesn't know concrete product types
client.use()  # Output: Using Product 1

client = Factory.create_product("2")
client.use()  # Output: Using Product 2

Remember