Design Pattern Learning Summary: (9) Builder mode

Source: Internet
Author: User

The builder mode, the builder mode, is the same as the previous factory to solve the problem of object creation.

Intention:

Separating the construction of a complex object from its implementation allows the same build process to create different representations.

The problem is thrown and what is the construction process of complex objects.

Code:

classcar{ Public:    voidInit ()//Construction    {         This-Designprototype ();  for(inti =0; I <4; i++)        {             This-Wheelinstall (i); }         This-Smartinit ();  This-systemtest (); }protected:    Virtual voidDesignprototype () =0; Virtual voidWheelinstall (int)=0; Virtual voidSmartinit () =0; Virtual voidSystemtest () =0;};

The constructor is not put into the constructor because the constructor cannot dynamically bind this because the subclass constructor calls the constructor of the parent class, and when the parent class constructor executes the dynamically bound statement, the corresponding function is queried with the virtual function table through this (if you do not know what to study). When the subclass is not initialized yet, an error is thrown.

Init () is the object building process, where the process is stable.

classFerrari: Publiccar{string_name ="Ferrari"; Virtual voidDesignprototype (); Virtual voidWheelinstall (int); Virtual voidSmartinit (); Virtual voidsystemtest (); Public:    voidSayname () {cout<< _name <<Endl; } Ferrari () {car::init ();};voidFerrari::d esignprototype () { This->_name ="Ferrari"; cout<<"Design Car prototype ... "<<Endl; //Actual implementation code ... cout <<"prototype finished, ready to go to the next stage"<<Endl;}voidFerrari::wheelinstall (intTime ) {cout<<"Initialize the first of"<< Time <<"a wheel"<<Endl; cout<<"in Assembly"<<Endl; //implementation code, such as Wheel classcout <<"The wheels are assembled and ready for the next phase."<<Endl;}voidFerrari::smartinit () {cout<<"initialization of the motorized layer"<<Endl; cout<<"The maneuver layer is normal and goes to the next stage."<<Endl;}voidferrari::systemtest () {cout<<"Automotive Performance Testing"<<Endl; cout<<"Security Testing"<<Endl; cout<<"try to run normally ."<<Endl; cout<<"Build Success"<<Endl;}

Test:

It seems that this has been able to meet our needs to a great extent, and the results seem to be perfect.

What is the structure of the builder mode?

Structure diagram:

In fact, it is a large class above us, divided into Director,builder, and product (it itself), Director copy init work, builder copy every small step, so director is a stable class, Car construction process is relatively stable, and builder is unpredictable, because every step of it can be changed, at least the code above us, is the wheel assembly this piece, if we have a wheel class, then I still imagine that Ferrari and BMW's wheels should be different, The wheel construction process should also be changeable, of course, this assumption is abstract, just to cater for such a pattern, not to be too serious at all.

So the builder mode should be:

Fewer product Responsibilities:

 class   car{ string   _name;  public  :  virtual  ~car () {}  virtual  void  sayname () {cout << _name << Endl;  string  getName () {return   _name;  void  setName (string  name) {_ Name = name;}};  
class Ferrari: Public  car{public:    void  sayname ()    {         " i m a car,my name is: " << getName () << Endl;    }};

Builder

classcarbulider{protected: Car*_car; Virtual voidDesignprototype () =0; Virtual voidWheelinstall (int) =0; Virtual voidSmartinit () =0; Virtual voidSystemtest () =0; Virtualcar* GetResult () {return_car;}; FriendclassDirector; Public:    Virtual~Carbulider () {}};

Concrete Builder:

classFerraribulider: Publiccarbulider{Virtual voidDesignprototype (); Virtual voidWheelinstall (int); Virtual voidSmartinit (); Virtual voidsystemtest (); Public: Ferraribulider () {}};voidFerraribulider::d esignprototype () {cout<<"Design Car prototype ... "<<Endl; //Actual implementation code ... Carbulider::_car =NewFerrari ();//this creates the object, which will be released by the client .Carbulider::_car->setname ("Ferrari"); cout<<"prototype finished, ready to go to the next stage"<<Endl;}voidFerraribulider::wheelinstall (intTime ) {cout<<"Initialize the first of"<< Time <<"a wheel"<<Endl; cout<<"in Assembly"<<Endl; //implementation code, such as Wheel classcout <<"The wheels are assembled and ready for the next phase."<<Endl;}voidFerraribulider::smartinit () {cout<<"initialization of the motorized layer"<<Endl; cout<<"The maneuver layer is normal and goes to the next stage."<<Endl;}voidferraribulider::systemtest () {cout<<"Automotive Performance Testing"<<Endl; cout<<"Security Testing"<<Endl; cout<<"try to run normally ."<<Endl; cout<<"Build Success"<<Endl;}

Diretor:

class director{    public:    Director () {  }    Car* Construct (Carbulider &);}; Car* director::construct (Carbulider &b) {      // pointer memory leak issue, if so call construct (new Carbulider).     B.designprototype ();      for (int04; i++)    {        B.wheelinstall (i);    }    B.smartinit ();    B.systemtest ();     return B.getresult ();}

How to use:

int Main () {    Ferraribulider B;    Director D;     *f = d.construct (b);    F-sayname ();     return 0 ;}

Results:

The details are a headache, C + + is implemented in many ways, and you worry about memory leaks. So the user decides to change the builder to a reference. But this time again design to the const problem, if with const then the corresponding function declaration should also change.

Design Pattern Learning Summary: (9) Builder mode

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.