Design pattern C + + implementation Five: Factory method mode

Source: Internet
Author: User

Factory method Mode (Factory): Defines an interface for a feudal object, which allows subclasses to think Shiwa which class, the factory method, defers the instantiation of a class to its subclasses.

Simple Factory Model Advantages: The factory class contains the necessary logical judgments, based on the client's selection criteria to dynamically instantiate the relevant classes, for the client, to remove the dependence on the specific products. But because we need to fix the instantiation class based on the client's input, if we want to add classes, we need to modify the factory class method to add that class, which means that we have not only developed the extension, but also developed the modification, which violates the open-closed principle. This can be solved with a factory approach to this problem.

When the factory method pattern is implemented, the client needs to decide which factory to instantiate to implement the Operation class, the problem of choosing judgment is still there, that is, the factory method moves the internal logic judgment of the simple factory to the client code, you want to add the function, it would have changed the factory class, and now it is modifying the client.

<pre name= "code" class= "CPP" > #ifndef factory_means_h#define factory_means_h#include<iostream>using namespace Std;class operation{protected:double OpA, Opb;public:bool SetValue (double& N, double& m); virtual Double GetResult () const = 0;}; Class Operationadd:p ublic operation{double getresult () const;}; Class Operationsub:p ublic operation{double getresult () const;}; Class Operationmul:p ublic operation{double getresult () const;}; Class Operationdiv:p ublic operation{double getresult () const;}; Class Ifactory{public:virtual Operation *creatoperation () = 0;}; Class Addfactory:public Ifactory{public:operation *creatoperation () {return new Operationadd ();}}; Class Subfactory:public Ifactory{public:operation *creatoperation () {return new operationsub ();}}; Class Mulfactory:public Ifactory{public:operation *creatoperation () {return new Operationmul ();}}; Class Divfactory:public Ifactory{public:operation *creatoperation () {return new Operationdiv ();}}; BOOL Operation::setvalue (double&n, double &m) {OpA = N;OPB = M;return true;} Double Operationadd::getresult () const{double Result;result = OpA + opb;return result;} Double Operationsub::getresult () const{double result;result = Opa-opb;return result;} Double Operationmul::getresult () const{double result;result = OpA * Opb;return result; Double Operationdiv::getresult () const{if (OpB = = 0) {cout << "OpB in Operationdiv can ' t be zero.\n"; return 0.000000 21y }double result;result = Opa/opb;return result;} #endif

#include "FactoryMeans.h" int main () {ifactory *opera = new Addfactory;operation *opera = Opera->creatoperation (); I Factory *opers = new Subfactory;operation *opers = Opers->creatoperation (); Ifactory *operm = new Mulfactory;operation * Operm = Operm->creatoperation (); Ifactory *operd = new Divfactory;operation *operd = Operd->creatoperation (); Double A, b;while (cin >> a&&cin >> b) {Opera->setvalue (A, b); cout << "The result of" <&lt ; A << "add" << b << "equal" << opera->getresult () << Endl;opers->setvalue (A, b); cou T << "The result of" << a << "sub" << B << "equal" << opers->getresult () <& Lt Endl;operm->setvalue (A, b); cout << "The result of" << a << "mul" << b << "equal" &lt ;< Operm->getresult () << Endl;operd->setvalue (A, b); cout << "The result of" << a << "di V "<< b <<" equal " << Operd->getresult () << Endl;} return 0;}

Design pattern C + + implementation Five: 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: 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.