First, UML diagram
Second, the concept
Factory method Mode (Factory): defines an interface for creating an object, letting subclasses decide which class to instantiate. A factory method is an instantiation of a class that is deferred to its subclasses.
Iii. role to be included
(1) Abstract Factory
(2) Detailed factory
(3) Abstract Products
(4) Detailed product
Iv. Advantages
(1) The factory method pattern is a slight improvement in the simple factory model. The factory method pattern is intended to define a factory interface that creates product objects, deferring the actual work to subclasses.
(2) compared with the simple factory model, the factory class of manufacturing products is no longer just one. Instead, each of the detailed product classes produces its detailed factory class accordingly.
The common features of these detailed factory classes are then extracted to form an abstract product class, which is inherited from this abstract product class.
(3) When a product needs to be added, it is necessary to add a detailed product class that inherits from the abstract product. Add a detailed factory class that inherits from the abstract factory. Change the client. instead of changing the switch in the factory as in the simple Factory mode.
V. C + + implementation
(1) Examples of calculators
#include <iostream> #include <cstdlib>using namespace std;//abstract Product class Operation{protected:double Numbera ;d ouble numberb;public:double Geta () {return numbera;} Double Getb () {return numberb;} void SetA (double number) {Numbera=number;} void Setb (double number) {Numberb=number;} Virtual Double GetResult () {double Result=0;return result;}};/ /Below are four detailed product classes class Operationadd:public Operation{public:double GetResult () {double result=0;result=numbera+numberb; return result;}; Class Operationsub:public operation{public:double GetResult () {double result=0;result=numbera-numberb;return result;}}; Class Operationmul:public operation{public:double GetResult () {double result=0;result=numbera*numberb;return result;}}; Class Operationdiv:public operation{public:double GetResult () {double result=0;if (numberb!=0) Result=numbera/numberb ; return result;}};/ /Abstract Factory Classes Class Abstractfactory{public:virtual operation* createoperation () {return new operation;}};/ /Below are four detailed factory classes. For each of the four detailed product class Addfactory:public Abstractfactory{public:operation* createoperation () {operation* oper=new operationadd;return oper;}}; Class Subfactory:public abstractfactory{public:operation* createoperation () {operation* oper=new OperationSub;return Oper;}}; Class Mulfactory:public abstractfactory{public:operation* createoperation () {operation* oper=new OperationMul;return Oper;}}; Class Divfactory:public abstractfactory{public:operation* createoperation () {operation* oper=new OperationDiv;return oper;}};/ /clientvoid Main () {abstractfactory* af=null;af=new addfactory (); operation* oper=null;oper=af->createoperation () ; Oper->seta (1); Oper->setb (2); Cout<<oper->getresult () <<endl;if (af!=null) {delete Af;af=NULL;} if (oper!=null) {delete oper;oper=null;} System ("Pause");}
(2) Examples of Lei Feng factory
#include <iostream> #include <cstdlib>using namespace std;//Abstract Product class: Lei Feng class leifeng{public:virtual void Sweep () {cout<< "sweeping" <<ENDL;} virtual void Wash () {cout<< "Laundry" <<ENDL;} virtual void Buyrice () {cout<< "Buy rice" <<endl;}};/ /Below are two detailed product classes class Undergraduate:public leifeng{public:void Sweep () {cout<< "students-sweeping" <<ENDL;} void Wash () {cout<< "student-laundry" <<ENDL; void Buyrice () {cout<< "Students-buy rice" <<endl;}}; Class Volunteer:public leifeng{public:void Sweep () {cout<< "volunteers-sweeping" <<ENDL;} void Wash () {cout<< "volunteer-laundry" <<ENDL; void Buyrice () {cout<< "volunteers-buy rice" <<endl;}};/ /Abstract Factory Classes Class Abstractfactory{public:virtual leifeng* Createleifeng () {return new leifeng;}};/ /Below are two detailed factory classes, respectively, in two detailed products corresponding to class Undergraduatefactory:public abstractfactory{public:undergraduate* Createleifeng () { return new undergraduate;}; Class Volunteerfactory:public abstractfactory{public:volunteer* Createleifeng () {return new volunteer ();}};/ /clientvoid Main () {//Want to produce VolUnteer the product, you just need to change the undergraduatefactory here to Volunteerfactory. abstractfactory* af=null;af=new volunteerfactory; leifeng* Lf=null;lf=af->createleifeng (); Lf->buyrice (); Lf->sweep (); Lf->wash (); if (af!=NULL) {delete af; Af=null;} if (lf!=null) {delete lf;lf=null;} System ("Pause");}
(3) Implementation
Big talk design pattern C + + Implementation-8th Chapter-factory Method mode