Analysis and Implementation of Common design patterns (C ++) 3-builder Pattern

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.

1) builder. h


/**//*
**************************************** ***************************
Created: 2006/07/19
Filename: builder. h
Author: Li Chuang

Http://www.cppblog.com/converse/

Purpose: Demo code in builder Mode
**************************************** ****************************
*/

# Ifndef builder_h

# Define builder_h


//
The virtual base class is the base class of all builder and provides interface functions for building different parts.
Class
Builder


{

Public:
Builder ()

{}
;

Virtual
~ Builder ()

{}



// Pure virtual functions, providing interface functions for building different parts


Virtual
Void builderparta ()
= 0
;

Virtual
Void builderpartb ()
=
0;
}
;


// Use builder to build products. The process of building products is the same, but different builders have different implementations.

// Different implementations are implemented through different builder Derived classes, and there is a builder pointer, through which multi-state invocation is implemented

Class
Director


{

Public:
Director (Builder
* Pbuilder );

~ Director ();


Void construct ();


PRIVATE:
Builder
* M_pbuilder;
}
;


// Builder's derived class to implement builderparta and builderpartb interface functions

Class
Concreatebuilder1
:
Public Builder


{

Public:
Concreatebuilder1 ()

{}


Virtual
~ Concreatebuilder1 ()

{}



Virtual
Void builderparta ();

Virtual
Void builderpartb ();
}
;


// Builder's derived class to implement builderparta and builderpartb interface functions

Class
Concreatebuilder2
:
Public Builder


{

Public:
Concreatebuilder2 ()

{}


Virtual
~ Concreatebuilder2 ()

{}



Virtual
Void builderparta ();

Virtual
Void builderpartb ();
}
;


# Endif

2) builder. cpp


/**//*
**************************************** ***************************
Created: 2006/07/19
Filename: builder. cpp
Author: Li Chuang

Http://www.cppblog.com/converse/

Purpose: Demo code in builder Mode
**************************************** ****************************
*/

# Include
"Builder. h
"
# Include
<Iostream
>

Void
Concreatebuilder1: builderparta ()


{
STD: cout
<
"Builderparta by concreatebuilder1/n
";
}


Void
Concreatebuilder1: builderpartb ()


{
STD: cout
<
"Builderpartb by concreatebuilder1/n
";
}


Void
Concreatebuilder2: builderparta ()


{
STD: cout
<
"Builderparta by concreatebuilder2/n
";
}


Void
Concreatebuilder2: builderpartb ()


{
STD: cout
<
"Builderpartb by concreatebuilder2/n
";
}


Director: Director (Builder
* Pbuilder)
: M_pbuilder (pbuilder)


{
}


Director ::
~ Director ()


{
Delete m_pbuilder;
M_pbuilder
= NULL;
}


//
The construct function indicates the entire construction process of an object. The assembly methods of different parts are the same,

// Build parta first, followed by partb, but different builders have different representations.

Void
Director: Construct ()


{
M_pbuilder
-> Builderparta ();
M_pbuilder
-> Builderpartb ();
}

3) Main. cpp


/**//*
**************************************** ***************************
Created: 2006/07/20
Filename: Main. cpp
Author: Li Chuang

Http://www.cppblog.com/converse/

Purpose: builder mode test code
**************************************** ****************************
*/

# Include
"Builder. h
"
# Include
<Stdlib. h
>

Int
Main ()


{
Builder
* Pbuilder1
=
New concreatebuilder1;
Director
* Pdirector1
=
New Director (pbuilder1 );
Pdirector1
-> Construct ();

Builder
* Pbuilder2
=
New concreatebuilder2;
Director
* Pdirector2
=
New Director (pbuilder2 );
Pdirector2
-> Construct ();

Delete pdirector1;
Delete pdirector2;

System (
"Pause
");


Return
0;
}

 

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.