C ++ design mode-Builder

Source: Internet
Author: User

Purpose:
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

UML structure diagram:


Applicable to the following situations:

1) when creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.

2) When the constructor must allow different representations of the object to be constructed.

Abstract base class:
1) Builder: this base class is the abstraction of all object creation processes. It provides interface functions for building different components.

Interface:
1) Builder: buildparta, builder: buildpartb: is a function interface for building different parts of an object. The derived class of builder is implemented in detail.
There is also a function that requires attention, that is, the Director: Construct Function, in this function, the object construction is completed by calling the above two interface functions-that is, the assembly processes of different parts are consistent (the same construct function is called ), however, different construction methods have different representations (depending on the actual type of builder, how to build, that is, polymorphism)

Resolution:
The builder mode is based on the following situation: an object may have different components, and different creation objects of these parts may have different representations, however, each part is assembled in the same way. for example, a bicycle is composed of wheel blocks and so on (different parts of an object). Different brands produce different products (different construction methods ). although different brands build different bicycles, the construction process is the same (Oh, have you ever seen the wheels grow on the seat ?).
That is to say, the Director: Construct Function fixes the Assembly mode of each component, and the specific component of the Assembly is implemented by the builder's derived class.

Implementation:
The implementation of the builder mode is based on the following object-oriented design principles: 1) extract the changed part to form a base class and corresponding interface functions, what will not change here is that parta and partb will be created, and different creation methods will be changed. So we will extract the builder base classes and buildparta and buildpartb interface functions here. 2) the base class that will change is aggregated by means of aggregation, where Director aggregates the pointer of the builder class.

 

 

Sample Code:

Namespace designpattern_builder <br/>{< br/> class product1 {/*... */}; <br/> class product2 {/*... */}; </P> <p> // class builder <br/> class builder // abstract base class <br/>{< br/> public: <br/> virtual void builderparta () {}// provides the default implementation <br/> virtual void builderpartb () {}< br/> virtual void builderpartc () {}< br/> protected: <br/> Builder () {}< br/> }; </P> <p> // class concretebuilder1 <br/> class concretebuilder1: Public builder // create product1 <br/>{< br/> public: <br/> concretebuilder1 (): _ product (null) {}</P> <p> virtual void builderparta (){/*... */} <br/> virtual void builderpartb (){/*... */} <br/> virtual void builderpartc (){/*... */} </P> <p> virtual product1 * getproduct1 () {return _ product;} // return the created product1 object <br/> PRIVATE: <br/> product1 * _ product; <br/>}; </P> <p> // class concretebuilder2 <br/> class concretebuilder2: public builder // create product2 <br/>{< br/> Public: <br/> concretebuilder2 (): _ product (null) {}</P> <p> virtual void builderparta (){/*... */} <br/> virtual void builderpartb (){/*... */} <br/> virtual void builderpartc (){/*... */} </P> <p> virtual product2 * getproduct2 () {return _ product;} // return the created product2 object <br/> PRIVATE: <br/> product2 * _ product; <br/> }; </P> <p> // class director <br/> {<br/> public: <br/> // create an object (director does not know what the created object is, only the client that calls the function knows) <br/> void construct (builder * builder) <br/>{< br/> builder-> builderparta (); <br/> builder-> builderpartb (); <br/> builder-> builderpartc (); <br/>}< br/>}; <br/>}</P> <p> // client code: <br/>{< br/> using namespace designpattern_builder; </P> <p> Director ctor; </P> <p> // create the first object <br/> concretebuilder1 * pbuilder1 = new concretebuilder1 (); <br/> Director. construct (pbuilder1); <br/> product1 * product1 = pbuilder1-> getproduct1 (); </P> <p> // create the second object <br/> concretebuilder2 * pbuilder2 = new concretebuilder2 (); <br/> Director. construct (pbuilder2); <br/> product2 * product2 = pbuilder2-> getproduct2 (); <br/>}</P> <p>

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.