26th Chapter creation Mode Big PK

Source: Internet
Author: User

26.1 Factory method Mode VS Builder mode

26.1.1 Building Superman by factory method

(1) Products: Two kinds of Superman, adult Superman and minors Superman.

(2) Factory: Choose a simple factory here

"Programming Experiment" factory method to build Superman

//Create model Large pk--factory method and builder mode//Example: Superman created with a simple factory#include <iostream>using namespacestd;//*************************** Abstract Product Interface *********************//Superman Interfaceclassisuperman{ Public:    //every Superman has a special skill.    Virtual voidSpecialtalent () =0; Virtual~Isuperman () {}};//************************* specific product ***********************//Adult SupermanclassAdultsuperman: Publicisuperman{ Public:    voidspecialtalent () {cout<<"The Superman force is great Infinity"<<Endl; }};//A minor Superman.classChildsuperman: Publicisuperman{ Public:    voidspecialtalent () {cout<<"Little Superman's ability to be bulletproof, fast moving"<<Endl; }};//*********************** Simple Factory ****************************classsupermanfactory{ Public:    Staticisuperman* Createsuperman (stringtype) {        //generate different Superman based on input parameters        if(Type = ="Adult")        {            return NewAdultsuperman (); }        Else if(Type = =" Child")        {            return NewChildsuperman (); }        Else            returnNULL; }};intMain () {//Analog Production Superman (note that products from the production plant (e.g. adult) are a model//There is no special place, which is very different from the following builder model)isuperman* sm = Supermanfactory::createsuperman (" Child"); //show Superman's skills.Sm->specialtalent (); DeleteSM; return 0;};

26.1.2 build Superman by builder mode

(1) Three major components of the product: body, special skills, identity marks

(2) Builders: Builder. Note that, unlike the standard builder model , the process of assembling the parts here is the responsibility of the individual builders, and the standard practice is to put them into the director role .

//Create model Large pk--factory method and builder mode//Example: Superman created with builder mode#include <iostream>using namespacestd;//*************************** Auxiliary class *********************//Superman's body driveclassbody{stringbody; Public:    string& GetBody () {returnbody;} voidSetbody (stringvalue) {Body=value; }};//Special Skillsclassspecialtalent{stringspecialtalent; Public:    string& Getspecialtalent () {returnspecialtalent;} voidSetspecialtalent (stringvalue) {specialtalent=value; }};//the symbol of Supermanclassspecialsymbol{stringsymbol; Public:    string& Getsymbol () {returnsymbol;} voidSetsymbol (stringvalue) {Symbol=value; }};//Supermanclasssuperman{Private: Body Body;    Specialsymbol Specialsymbol; Specialtalent specialtalent; Public:    string& GetBody () {returnbody.getbody ();} voidSetbody (stringvalue)    {body.setbody (value); }    string& Getspecialtalent () {returnspecialtalent.getspecialtalent ();} voidSetsepcialtalent (stringvalue)    {specialtalent.setspecialtalent (value); }    string& Getspecialsymbol () {returnSpecialsymbol.getsymbol ();} voidSetspecialsymbol (stringvalue)    {Specialsymbol.setsymbol (value); }    Virtual~Superman () {}};//*************************builder role ***********************//Abstract Builderclassbuilder{protected: Superman*Superman; Public: Builder () {Superman=NewSuperman (); }    //to build a superhuman body.    voidSetbody (stringvalue) {Superman-setbody (value); }    //build Superman's special skills    voidSetspecialtalent (stringvalue) {Superman-setsepcialtalent (value); }    //build Superman's special markings    voidSetspecialsymbol (stringvalue) {Superman-Setspecialsymbol (value); }    //Build a complete Superman//Superman's various parts are ready, specifically how to assemble by the implementation of the class to decide,//embodies the intent of the builder model to separate the construction and presentation of complex objects    Virtualsuperman* Getsuperman () =0; Virtual~Builder () {DeleteSuperman; }};//Adult Superman BuilderclassAdultsupermanbuilder: Publicbuilder{ Public: Superman*Getsuperman () {//1. The standard builder model, the construction process is placed in the Director for Assembly. //2. The builder model focuses on the various parts of the product, even the build order,//That is, the same parts, the assembly sequence, the resulting results may be different,//This is the intention of the builder model)Superman->setbody ("a strong body."); Superman->setsepcialtalent ("can fly"); Superman->setspecialsymbol ("chest with S Mark"); returnSuperman; }};//Minor Superman builderclassChildsupermanbuilder: Publicbuilder{ Public: Superman*Getsuperman () {//Construction ProcessSuperman->setbody ("a strong body."); Superman->setsepcialtalent ("Bulletproof"); Superman->setspecialsymbol ("small S mark on chest"); returnSuperman; }};//Director Classclassdirector{Private:    Staticbuilder*Adultbuilder; Staticbuilder*Childbuilder; Public:    //in this case, the following function is just an empty shell, and the standard builder pattern//The assembly process of its components is done in the following two functions! //build an adult Superman    Staticsuperman*Getadultsuperman () {returnAdultbuilder->Getsuperman (); }    //The construction of a minor Superman.    Staticsuperman*Getchildsuperman () {returnChildbuilder->Getsuperman (); }}; Builder* Director::adultbuilder =NewAdultsupermanbuilder (); Builder* Director::childbuilder =NewChildsupermanbuilder ();intMain () {//Build a Supermansuperman* SM =Director::getadultsuperman (); //superman* sm = Director::getchildsuperman (); //to show a superhuman ability.cout <<"Superman's body:"<< sm->getbody () <<Endl; cout<<"Superman's special abilities:"<< sm->getspecialtalent () <<Endl; cout<<"Superman band's logo:"<< Sm->getspecialsymbol () <<Endl; return 0;};/*output Superman's body: A strong body Superman's special ability: Flying Superman's logo: chest with s Mark*/

26.1.3 Best Practices

(1) Different intentions

Factory Method model, concerned about a product as a whole , do not care about how the parts of the product are created.

The ② Builder model , where a specific product is produced depends on the production of each component and the assembly sequence, it focuses on " assembling the Product object by part step-by -step"

③ is simple to understand that Factory mode is a bold application of object creation, and the builder pattern outlines a complex object through a thin thread, focusing on the process of creating a product component.

(2) The complexity of the product is different

Factory Method mode The product that is created is generally a single nature product, is a shape .

The ② Builder mode creates a composite product , which is made up of various components, different parts, and of course the product objects are different.

③ generally the factory method model creates a coarser granularity of the product, while the builder model has a finer granularity of the product object.

(3) method selection: If you follow the production of a product part, installation steps , select the builder , otherwise choose the factory method mode .

26th Chapter creation Mode Big PK

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.