[파이썬] 추상 팩토리 패턴과 객체 패밀리

The 추상 팩토리 패턴 (Abstract Factory Pattern) is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern encapsulates the creation of objects within a factory, which is responsible for instantiating different types of related objects.

In Python, the abstract factory pattern can be implemented using classes and interfaces. The factory class acts as an interface for creating objects, while the concrete factories implement this interface and are responsible for creating specific objects.

On the other hand, 객체 패밀리 (Object Family) refers to a group of objects that have different characteristics but belong to the same category or have a common purpose. These objects are related or dependent on each other and work together to achieve a specific goal.

Let’s have a look at an example of how to implement the abstract factory pattern and create an object family in Python:

# Abstract Factory Interface
class AbstractFactory:
    def create_product_a(self):
        pass

    def create_product_b(self):
        pass

# Concrete Factory 1
class ConcreteFactory1(AbstractFactory):
    def create_product_a(self):
        return ProductA1()

    def create_product_b(self):
        return ProductB1()

# Concrete Factory 2
class ConcreteFactory2(AbstractFactory):
    def create_product_a(self):
        return ProductA2()

    def create_product_b(self):
        return ProductB2()

# Abstract Product A
class AbstractProductA:
    def operation_a(self):
        pass

# Concrete Product A1
class ProductA1(AbstractProductA):
    def operation_a(self):
        print("Product A1 operation")

# Concrete Product A2
class ProductA2(AbstractProductA):
    def operation_a(self):
        print("Product A2 operation")

# Abstract Product B
class AbstractProductB:
    def operation_b(self):
        pass

# Concrete Product B1
class ProductB1(AbstractProductB):
    def operation_b(self):
        print("Product B1 operation")

# Concrete Product B2
class ProductB2(AbstractProductB):
    def operation_b(self):
        print("Product B2 operation")


# Client Code
def main(factory: AbstractFactory):
    product_a = factory.create_product_a()
    product_b = factory.create_product_b()

    product_a.operation_a()
    product_b.operation_b()


# Using Concrete Factory 1
factory_1 = ConcreteFactory1()
main(factory_1)

# Using Concrete Factory 2
factory_2 = ConcreteFactory2()
main(factory_2)

In the above example, we define an abstract factory interface that provides methods for creating products of types A and B. We then implement two concrete factories, ConcreteFactory1 and ConcreteFactory2, which each create a specific set of products (A and B).

The abstract product classes are defined for products of type A and B, and their concrete implementations are also provided.

In the client code section, we can see how we can create objects from a specific factory and use them without knowing their concrete types. This allows us to create families of related objects without tightly coupling our code to their implementations.

The abstract factory pattern and object family concept are powerful tools in software design, as they promote loose coupling and allow for easy extensibility in creating different variants of related objects.

By utilizing these patterns, developers can write more modular and maintainable code, making it easier to add new functionality or switch between different implementations of related objects in the future.