[design mode]<2>. C + + and builder mode (builder pattern)

Source: Internet
Author: User

Original address:

Http://www.cnblogs.com/hebaichuanyeah/p/5585957.html

When building a complex object, detach the build process from the presentation. Causes the same process to create different objects.

For a simple example, building the produce class requires building three parts of the PART1,PART2,PART3. Build them through the build class and return. Through Director
Class calls the build object for configuration.

C + + code

#include <iostream>using namespacestd;classproduce{ Public: Produce () {}Virtual~produce () {}};classbuilder{ Public: Builder () {}Virtual~Builder () {}Virtual voidBuildPart1 () {}Virtual voidBuildPart2 () {}Virtual voidBuildPart3 () {}VirtualProduce *getproduce () {}};classProducebuilder: Publicbuilder{Private:P roduce*Produce; Public: Producebuilder () {}Virtual voidBuildPart1 () {Produce=NewProduce (); cout<<"bulid part1"<<Endl; //Populate the Build part1 section    }    Virtual voidBuildPart2 () {cout<<"bulid part2"<<Endl; //Populate the Build part2 section    }    Virtual voidBuildPart3 () {cout<<"bulid part3"<<Endl; //Populate the Build part3 section    }    VirtualProduce *getproduce () {returnProduce; }};classdirector{Private: Builder*Bulider; Public: Director (Builder*builder) {Bulider=Builder; }    voidconstruct () {Bulider-BuildPart1 (); Bulider-BuildPart2 (); Bulider-BuildPart3 (); }};main () {Produce*Produce; Builder* Bulider =NewProducebuilder (); Director* Mydirector =NewDirector (Bulider); Mydirector-construct (); Produce= bulider->getproduce ();}

[design mode]<2>. C + + and builder mode (builder pattern)

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.