C + + design pattern factory pattern

Reprinted from: https://www.cnblogs.com/xiaolincoding/p/11524376.html

What is the factory model?

This type of design pattern is a creation pattern, which provides the best way to create objects.
In factory mode,When creating an object, we do not expose the creation logic to the client, and point to the newly created object by using a common interface.

Simply put, using the C + + polymorphism feature, create the corresponding subclass (derived class) object of the class with inheritance relationship through a factory class. When the project is complex, it can facilitate the creation of subclass objects.

The implementation of factory mode can be divided into simple factory mode, factory method mode and abstract factory mode. Each implementation mode has advantages and disadvantages.

Recently, the speculation of shoes is very hot. Then, in the form of shoe factory, analyze one by one, and analyze each implementation mode.

Simple factory mode

Specific circumstances:
  • Shoe factories can designate the production of Nike, Adidas and Li Ning brand shoes. The boss will produce whichever shoes are hot, depending on the situation.
UML diagram:

Structural composition of simple factory mode:
  1. Shoesfactory: the core class of the factory pattern, which defines an interface for creating a specific instance object.
  2. Abstract product class (Shoes): it is the inherited parent class or implemented interface of a specific product class.
  3. Specific product class (NiKeShoes\AdidasShoes\LiNingShoes): the object created by the factory class is this specific product instance.
Features of simple factory mode:
  • The factory class encapsulates the function of creating a specific product object.
Defects of simple factory mode:
  • The extensibility is very poor. When adding new products, you need to modify the factory class.
Code for simple factory mode:
  • Shoes is the abstract class (base class) of shoes, and the interface function is Show(), which is used to display shoe advertisements.
  • NiKeShoes, AdidasShoes and LiNingShoes are specific shoe classes. They are Nike, Adidas and Li Ning Shoes respectively. They all inherit from the Shoes abstract class# include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
// Shoe abstract class
class Shoes
{
public:
    virtual ~Shoes() {}
    virtual void Show() = 0;
};

// Nike shoes
class NiKeShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm nike shoes. My slogan: Just do it" << std::endl;
    }
};

// Adidas shoes
class AdidasShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm adidas shoes, my slogan:Impossible is nothing" << std::endl;
    }
};

// Li Ning shoes
class LiNingShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm Li Ning sneakers. My slogan is: Everything is possible" << std::endl;
    }
};

enum SHOES_TYPE
{
    NIKE,
    LINING,
    ADIDAS
};

// General shoe factory
class ShoesFactory
{
public:
    // Create the corresponding shoe object according to the shoe type
    Shoes* CreateShoes(SHOES_TYPE type)
    {
        switch (type)
        {
        case NIKE:
            return new NiKeShoes();
            break;
        case LINING:
            return new LiNingShoes();
            break;
        case ADIDAS:
            return new AdidasShoes();
            break;
        default:
            return NULL;
            break;
        }
    }
};


int main()
{
    // Construct factory object
    ShoesFactory shoesFactory;

    // Create Adidas shoe object from shoe factory object
    Shoes* pNikeShoes = shoesFactory.CreateShoes(NIKE);
    if (pNikeShoes != nullptr)
    {
        // Nike shoes advertising shouted
        pNikeShoes->Show();

        // Release resources
        delete pNikeShoes;
        pNikeShoes = nullptr;
    }

    // Create Adidas shoe object from shoe factory object
    Shoes* pLiNingShoes = shoesFactory.CreateShoes(LINING);
    if (pLiNingShoes != nullptr)
    {
        // Li Ning shoes advertising shouted
        pLiNingShoes->Show();

        // Release resources
        delete pLiNingShoes;
        pLiNingShoes = nullptr;
    }

    // Create Adidas shoe object from shoe factory object
    Shoes* pAdidasShoes = shoesFactory.CreateShoes(ADIDAS);
    if (pAdidasShoes != nullptr)
    {
        // Adidas shoes advertising shouted
        pAdidasShoes->Show();

        // Release resources
        delete pAdidasShoes;
        pAdidasShoes = nullptr;
    }

    return 0;
}

 

  • The main function first constructs a factory object, and then creates a specific shoe product object of the specified type. After creating an object of a specific shoe product, you can directly print advertisements. Because the new method is used to create objects, delete is used up   Release resources!

 

 

