Define an interface for creating objects, let subclasses decide which class to instantiate, and the creation of objects is uniformly handed over to the factory for production, which has good encapsulation, which not only realizes the understanding of coupling, but also ensures the principle of minimum knowledge.
1. Simple factory mode
The simple factory mode belongs to the creation mode, also known as the static factory method mode. The simple factory mode determines which product class instance is created by a factory object. The essence of the simple factory pattern is that a factory class dynamically determines which kind of product class (which inherits from a parent class or interface) should be created according to the passed parameters.
For example: suppose there is a factory that can produce products a and B. When customers need products, they must tell the factory which products are - A or B. When a new product is added, it is necessary to modify the factory class.
// Factory.cpp: defines the entry point for the console application. // #include "stdafx.h" #include<iostream> using namespace std; class Product { public: virtual void show() = 0; //Pure virtual function }; class Product_A : public Product { public: void show() { cout << "Product_A" << endl; } }; class Product_B : public Product { public: void show() { cout << "Product_B" << endl; } }; class Factory { public: Product* Create(int i) { switch (i) { case 1: return new Product_A; break; case 2: return new Product_B; break; default: break; } } }; int main() { Factory *factory = new Factory(); factory->Create(1)->show(); factory->Create(2)->show(); system("pause"); return 0; }
2. Factory method model
The disadvantage of the simple factory mode is that when adding new products, you have to modify the factory class, which violates the open and closed principle. Classes, modules and functions can be extended but not modified. Therefore, the factory method mode appears. The so-called factory method pattern refers to defining an interface for creating objects and letting subclasses decide which class to instantiate. For example
Now there are two products A and B, and two factories have been opened for so long. Factory A is responsible for producing product A and factory B is responsible for producing product B. At this time, the customer doesn't need to tell the factory what kind of products to produce, just tell the factory what to produce.
#include<iostream> using namespace std; class Product { public: virtual void show() = 0; //Pure virtual function }; class Product_A : public Product { public: void show() { cout << "Product_A" << endl; } }; class Product_B : public Product { public: void show() { cout << "Product_B" << endl; } }; class Factory { public: virtual Product* create() = 0; //Pure virtual function }; class Factory_A : public Factory { public: Product* create() { return new Product_A; } }; class Factory_B : public Factory { public: Product* create() { return new Product_B; } }; int main() { Factory_A* productA = new Factory_A(); Factory_B* productB = new Factory_B(); productA->create()->show(); //The new object returned by crite() can then call show() productB->create()->show(); system("pause"); return 0; }
3. Abstract factory pattern
Why should there be an abstract factory model? If there are A1 and A2 factory products in product a and B1 and B2 factory products in product B, what should we do? The above two factory models can't be solved. At this time, the abstract factory pattern appears. Two factories are also set up. Factory a is responsible for producing A1 and A2 products, and factory B is responsible for producing B1 and B2 products.
Provides an interface to create a series of related or interdependent objects without specifying their specific classes.
Applicability: when a system needs to be independent of the creation, combination and representation of its products. When a system is to be configured by one of multiple product families. When you want to emphasize the design of a series of related product objects for joint use. When you provide a product class library and only want to display their interfaces rather than implementations.
#include <iostream> using namespace std; class product1 //Define abstract classes { public: virtual void show() = 0; }; class product_A1 :public product1 //Define concrete classes { public: void show(){ cout << "product A1" << endl; } }; class product_B1 :public product1 { public: void show(){ cout << "product B1" << endl; } }; class product2 //Define abstract classes { public: virtual void show() = 0; }; class product_A2 :public product2 //Define concrete classes { public: void show(){ cout << "product A2" << endl; } }; class product_B2 :public product2 { public: void show(){ cout << "product B2" << endl; } }; class Factory { public: virtual product1 *creat1() = 0; virtual product2 *creat2() = 0; }; class FactoryA { public: product1 *creat1(){ return new product_A1(); } product2 *creat2(){ return new product_A2(); } }; class FactoryB { public: product1 *creat1(){ return new product_B1(); } product2 *creat2(){ return new product_B2(); } }; int main() { FactoryA *factoryA = new FactoryA(); factoryA->creat1()->show(); factoryA->creat2()->show(); FactoryB *factoryB = new FactoryB(); factoryB->creat1()->show(); factoryB->creat2()->show(); system("pause"); return 0; }