4th. Create pattern-factory method mode (1)

Source: Internet
Author: User

1. Simple Factory

1.1 definition of a simple factory

(1) provides a function to create an object instance without concern for its specific implementation .

①api interface: Define the functional interfaces required by the customer

②impl: Implements the implementation class of the API specifically, there may be multiple

③simplefatory class: Factory, select the appropriate implementation class to create the API interface object

④client: Client, through factory to get API interface object, and then programming for API interface.

(2) Think simple factory

The essence of ① simple factory: The choice realizes , its emphasis is in the choice . The main function inside the factory class is to "Select the appropriate implementation class" To create the instance object.

② The purpose of the Simple factory: To select the appropriate implementation for the client, thus decoupling the client from the implementation . In this way, the specific implementation has changed, there is no need to change the client, the change was absorbed and shielded by a simple factory.

(3) Recommendations for simple factory naming

The ① class name is suggested as " module name +factory". For example, the factory of the user module is Userfactory

The ② method name is usually either "get+ Interface name " or "Create+ Interface name". For example, if you have an interface name of Userebi, the method name is usually Getuserebi or Createuserebi.

does not advocate naming the method name "new+ Interface name ", it should be a keyword in C + +, and an object instance obtained through a simple factory does not always have to be a new instance each time. If you use Newuserebi, it can be an illusion, as if it were a new instance each time.

1.2 advantages and disadvantages of a simple factory

(1) Advantages of simple factory

① Help Package : Simple factory, but very friendly to help us achieve the package of components, so that the component can be truly interface-oriented programming

decoupling, through a simple factory, the realization of the client and the implementation of the decoupling of the class, the client does not know exactly who to implement, and do not know how to implement, just through the factory to obtain the interface object it needs.

(2) Disadvantages of the Simple factory

① may increase the complexity of the client, the client chooses the concrete implementation class through the parameter, then must let the client understand each parameter represents the specific function and the meaning , will increase the use difficulty, also partially exposes its internal implementation.

does not facilitate the extension of the sub-factory, the constructor of the factory class is private, and the interface is created using a static method. Therefore, it is not possible to change the behavior of creating an interface method by writing a subclass of a simple factory class. However, it is usually not necessary to create a subclass for a simple factory.

③ when a new implementation class is added to the interface, it is necessary to modify the factory class code, which does not conform to the open and closed principle.

1.3 when to choose a simple factory

① if you want to fully encapsulate the implementation of the isolation, let external only operate the wrapper through the interface. You can choose a simple factory, let the client through the factory to obtain the corresponding interface, without concern for the specific implementation

② if you want to centralize the responsibility of creating objects , you can choose a simple factory

"Case Analysis" calculator with simple factory implementation

//Create pattern: Simple Factory#include<stdio.h>//the Arithmetic class (the other addition and subtraction multiplication classes inherit from here)classcoperator{protected:    DoubleMfirst; DoubleMsecond; Public:    voidSetfirst (DoubleValue) {Mfirst =value;} DoubleGetFirst () {returnMfirst;} voidSetsecond (DoubleValue) {Msecond =value;} DoubleGetsecond () {returnMsecond;} Virtual DoubleGetResult () {return 0;} };//Addition ClassclassCADD: Publiccoperator{ Public:    DoubleGetResult () {returnMfirst +Msecond; }    };//Subtraction ClassclassCsub: Publiccoperator{ Public:     DoubleGetResult () {returnMfirst-Msecond; }    };//Multiplication ClassclassCMul: Publiccoperator{ Public:    DoubleGetResult () {returnMfirst *Msecond; }    };//Division classclassCdiv: Publiccoperator{ Public:     DoubleGetResult () {Const DoubleP =0.000000000000001; if((-p < Msecond) && (msecond< P))//the divisor cannot be 0         {            return 0; }                    returnMfirst/Msecond; }    };//Simple Factory classclasscsimplefactory{ Public:    /** * Create a method for implementing a class object with a specific operation *@ parameter: An external incoming selection condition (+ 、-、 *,/) *@ return value: Create a good Operation implementation class object*/    //defining a CREATE function as a static member function allows the client to omit the creation of a factory class object    Staticcoperator* Createoperator (Charcoperator) {Coperator* Oper =NULL; Switch(Coperator)//choose to create a specific class based on the parameters passed in by the client        {         Case '+': Oper=NewCAdd ();  Break;  Case '-': Oper=Newcsub ();  Break;  Case '*': Oper=NewCMul ();  Break;  Case '/': Oper=NewCdiv ();  Break; }                returnOper; }};intMain () {//Client Invocation Example//The client simply relies on the Coperator interface class and factory class, and cannot know the specific implementation class//decoupling between the client and the concrete implementation classcoperator* oper = Csimplefactory::createoperator ('/'); Oper->setfirst (1); Oper->setsecond (2); printf ("%f +%f =%f\n", Oper->getfirst (), Oper->getsecond (),oper->GetResult ()); return 0;}

4th. Create pattern-factory method mode (1)

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.