Factory method model

Details:
  • Now all kinds of shoes are very hot, so in order to mass produce each type of shoes, it is necessary to set up an independent production line for shoes of different brands, so each production line can only produce shoes of the same type.

 

UML diagram:

Structural composition of plant method mode:
  1. ShoesFactory: the core class of the factory method pattern, which provides an interface for creating specific products, and is implemented by specific factory classes.
  2. Concrete factory class (NiKeProducer\AdidasProducer\LiNingProducer): inherits from the abstract factory and implements the method of creating corresponding concrete product objects.
  3. Abstract product class (Shoes): it is the parent class (base class) inherited by specific products.
  4. Specific product class (NiKeShoes\AdidasShoes\LiNingShoes): This is the object created by a specific factory.
Characteristics of factory method mode:
  • The factory method pattern abstracts the factory class, provides the interface to create specific products, and is implemented by subclasses.
  • The application of factory method pattern is not only to encapsulate the creation of specific product objects, but to put the creation of specific product objects into the implementation of specific factory classes.
Defects of factory method mode:
  • For each new product, you need to add a specific factory class of the corresponding product. The factory method pattern requires more class definitions than the simple factory pattern.
  • A production line can only have one product.
Code of factory method mode:
  • ShoesFactory abstract factory class, which provides pure virtual functions to create specific shoe products.
  • The specific factory classes of NiKeProducer, AdidasProducer and LiNingProducer inherit the continuous factory class to create the corresponding specific shoe product object.

 

#include <iostream>
#include <algorithm>
#include <cstdio>
enum SHOES_TYPE
{
    NIKE,
    LINING,
    ADIDAS
};
// Shoe abstract class
class Shoes
{
public:
    virtual ~Shoes() {}
    virtual void Show() = 0;
};

// Nike shoes
class NiKeShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm nike shoes. My slogan: Just do it" << std::endl;
    }
};

// Adidas shoes
class AdidasShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm adidas shoes, my slogan:Impossible is nothing" << std::endl;
    }
};

// Li Ning shoes
class LiNingShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm Li Ning sneakers. My slogan is: Everything is possible" << std::endl;
    }
};


// General shoe factory
class ShoesFactory
{
public:
    virtual Shoes* CreateShoes() = 0;
    virtual ~ShoesFactory() {}
};


// Nike producer/Production chain
class NiKeProducer : public ShoesFactory
{
public:
    Shoes* CreateShoes()
    {
        return new NiKeShoes();
    }
};

// Adidas producer/Production chain
class AdidasProducer : public ShoesFactory
{
public:
    Shoes* CreateShoes()
    {
        return new AdidasShoes();
    }
};

// Li Ning producer/Production chain
class LiNingProducer : public ShoesFactory
{
public:
    Shoes* CreateShoes()
    {
        return new LiNingShoes();
    }
};

int main()
{
    // ================ Nike production process ==================== //
    // Shoe factory opens Nike production line
    ShoesFactory* niKeProducer = new NiKeProducer();
    // Nike production line produces sneakers
    Shoes* nikeShoes = niKeProducer->CreateShoes();
    // Nike shoes advertising shouted
    nikeShoes->Show();
    // Release resources
    delete nikeShoes;
    delete niKeProducer;

    // ================ Adidas production process ==================== //
    // Adidas producers opened in shoe factories
    ShoesFactory* adidasProducer = new AdidasProducer();
    // Adidas production line produces sneakers
    Shoes* adidasShoes = adidasProducer->CreateShoes();
    // Adidas shoes shouted
    adidasShoes->Show();
    // Release resources
    delete adidasShoes;
    delete adidasProducer;

    return 0;
}

