Design pattern-builder mode (created)

Source: Internet
Author: User

First, look at the UML diagram of the builder mode:

Second, look at an example of a builder model

classBuilder { Public:    Virtual voidBuildhead () {}Virtual voidbuildbody () {}Virtual voidBuildleftarm () {}Virtual voidBuildrightarm () {}Virtual voidBuildleftleg () {}Virtual voidBuildrightleg () {}};//construct Thin ManclassThinbuilder: Publicbuilder{ Public:    voidBuildhead () {cout<<"Build Thin Body"<<Endl;} voidBuildbody () {cout<<"Build Thin head"<<Endl;} voidBuildleftarm () {cout<<"Build Thin Leftarm"<<Endl;} voidBuildrightarm () {cout<<"Build Thin Rightarm"<<Endl;} voidBuildleftleg () {cout<<"Build Thin Leftleg"<<Endl;} voidBuildrightleg () {cout<<"Build Thin Rightleg"<<Endl;}};//build a Fat manclassFatbuilder: Publicbuilder{ Public:    voidBuildhead () {cout<<"Build Fat Body"<<Endl;} voidBuildbody () {cout<<"Build Fat head"<<Endl;} voidBuildleftarm () {cout<<"Build Fat Leftarm"<<Endl;} voidBuildrightarm () {cout<<"Build Fat Rightarm"<<Endl;} voidBuildleftleg () {cout<<"Build Fat Leftleg"<<Endl;} voidBuildrightleg () {cout<<"Build Fat Rightleg"<<Endl;}};//the commander of the constructionclassDirector {Private: Builder*M_pbuilder; Public: Director (Builder*builder) {M_pbuilder =Builder;} voidCreate () {M_pbuilder-Buildhead (); M_pbuilder-Buildbody (); M_pbuilder-Buildleftarm (); M_pbuilder-Buildrightarm (); M_pbuilder-Buildleftleg (); M_pbuilder-Buildrightleg (); }};

III. linkages between builders ' models and factory models

By contacting the previous factory model, you can see that the builder model has only one more "director class" role than the factory model. If the Director class is viewed as a client, it can be seen as a simple factory model.

The builder pattern is typically used to create more complex objects than the factory model, because the object creation process is more complex, so the object creation process is independent of the new class-The Director class. In other words, the factory pattern is to encapsulate the entire creation of objects in the factory class, which is provided by the factory class to the client, and the builder's model typically only provides for the construction of individual components in the product class, and the concrete construction process is delivered to the Director class. The Director class is responsible for assembling individual components into products according to specific rules, and then delivering the assembled product to the client.

The builder pattern is similar to the factory model, they are both builder models and the applicable scenarios are similar. In general, if the construction of the product is complex , then use the factory model, if the product is more complex to build, then use the builder mode.

Reference:

http://blog.csdn.net/hguisu/article/details/7518060

http://blog.csdn.net/wuzhekai1985/article/details/6667467

Design pattern-builder mode (created)

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.