Simple Factory Pattern of C++ Design Patterns

1. The design idea of ​​the simple factory pattern

First, encapsulate the relevant code of various objects that need to be created into different classes, these classes are called product classes , and abstract and extract their common code and encapsulate them in an abstract product class . Each specific product class They are all subclasses of the abstract product class; then a factory class is provided to create various products, and a factory method for creating products is provided in the factory class, which can create different specific product objects according to the parameters passed in; The client only needs to call the factory method of the factory class and pass in the corresponding parameters to get a product object.

2. The 3 roles of the simple factory pattern

(1) Factory (factory role): The factory role is the factory class, which is the core of the simple factory pattern and is responsible for implementing the internal logic for creating all product instances; the factory class can be directly called by the outside world to create the required product objects; in the factory A static factory method factoryMethod() is provided in the class, and its return type is the abstract product type Product.

(2) Product (abstract product role): It is the parent class of all objects created by the factory class and encapsulates the public methods of various product objects. Its introduction will improve the flexibility of the system, so that in the factory class only the Define a generic factory method because all concrete product objects created are subclass objects.

(3) ConcreteProduct (specific product role): It is the creation target of the simple factory pattern, and all created objects act as instances of a specific class of this role. Each concrete product role inherits the abstract product role and needs to implement the abstract methods declared in the abstract product.


3. Code implementation

#include<iostream>
using namespace std;

class AbstractProduct
{
public:
	AbstractProduct(string name) :mname(name)
	{}
	virtual void operation() = 0;
	virtual ~AbstractProduct()
	{}
protected:
	string mname;
};
class ProductA : public AbstractProduct
{
public:
	ProductA(string name) :AbstractProduct(name)
	{}
	~ProductA()
	{}
	virtual void operation()
	{
		cout << "ProductA::Operation()" << endl;
	}
};
class ProductB : public AbstractProduct
{
public:
	ProductB(string name) :AbstractProduct(name)
	{}
	~ProductB()
	{}
	virtual void operation()
	{
		cout << "ProductB::Operation()" << endl;
	}
};
class Factory
{
public:
	AbstractProduct* createProduct(int flag)
	{
		switch (flag)
		{
		case 1:
			return new ProductA("A");
			break;
		case 2:
			return new ProductB("B");
			break;
		default:
			return NULL;
			break;
		}
	}
};

intmain()
{
	Factory f;
	AbstractProduct* pap = f.createProduct(1);
	pap->operation();
	AbstractProduct* pbp = f.createProduct(2);
	pbp->operation();
	delete pap;
	delete pbp;

     return 0;
}

operation result:






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324379379&siteId=291194637