C + + implementation of Builder mode

Source: Internet
Author: User

Note : This article is only for learning exchange, reproduced please indicate the source. Welcome Reprint!

Builder Mode (creator) Also called the generator pattern , as we all know. Suppose we want to finish the process of creating a complex product, we must create it separately. Re-assembled.

Bicycles , for example, have iron racks, bell, seats, tires, rims .

Suppose we need to assemble a bicycle, we must have these things, from the perspective of the creator model. We see bicycles as " products ". Put the iron frame, bell, car seat ... As " parts ". And the price and performance of different parts are not the same. So we have to select the corresponding parts according to the actual situation before assembling, this process can be summed up as follows: after selecting the detailed parts, we will assemble them and get the bikes we expect .

Through the above analysis. Gives the builder pattern definition:

Builder Mode : separates the construction of a complex object from its representation so that the same build process can create different representations.

A two-point description is given in the definition above:

1. Object Representation : macro Concept , what do we need to do to build an object? ( what to do )

2. Construction of Objects : micro-concept . What should we do to build objects? ( how to do )

structure diagram of the builder pattern

This picture is from "Big Talk design mode"

C + + implementation code:

#include <iostream> #include <list> #include <string> #include <algorithm>//for_each (b,e,fun) Using namespace Std;template<typename t>struct showfun//defines an functor for for_each functions to use {void operator () (T-t) {cout< <t<<endl;}}; Class product//Detailed Product class {private:list<string> parts;public:void Add (String part)//Add parts to the product {Parts.push_back;} void Show ()//Displays the individual parts of the product {cout<<endl<< "product creation----" <<endl;for_each (Parts.begin (), Parts.end (), Showfun<string> ());//Output product}};class builder//abstract Builder {public:virtual void Buildparta () =0;//Create part avirtual void BUILDPARTB () =0;//Create part bvirtual product GetResult () =0;//returns the results of the products that were added to the part};class Concretebuilder1:public builder// Detailed builder 1, note that the default inheritance for C + + is private{private:product p;//built-in product class, aggregation relationship public:void Buildparta ()//Add part A{p.add ("Part A");} void BUILDPARTB ()//Add part B{p.add ("Part B"); Product GetResult ()//returns the Products object {return p;}}; Class Concretebuilder2:public builder//detailed builder 2{private:product p;//built-in product class. Aggregation relationship public:void Buildparta ()//Add part A{p.add ("Part X");} void BuildPartB ()//Add part B{p.add ("Part Y");} Product GetResult ()//returns the Products object {return p;}}; Class director//managers, to create the villain object {public:void Construct (builder& B)//using references to implement polymorphism {B.buildparta (); B.BUILDPARTB ();}}; int main () {Director director;/******** builder 1**********/builder &b1=concretebuilder1 ();d irector. Construct (B1);//Create B1. GetResult (). Show ();//Display the product created above/******* create this 2*******/builder &b2=concretebuilder2 ();d irector. Construct (B2); B2. GetResult (). Show ();//Display the product created above return 0;}

test Results

References:

[1] "Big talk design mode"

[2] The Zen of design patterns

[3] "Headfirst design mode"


C + + implementation of Builder mode

Related Article

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.