Three attitudes of the factory model?

insert image description here

A common design pattern in software engineering - the factory pattern. The factory pattern is a powerful tool for creating object instances that can help us better organize code and reduce coupling. In this article, I will introduce the three attitudes of the factory model in detail, and at the same time use examples and code demonstrations to help you better understand.

Three postures of the factory model

The factory pattern is a creational design pattern, which is used to solve the decoupling of object creation process and client code. It has three common postures, namely simple factory pattern, factory method pattern and abstract factory pattern. Let's understand each of these gestures one by one.

1. Simple factory pattern

The simple factory pattern is also known as the static factory pattern, which creates object instances through a factory class. The client obtains the required object through the static method of the factory class without directly instantiating the object.

class Product:
    def operation(self):
        pass

class ConcreteProductA(Product):
    def operation(self):
        return "ConcreteProductA operation"

class ConcreteProductB(Product):
    def operation(self):
        return "ConcreteProductB operation"

class SimpleFactory:
    def create_product(self, product_type):
        if product_type == "A":
            return ConcreteProductA()
        elif product_type == "B":
            return ConcreteProductB()

# 在客户端使用
factory = SimpleFactory()
product_a = factory.create_product("A")
print(product_a.operation())  # 输出:ConcreteProductA operation

2. Factory method pattern

The factory method pattern defines an abstract method for creating objects, and lets subclasses decide what type of object to actually create. Each specific product has a corresponding factory, thus realizing the decoupling of object creation and use.

class Creator:
    def factory_method(self):
        pass

class ConcreteCreatorA(Creator):
    def factory_method(self):
        return ConcreteProductA()

class ConcreteCreatorB(Creator):
    def factory_method(self):
        return ConcreteProductB()

# 在客户端使用
creator_a = ConcreteCreatorA()
product_a = creator_a.factory_method()
print(product_a.operation())  # 输出:ConcreteProductA operation

3. Abstract factory pattern

The Abstract Factory pattern provides an interface for creating a series of related or interdependent objects without specifying concrete classes. Through the abstract factory, the client can create a group of related products, so as to achieve a higher level of abstraction.

class AbstractFactory:
    def create_product_a(self):
        pass

    def create_product_b(self):
        pass

class ConcreteFactory1(AbstractFactory):
    def create_product_a(self):
        return ConcreteProductA()

    def create_product_b(self):
        return ConcreteProductB()

class ConcreteFactory2(AbstractFactory):
    def create_product_a(self):
        return AnotherConcreteProductA()

    def create_product_b(self):
        return AnotherConcreteProductB()

# 在客户端使用
factory_1 = ConcreteFactory1()
product_a = factory_1.create_product_a()
print(product_a.operation())  # 输出:ConcreteProductA operation

Examples and Case Studies

Example: Order Processing System
In this example, we will use the factory pattern to create different types of order objects: normal order and discount order.

Step 1: Define the order class
First, we define the base class and two subclasses of the order, representing ordinary orders and discount orders respectively. Each order type has different properties and handling.

class Order:
    def process(self):
        pass

class RegularOrder(Order):
    def process(self):
        return "Processing a regular order"

class DiscountOrder(Order):
    def process(self):
        return "Processing a discount order"

Step 2: Create an order factory
Next, we create an order factory and return the corresponding order object according to the order type.

class OrderFactory:
    def create_order(self, order_type):
        if order_type == "regular":
            return RegularOrder()
        elif order_type == "discount":
            return DiscountOrder()

Step 3: Client code
Finally, in the client code, you can use the order factory to create an order object and perform corresponding processing operations.

factory = OrderFactory()

order_type = "regular"
order = factory.create_order(order_type)
print(order.process())  # 输出:Processing a regular order

order_type = "discount"
order = factory.create_order(order_type)
print(order.process())  # 输出:Processing a discount order

case analysis

In this case, we created an order processing system using the factory pattern. Through the order factory, we can flexibly create different kinds of order objects according to the order type without instantiating them directly in the client code.
In actual development, the order type can also be further extended to add more order attributes and processing logic. Factory pattern will help you keep your code clean and readable.

epilogue

As a common design pattern in software engineering, the factory pattern provides us with a flexible and decoupled way to create objects. Through the explanation, case analysis and possible code demonstration of this article, I believe you have a clearer understanding of the three postures of the factory pattern. If you have more questions about the factory model, or want to share your practical application experience, please leave a message in the comment area, thank you for your reading and support!

insert image description here

Guess you like

Origin blog.csdn.net/m0_53918860/article/details/132383996