Big talk design pattern C + + Implementation-8th Chapter-factory Method mode

Source: Internet
Author: User

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" &LT;&LT;ENDL;} virtual void Wash () {cout<< "Laundry" &LT;&LT;ENDL;} virtual void Buyrice () {cout<< "Buy rice" <<endl;}};/ /Below are two detailed product classes class Undergraduate:public leifeng{public:void Sweep () {cout<< "students-sweeping" &LT;&LT;ENDL;} void Wash () {cout<< "student-laundry" &LT;&LT;ENDL; void Buyrice () {cout<< "Students-buy rice" <<endl;}}; Class Volunteer:public leifeng{public:void Sweep () {cout<< "volunteers-sweeping" &LT;&LT;ENDL;} void Wash () {cout<< "volunteer-laundry" &LT;&LT;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

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.