Factory model of Design Pattern

Source: Internet
Author: User

I. Overview

Compared with the simple factory mode, the factory mode delays the instantiation of a class to its subclass, improving the problem that the simple factory cannot close the modification.
Here we need to pay attention to the code changes: Compared with the simple factory model, the classes related to the product class remain unchanged, but the classes related to the factory class will change.
So the implementation of the product classes here completely copies the simple factory code. We only need to focus on changes in the factory class to understand the true intention of the factory model.

Advantages:
1) solved the problem that the modification of a simple factory cannot be closed. When the system adds a product, it is enough to add a product factory, and the factory base class is not affected.
That is to say, when a product is added, some subclasses are derived from the factory class, so that these subclasses can produce the corresponding products. When a product is added, the factory class remains unchanged. You only need to add a factory derived class.
Disadvantages:
1) unable to create multiple series products
2) When a class does not know the class of the object it must create
3) When a class wants its subclass to specify the object it creates
4) when the class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies.

In short:
The factory mode is equivalent to putting the operations for simple factory calls to the product into the derived class of the factory mode. What we do is to define a public interface for creating all products for use by factory Derived classes.
Product clusters remain unchanged and provide public property interfaces for the product. The structure of the product is maintained at Layer 2.
The factory class cluster is changed to provide the public creation interface of the product. The structure is changed to a two-layer structure: factory class-factory subclass.

Ii. Category chart

Iii. Code

Iproduct. H, iproduct. cpp (see simple factory mode code)

Concreteproducta. H, concreteproducta. cpp (same as above)

Concreteproductb. H, concreteproductb. cpp (same as above)

Concreteproductnew. h

# Pragma once # include "iproduct. H" // when adding a product, add the product derived class: cconcreteproductnewclass cconcreteproductnew: Public iproduct {public: cconcreteproductnew (); Virtual ~ Cconcreteproductnew (); Public: Virtual void function ();};

 

Concreteproductnew. cpp

#include "ConcreteProductNew.h"CConcreteProductNew::CConcreteProductNew(){cout<<__FUNCTION__<<endl;}CConcreteProductNew::~CConcreteProductNew(){cout<<__FUNCTION__<<endl;}void CConcreteProductNew::Function(){cout<<__FUNCTION__<<endl;}

 

Ifacloud. h

# Include "iproduct. H "/* simple factory class: it is used to call products. When there is a new product requirement, code will be added to the factory class, the encapsulation of the factory model is damaged. That is to say, this is a method for "factory class" to correspond to "product derived class". Once a new product class is available, it will be moved to the factory class. Factory class: provides a virtual interface for calling products to protect existing products in the form of a derived subclass. If new product requirements exist, they are implemented in a new derived class, instead of the original code, it ensures encapsulation. That is to say, this is a method in which the "factory derived class" corresponds to its "product derived class". When there is a new product class, you only need to assign child classes from the factory base class for implementation. */Class ifacloud {public: ifacloud (); Virtual ~ Ifacloud (); Public: Virtual iproduct * createproduct (INT ntype = 0) = 0 ;};

Ifacloud. cpp

#include "IFactory.h"IFactory::IFactory(){}IFactory::~IFactory(){}

Concretefactory1.h

# Pragma once # include "ifacloud. H "// factory derived class: factory base class. // the header file below should be placed in the CPP file # include" concreteproducta. H "# include" concreteproductb. H "class concretefactory1: Public ifacloud {public: concretefactory1 (); Virtual ~ Concretefactory1 (); Public: Virtual iproduct * createproduct (INT ntype = 0 );};

Concretefactory1.cpp

#include "ConcreteFactory1.h"ConcreteFactory1::ConcreteFactory1(){}ConcreteFactory1::~ConcreteFactory1(){}IProduct *ConcreteFactory1::CreateProduct(int nType){IProduct *ptr = NULL;switch (nType){case 0:ptr = new CConcreteProductA();break;case 1:ptr = new CConcreteProductB();break;default:ptr = new CConcreteProductA();}return ptr;}

Concretefactorynew. h

# Pragma once # include "ifacloud. H "// This header file should be placed in CPP # include" concreteproductnew. H "// when adding a product, add the factory derived class concretefactorynewclass cconcretefactorynew: Public ifacloud {public: cconcretefactorynew (); Virtual ~ Cconcretefactorynew (); Public: Virtual iproduct * createproduct (INT ntype = 0 );};

 

Concretefactorynew. cpp

#include "ConcreteFactoryNew.h"CConcreteFactoryNew::CConcreteFactoryNew(){}CConcreteFactoryNew::~CConcreteFactoryNew(){}IProduct *CConcreteFactoryNew::CreateProduct(int nType){return new CConcreteProductNew();}

Main. cpp

# Include "ifacloud. H "# include" iproduct. H "# include" concretefactory1.h "# include" concretefactorynew. H "# include <iostream> using namespace STD; int main () {// you can call the factory interface cout of an existing product <" Call Inner Product: "<Endl; ifactory * pfactroy = new concretefactory1 (); iproduct * pproduct = pfactroy-> createproduct (0); pproduct-> function (); Delete pproduct; pproduct = NULL; pproduct = pfactroy-> createproduct (1); pproduct-> function (); Delete pproduct; pproduct = NULL; Delete pfactroy; pfactroy = NULL; // call the factory interface cout of the new product <"call New Product:" <Endl; pfactroy = new cconcretefactorynew; pproduct = pfactroy-> createproduct (); pproduct-> function (); Delete pproduct; pproduct = NULL; Delete pfactroy; pfactroy = NULL; return 0 ;}

 

 

Iv. Running results

Call inner product:
Cconcreteproducta: cconcreteproducta
Cconcreteproducta: Function
Cconcreteproducta ::~ Cconcreteproducta
Cconcreteproductb: cconcreteproductb
Cconcreteproductb: Function
Cconcreteproductb ::~ Cconcreteproductb
Call New Product:
Cconcreteproductnew: cconcreteproductnew
Cconcreteproductnew: Function
Cconcreteproductnew ::~ Cconcreteproductnew
Press any key to continue...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.