C++ design pattern (factory method pattern)


Preface

This article will teach you the factory method pattern in C++.

1. Introduction to the factory method pattern

Factory method pattern is a creational design pattern that is used to create objects through factory methods without explicitly specifying their concrete classes. This pattern defines an interface that creates objects, but defers the specific object creation to its subclasses. This can separate the creation and use of objects and improve the flexibility and scalability of the code.

UML structure diagram:

Insert image description here

2. Comparison between factory method pattern and simple factory pattern

1. Implementation method:

Simple factory pattern: A factory class is responsible for creating objects, and determines which specific object to create based on the parameters or conditions passed in. It is equivalent to a factory class that centralizes all product creation logic.
Factory method pattern: Delay the creation of objects to subclasses. Each specific product has a corresponding factory class responsible for creating the product. Each factory class is only responsible for creating a specific product.

2. Flexibility and scalability:

Simple factory mode: through a factory class to centrally manage the creation of objects, the code of the factory class needs to be modified when adding new products, which does not conform to the principle of opening and closing (open for extension, closed for modification).
Factory method mode: Each specific product has a corresponding factory class. When adding a new product, you only need to add a new specific product and the corresponding factory class, without modifying the existing code. Comply with the opening and closing principle.

3. Coupling degree:

Simple factory pattern: The client code needs to rely on the factory class and create instances of specific products through the factory class.
Factory method pattern: Client code relies on abstract factories and abstract product classes, and creates instances of specific products through abstract factories. Client code is decoupled from concrete factory classes and concrete product classes.

4. Number of classes:

Simple factory pattern: Only one factory class is needed to create all products, and the factory class may become very large.
Factory method pattern: Each specific product has a corresponding factory class. The number of classes is more than that of the simple factory pattern, but it can manage the creation of objects at a finer granularity.

3. Applicable Scenarios of Factory Method Pattern

1. The creation of objects needs to follow a specific interface or abstract class: The factory method pattern is suitable for creating a group of related objects that share a common interface or abstract class. By defining abstract factories and concrete factory classes, you can ensure that the objects created conform to the definition of the same interface or abstract class.

2. It is necessary to determine the specific object to be created through the subclass: the factory method mode delays the creation of the object to the subclass, and the subclass can choose which specific object to implement according to the specific needs. This allows you to dynamically add or switch specific object types without modifying the client code.

3. New products need to be added through extension: If new products need to be added to the system, the factory method pattern can provide an extension mechanism to make it easier to add new products. You only need to create new concrete product classes and corresponding concrete factory classes without modifying existing code.

4. It is necessary to decouple the coupling between client code and specific products: by introducing abstract factories and specific factory classes, the factory method pattern can decouple client code from the creation process of specific products. The client only needs to rely on the abstract factory and abstract product class, and does not need to directly create specific objects by itself, thus reducing the degree of coupling.

4. Factory method pattern sample code

#include <iostream>
#include <string>

// 抽象产品类
class Product {
    
    
public:
    virtual void use() const = 0;
};

// 具体产品类 A
class ConcreteProductA : public Product {
    
    
public:
    void use() const override {
    
    
        std::cout << "Using ConcreteProductA" << std::endl;
    }
};

// 具体产品类 B
class ConcreteProductB : public Product {
    
    
public:
    void use() const override {
    
    
        std::cout << "Using ConcreteProductB" << std::endl;
    }
};

// 抽象工厂类
class Factory {
    
    
public:
    virtual Product* createProduct() const = 0;
};

// 具体工厂类 A
class ConcreteFactoryA : public Factory {
    
    
public:
    Product* createProduct() const override {
    
    
        return new ConcreteProductA();
    }
};

// 具体工厂类 B
class ConcreteFactoryB : public Factory {
    
    
public:
    Product* createProduct() const override {
    
    
        return new ConcreteProductB();
    }
};

int main() {
    
    
    // 使用具体工厂类 A 创建产品对象
    Factory* factoryA = new ConcreteFactoryA();
    Product* productA = factoryA->createProduct();
    productA->use();

    delete productA;
    delete factoryA;

    // 使用具体工厂类 B 创建产品对象
    Factory* factoryB = new ConcreteFactoryB();
    Product* productB = factoryB->createProduct();
    productB->use();

    delete productB;
    delete factoryB;

    return 0;
}

Summarize

This article will explain it here.

Guess you like

Origin blog.csdn.net/m0_49476241/article/details/132482670