C + + Design Pattern -- simple factory pattern

Problem description

Previously, I made a reader for windows 8 Platform in the company. First of all, it is necessary to render the contents of the e-book to the screen, and each page of the e-book contains a variety of contents, such as: graphics, images and text, etc.; different contents are different objects; before rendering different contents to the screen, it is necessary to operate new, establish different objects, and then describe them on the screen. At this time, a lot of new operations need to be carried out. New operations are distributed in different parts of the code, which is very cumbersome and messy to manage. In the later stage of expansion and maintenance, sometimes the developers don't know what the object is because of many objects, which increases the difficulty. At the same time, new operations will have corresponding exception handling. Finally, it will be found that , in the code, a new object is added, and then an exception handling code is followed. At this time, the code becomes extremely confusing and bloated. So what? What should I do? At this point, we need a new class that specializes in the creation and release of objects. After that, the various operations of objects have nothing to do with this class. This is a special class for creating objects, which exposes the interface of creating objects for external calls.

There is a very vivid description of the factory model. The object class is like a factory, and the objects to be built are products. The people who process products and use products in the factory do not care how the products are produced. From the perspective of software development, this effectively reduces the coupling between modules.

 

UML class diagram

For factory mode, it can be divided into three categories:

  1. Simple factory mode;
  2. Factory method mode;
  3. Abstract factory pattern.

For the above three factory models, they are gradually abstract from top to bottom, and more general. This post mainly talks about simple factory mode, and the latter two will be summarized in the post.

ProductA, ProductB, and ProductC inherit from the Product virtual class. The Show method is the self description of different products. Factory depends on ProductA, ProductB, and ProductC. Factory creates different Product objects according to different conditions.

 

Suitable occasion

  1. In the program, when there are many objects to be created, resulting in many and miscellaneous new operations of the objects, the simple factory mode needs to be used;
  2. Because we don't need to care about the object creation process, but we focus on the actual operation of the object, so we need to separate the creation and operation of the object, so that it is convenient for later program expansion and maintenance.

 

code implementation

#include <iostream>
#include <vector>
using namespace std;
 
typedef enum ProductTypeTag
{
    TypeA,
    TypeB,
    TypeC
}PRODUCTTYPE;
 
// Here is the product class
class Product
{
public:
    virtual void Show() = 0;
};
 
class ProductA : public Product
{
public:
    void Show()
    {
        cout<<"I'm ProductA"<<endl;
    }
};
 
class ProductB : public Product
{
public:
    void Show()
    {
        cout<<"I'm ProductB"<<endl;
    }
};
 
class ProductC : public Product
{
public:
    void Show()
    {
        cout<<"I'm ProductC"<<endl;
    }
};
 
// Here is the Factory class
class Factory
{
public:
    Product* CreateProduct(PRODUCTTYPE type)
    {
        switch (type)
        {
        case TypeA:
            return new ProductA();
 
        case TypeB:
            return new ProductB();
 
        case TypeC:
            return new ProductC();
 
        default:
            return NULL;
        }
    }
};
 
int main(int argc, char *argv[])
{
    // First, create a factory object
    Factory *ProductFactory = new Factory();
    Product *productObjA = ProductFactory->CreateProduct(TypeA);
    if (productObjA != NULL)
        productObjA->Show();
 
    Product *productObjB = ProductFactory->CreateProduct(TypeB);
    if (productObjB != NULL)
        productObjB->Show();
 
    Product *productObjC = ProductFactory->CreateProduct(TypeC);
    if (productObjC != NULL)
        productObjC->Show();
 
    delete ProductFactory;
    ProductFactory = NULL;
 
    delete productObjA;
    productObjA = NULL;
 
    delete productObjB;
    productObjB = NULL;        
 
    delete productObjC;
    productObjC = NULL;
 
    return 0;
}

 

Published 11 original articles, won praise 4, visited 1014
Private letter follow

Tags: Windows

Posted on Sun, 12 Jan 2020 02:40:59 -0800 by helpmeplease1234