[C++] Design Patterns - Several Factory Patterns

Posted by kelvin on Wed, 28 Aug 2019 12:45:44 +0200

Factory

Factory model

Strictly speaking, the simple factory model does not fall within the scope of the GOF design model, which is used here for comparative learning.

Simple factory model

In short, a simple factory means that there is a factory that can produce different products, the product has an abstract class, and the actual product class implements the abstract class. The code is as follows:

#include <iostream>
using namespace std;

// Simple Factory
class Car
{
public:
	virtual void show() = 0;
};

class Benz : public Car
{
public:
	void show()
	{
		cout << "Benz" << endl;
	}
};

class Bmw : public Car
{
public:
	void show()
	{
		cout << "Bmw" << endl;
	}
};

class Factory
{
public:
	enum CarBrand
	{
		BENZ,
		BMW
	};

	Car* CreateCar(CarBrand type)
	{
		Car* temp = nullptr;
		switch (type)
		{
		case Factory::BENZ:
			temp = new Benz();
			break;
		case Factory::BMW:
			temp = new Bmw();
			break;
		default:
			temp = nullptr;
			break;
		}
		return temp;
	}
};

int main()
{
	Factory* factory = new Factory();
	factory->CreateCar(Factory::BENZ)->show();
	factory->CreateCar(Factory::BMW)->show();
}

The abstract class Car is defined. Benz and Bmw inherit and implement Car respectively. In factory class Factory, the method of generating instance objects of two classes is provided, and different instances are generated by condition judgment. Operation results:

Benz
Bmw

Factory Method Model

Compared with the simple factory model, the factory method model has two factories, each of which produces its own products. So the product has an abstract class and the factory has an abstract class.

// Factory Method
class Car
{
public:
	virtual void show() = 0;
};

class Benz : public Car
{
public:
	void show()
	{
		cout << "Benz" << endl;
	}
};

class Bmw : public Car
{
public:
	void show()
	{
		cout << "Bmw" << endl;
	}
};

class Factory
{
public:
	virtual Car* create() = 0;
};

class Factory_A : public Factory
{
public:
	Car* create()
	{
		return new Benz();
	}
};

class Factory_B : public Factory
{
public:
	Car* create()
	{
		return new Bmw();
	}
};

int main()
{
	Factory* factory_A = new Factory_A();
	factory_A->create()->show();

	Factory* factory_B = new Factory_B();
	factory_B->create()->show();
}

Factory_A only produces Benz, while Factory_B only produces Bmw. Result:

Benz
Bmw

Abstract factory pattern

Compared with the former two, the abstract factory model has two factories, two products, and two products have different models, such as Benz E-class and S-class, Bmw 5-series and 7-series. The two factories are Benz's and Bmw's.

// Abstract Factory Pattern
class CClass // Class C Car
{
public:
	virtual void show() = 0;
};

class BenzESeries : public CClass // Mercedes-Benz E-Class
{
public:
	void show()
	{
		cout << "Benz E Class" << endl;
	}
};

class Bmw5Series : public CClass // BMW 5 Series
{
public:
	void show()
	{
		cout << "Bmw 5 Series" << endl;
	}
};

class DClass // Class D
{
public:
	virtual void show() = 0;
};

class BenzSSeries : public DClass // Mercedes-Benz S-Class
{
public:
	void show()
	{
		cout << "Benz S Series" << endl;
	}
};

class Bmw7Series : public DClass // BMW 7 Series
{
public:
	void show()
	{
		cout << "Bmw 7 Series" << endl;
	}
};

class Factory
{
public:
	virtual CClass* createCClass() = 0;
	virtual DClass* createDClass() = 0;
};

class Factory_Benz : public Factory // Mercedes-Benz factories can produce Class E and Class S
{
public:
	CClass* createCClass()
	{
		return new BenzESeries();
	}

	DClass* createDClass()
	{
		return new BenzSSeries();
	}
};

class Factory_Bmw : public Factory // BMW's factories can produce 5-series and 7-series
{
public:
	CClass* createCClass()
	{
		return new Bmw5Series();
	}

	DClass* createDClass()
	{
		return new Bmw7Series();
	}
};

int main()
{
	Factory* Benz = new Factory_Benz();
	Benz->createCClass()->show();
	Benz->createDClass()->show();

	Factory* Bmw = new Factory_Bmw();
	Bmw->createCClass()->show();
	Bmw->createDClass()->show();
}

Result:

Benz E Class
Benz S Series
Bmw 5 Series
Bmw 7 Series

The code in this article refers to Summary of C++ Factory ModelAbstract factory pattern of design pattern (C++).