There are many examples of builder in life, and personally feel that college life is the best experience of a builder model: to complete a university education, the university education process is generally divided into 4 semesters, so no study can be seen as a part of the construction of a complete university education process, Each person has a different final result after the 4-year (4-phase) build process, because many parameters may be introduced in the four-phase build (each person's chances and encounters are not exactly the same).
The problem with the builder model is that when the objects we're creating are complex (often made up of a lot of other objects), we want to separate the creation of complex objects from the representation (presentation) of the object, and the benefit of this is to build the complex object step-by-step, Because parameters can be introduced during each step of the construction process, the presentation of the resulting objects after the same steps are created is different.
The UML structure of the builder mode is shown in Figure 1:
The key to the builder pattern is that the Director object does not return objects directly, but rather steps through the creation of the object step by step (BUILDPARTA,BUILDPARTB,BUILDPARTC). Of course, here the Director can provide a default interface for returning objects (that is, the creation of a generic complex object that returns a parameter that is not specified or uniquely specified in Buildpart).
The implementation of the builder pattern is based on several object-oriented design principles:
1) The change of the part to form a base class and corresponding interface functions, in this will not change is to create Parta and PARTB, the change is different creation method, so the builder base class and the BUILDPARTA,BUILDPARTB interface functions are extracted here
2) Aggregates the base classes that change in aggregation, where the director aggregates the pointer of the builder class.
Above, through two derived classes ConcreteBuilder1, ConcreteBuilder2 defines two different construction details (the construction step is the same, determined by the construct function), through two derived classes of objects constructed by The properties or functions shown outside are different, as determined by the method of construction (Buildparta, BUILDPARTB, BUILDPARTC) in the respective builder derived classes.
Builder.h
#ifndef _builder_h_#define _builder_h_#include <string> #include <vector>using namespace std;//product class Product{private:string M_parta; String M_PARTB; String m_partc;public:void Setparta (const string& s); void SETPARTB (const string& s); void SETPARTC (const string& s); Product (); ~product ();};/ /abstract Builder base class, defining different parts of the creation interface class builder{public:virtual void Buildparta () = 0; virtual void buildpartb () = 0; virtual void buildpartc () = 0; Virtual product* getproduct () = 0; Builder (); Virtual ~builder ();};/ /Builder derived classes that implement the Builderparta and BUILDERPARTB and BUILDPARTC interface functions class Concretebuilder1:public Builder{public: ConcreteBuilder1 (); ~concretebuilder1 (); virtual void Buildparta (); virtual void BUILDPARTB (); virtual void buildpartc (); Virtual product* getproduct ();p rivate:product* m_pproduct;};/ /Builder derived classes that implement the Builderparta and BUILDERPARTB and BUILDPARTC interface functions class Concretebuilder2:public Builder{public: ConcreteBuilder2 ();~concretebuilder2 (); virtual void Buildparta (); virtual void BUILDPARTB (); virtual void buildpartc (); Virtual product* getproduct ();p rivate:product* m_pproduct;};/ /concretebuilder1 and ConcreteBuilder2 are the builder's two derived classes for implementing two different construction details//building products using builder, the process of building products is consistent, but different builders have different implementations// This different implementation is implemented by different builder-derived classes, and there is a builder pointer that enables polymorphic invocation of Class Director{public:director (builder* pBuilder); ~director (); The Construct function defines the entire build process of an object, and the assembly method between the different parts is consistent,//first build Parta followed by PARTB, but according to different builders there will be different representations of void Construct (); void Construct (const string& Buildpara);p rivate:builder* M_pbuilder;}; #endif
Director.cpp
#include "Builder.h" #include <iostream> #include <vector>using namespace std; Product::~product () {}product::P roduct () {}void Product::setparta (const string& s) {This->m_parta = s;} void PRODUCT::SETPARTB (const string& s) {THIS->M_PARTB = s;} void PRODUCT::SETPARTC (const string& s) {THIS->M_PARTC = s;} Builder::builder () {}builder::~builder () {}concretebuilder1::concretebuilder1 () {this->m_pproduct = new Product () ; cout<< "Create Empty product!" <<endl;} void Concretebuilder1::buildparta () {This->m_pproduct->setparta ("A"); cout<< "Buildparta" <<ENDL;} void Concretebuilder1::buildpartb () {THIS->M_PPRODUCT->SETPARTB ("B"); cout<< "BUILDPARTB" <<ENDL;} void Concretebuilder1::buildpartc () {THIS->M_PPRODUCT->SETPARTC ("C"); cout<< "BUILDPARTC" <<ENDL;} product* concretebuilder1::getproduct () {return this->m_pproduct;} Concretebuilder1::~concretebuilder1 () {Delete this->M_pproduct; This->m_pproduct = NULL;} Concretebuilder2::concretebuilder2 () {this->m_pproduct = new Product (); cout<< "Create Empty product!" <<endl;} void Concretebuilder2::buildparta () {This->m_pproduct->setparta ("A"); cout<< "Buildparta" <<ENDL;} void Concretebuilder2::buildpartb () {THIS->M_PPRODUCT->SETPARTB ("B"); cout<< "BUILDPARTB" <<ENDL;} void Concretebuilder2::buildpartc () {THIS->M_PPRODUCT->SETPARTC ("C"); cout<< "BUILDPARTC" <<ENDL;} product* concretebuilder2::getproduct () {return this->m_pproduct;} Concretebuilder2::~concretebuilder2 () {Delete this->m_pproduct; This->m_pproduct = NULL;} Director::D irector (builder* pBuilder) {this->m_pbuilder = PBuilder;} void Director::construct () {This->m_pbuilder->buildparta (); THIS->M_PBUILDER->BUILDPARTB (); THIS->M_PBUILDER->BUILDPARTC ();} Director::~director () {Delete this->m_pbuilder; This->m_pBuilder = NULL;}
main.cpp
#include "Builder.h" #include <iostream>using namespace Std;int main () { director* pdirector = new Director (new ConcreteBuilder1 ()); Pdirector->construct (); director* PDirector1 = new Director (new ConcreteBuilder2 ()); Pdirector1->construct (); return 0;}
The builder pattern and the factory pattern are very similar, but there are differences:
The main function of the builder mode is the invocation order of the basic method, which means that these basic methods have been implemented, while the factory method is focused on creating, what object do you want me to create an object out of, assembly order is not his concern.
The scenario used by the builder pattern is that the product class is very complex, or the sequence of calls in the product class produces different performance, which is very appropriate when using the builder pattern.
C++builder Builder Model Details-design mode (4)