result:

 

Abstract factory pattern

Details:
  • In order to expand its business, the shoe factory not only produces shoes, but also produces sports brand clothes.
UML diagram:

Structure composition of abstract factory pattern (same as factory method pattern):
  1. ShoesFactory: the core class of the factory method pattern, which provides an interface for creating specific products, and is implemented by specific factory classes.
  2. Concrete factory class (NiKeProducer): it inherits from the abstract factory and implements the method of creating corresponding concrete product objects.
  3. Abstract product class (Shoes\Clothe): it is the parent class (base class) inherited by specific products.
  4. Specific product class (NiKeShoes\NiKeClothe): This is the object created by a specific factory.
Characteristics of abstract factory mode:
  • Provides an interface to create product objects in multiple product families. If you create a Nike factory, you can create Nike shoe products, clothing products, pants products, etc.
Defects of abstract factory pattern:
  • Like the factory method mode, when adding a product, you need to add a specific factory class of the corresponding product.
Abstract factory code:
  • Clothe and Shoes are abstract product classes of clothes and Shoes respectively.
  • NiKeClothe and NiKeShoes are specific product categories of Nike clothes and Nike clothes respectively.

 

#include <iostream>
#include <algorithm>
#include <cstdio>
enum SHOES_TYPE
{
    NIKE,
    LINING,
    ADIDAS
};
// Shoe abstract class
class Shoes
{
public:
    virtual ~Shoes() {}
    virtual void Show() = 0;
};

// Nike shoes
class NiKeShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm nike shoes. My slogan: Just do it" << std::endl;
    }
};

// Adidas shoes
class AdidasShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm adidas shoes, my slogan:Impossible is nothing" << std::endl;
    }
};

// Li Ning shoes
class LiNingShoes : public Shoes
{
public:
    void Show()
    {
        std::cout << "I'm Li Ning sneakers. My slogan is: Everything is possible" << std::endl;
    }
};


// Basic clothing
class Clothe
{
public:
    virtual void Show() = 0;
    virtual ~Clothe() {}
};

// Nike clothes
class NiKeClothe : public Clothe
{
public:
    void Show()
    {
        std::cout << "I'm Nike clothes. I'm the best at fashion!" << std::endl;
    }
};


// General Factory
class Factory
{
public:
    virtual Shoes* CreateShoes() = 0;
    virtual Clothe* CreateClothe() = 0;
    virtual ~Factory() {}
};


// Nike producer/Production chain
class NiKeProducer : public Factory
{
public:
    Shoes* CreateShoes()
    {
        return new NiKeShoes();
    }
    Clothe* CreateClothe()
    {
        return new NiKeClothe();
    }
};

// Adidas producer/Production chain
class AdidasProducer : public Factory
{
public:
    Shoes* CreateShoes()
    {
        return new AdidasShoes();
    }
};

// Li Ning producer/Production chain
class LiNingProducer : public Factory
{
public:
    Shoes* CreateShoes()
    {
        return new LiNingShoes();
    }
};

int main()
{
    // ================ Nike production process ==================== //
    // Shoe factory opens Nike production line
    Factory* niKeProducer = new NiKeProducer();

    // Nike production line produces sneakers
    Shoes* nikeShoes = niKeProducer->CreateShoes();
    // Nike production line produces clothes
    Clothe* nikeClothe = niKeProducer->CreateClothe();

    // Nike shoes advertising shouted
    nikeShoes->Show();
    // Nike clothes advertising shouted
    nikeClothe->Show();

    // Release resources
    delete nikeShoes;
    delete nikeClothe;
    delete niKeProducer;
    return 0;
}

 

 

 

Posted on Fri, 29 Oct 2021 05:55:01 -0400 by designxperts