I. Overview
Factory models include simple factories, factory methods, and abstract factories. They are arranged in a sequence from simple to complex and belong to the creation type in design patterns, the simple factory does not belong to the gof 23 medium mode.
But it is a good foundation for understanding other factory models, so many people will mention the simple factory model when talking about the design model.
The Creation Mode focuses on Object creation. The Creation Mode abstracts the process of object creation and encapsulates the process of object creation, as a customer program, you only need to use objects, instead of the logic in the object creation process.
Advantages:
1) solved a lot of new problems in the code, and encapsulated the operations of the new object into the factory class. As a result, using the factory method to create an object inside a class is usually more flexible than creating an object directly.
Disadvantages:
1) because it is a simple encapsulation, modification is not closed in this mode. When adding a product, you need to modify the factory class, violating the open/closed principle (OCP ).
Because if all the products are directly produced in the factory class (new), the factory class should be modified when new products are added, and the encapsulation is not good.
Example:
A simple factory plans a certain type of products to form a product set. However, these products are not directly open to the outside, but are provided for external calls through their basic interfaces,
This is like a shoot factory. Of course we cannot pick up the goods directly from the production department. You need to know that there are many production lines. They only know their online models, but they do not know about other online models, as an external employee, you must apply for a visit.
So don't try it out. This shoe factory has already arranged the sales department to supply goods externally. If you need any type of shoes, contact the Sales Department. The sales department will help you make all the necessary arrangements.
Currently, many codes do not use the factory class, but directly call the prodcut class to directly use the product, which leads to the new to go to the sky. Now we have a simple factory model to plan the product, well managed products, everything is in a benign direction.
In short:
There are two types of clusters in a simple factory: product, factory, and product. In principle, the external operation on the product can only be performed through the factory, but not directly to the product, thus protecting the confidentiality of the product.
The product class is responsible for completing various products and providing only limited interfaces to the outside. It has a two-layer structure: Product virtual base class-product subclass
The factory class is responsible for creating products. It has a layer-1 Structure: simple factory class
Ii. Class diagram:
Iii. Code:
Simplefactory. h
# Pragma once // The factory class depends on the product class. Because a factory without a product cannot be called a factory, there are: // factory class --- (CALL) ---> product class # include "iproduct. H "// the header file below should actually be placed in the CPP file. The reason why it is put here is to better interpret the simple factory mode (concentrate them together in the H file description) # include" concreteproducta. H "# include" concreteproductb. H "// factory class: The product base class is returned for the outer layer, and the specific product is produced (created, new) in the inner layer. // This is like a factory that supplies products to the outside world. The customer needs to call the production department by a single phone, so that the production department can take the products directly. That is, one-step (new) molding does not focus on the production process of the product. Class csimplefactory {public: csimplefactory (){};~ Csimplefactory () {}; public: iproduct * createproduct (INT nproducttype); iproduct * createproducta (); iproduct * createproductb ();};
Simplefactory. cpp
#include"SimpleFactory.h"IProduct *CSimpleFactory::CreateProduct(int nProductType){IProduct *ptr = 0;switch (nProductType){case 0:ptr = new CConcreteProductA();break;case 1:ptr = new CConcreteProductB();break;default:ptr = new CConcreteProductA();}return ptr;}IProduct *CSimpleFactory::CreateProductA(){cout<<__FUNCTION__<<endl;return new CConcreteProductA();}IProduct *CSimpleFactory::CreateProductB(){cout<<__FUNCTION__<<endl;return new CConcreteProductB();}
Iproduct. h
# Pragma once # include <stdio. h> # include <iostream> using namespace STD; // This is a virtual product base class. Its task is to train (abstract) required products and abstract their public attributes, then let its derived classes focus on their own functions. // Therefore, it does not depend on any business-related header files, but may depend on system header files. For example, if you use string, the string header file must be included. // This product base class exists because we want to abstract their public attributes for the products we produce, and this public attribute is open to the outside world, for external calls (such as client programs. // During external calls, the operation will be performed with polymorphism and will not be exposed to the internal details of the product. This ensures the sealing of the product. Class iproduct {protected: iproduct (); Public: Virtual ~ Iproduct (); Public: Virtual void function () = 0 ;};
Iproduct. cpp
#include "IProduct.h"IProduct::IProduct(void){}IProduct::~IProduct(void){}
Concreteproducta. h
# Pragma once # include "iproduct. H "// cconcreteproducta (derived class) ---- |> iproduct (base class) // detailed product implementation: Based on Product functional requirements (base class Interface ), function a product class cconcreteproducta: Public iproduct {public: cconcreteproducta (); Virtual ~ Cconcreteproducta (); Virtual void function ();};
Concreteproducta. cpp
#include "ConcreteProductA.h"CConcreteProductA::CConcreteProductA(void) {cout<<__FUNCTION__<<endl;}CConcreteProductA::~CConcreteProductA(void) {cout<<__FUNCTION__<<endl;}void CConcreteProductA::Function() {cout<<__FUNCTION__<<endl;}
Concreteproductb. h
# Pragma once # include "iproduct. H "// cconcreteproducta (derived class) ---- |> cproduct (base class) // detailed product implementation: Based on Product functional requirements (base class Interface ), function B Product class cconcreteproductb: Public iproduct {public: cconcreteproductb (void); Virtual ~ Cconcreteproductb (void); Virtual void function ();};
Concreteproductb. cpp
#include "ConcreteProductB.h"CConcreteProductB::CConcreteProductB(void) {cout<<__FUNCTION__<<endl;}CConcreteProductB::~CConcreteProductB(void) {cout<<__FUNCTION__<<endl;}void CConcreteProductB::Function() {cout<<__FUNCTION__<<endl;}
Main. cpp
#include "SimpleFactory.h"#include <stdio.h>int main(){CSimpleFactory fac;IProduct *pProduct = fac.CreateProductA();pProduct->Function();delete pProduct;pProduct = NULL;pProduct = fac.CreateProductB();pProduct->Function();delete pProduct;pProduct = NULL;return 0;}
4. Running result:
Csimplefactory: createproducta
Cconcreteproducta: cconcreteproducta
Cconcreteproducta: Function
Cconcreteproducta ::~ Cconcreteproducta
Csimplefactory: createproductb
Cconcreteproductb: cconcreteproductb
Cconcreteproductb: Function
Cconcreteproductb ::~ Cconcreteproductb
Press any key to continue...