1#include <iostream>2#include <string>3 4 using namespacestd;5 6 classstproduct7 {8 Public:9 voidSetA (stringstr)Ten { OneM_a =str; A } - - voidSETB (stringstr) the { -M_b =str; - } - + voidSETC (stringstr) - { +M_c =str; A } at - voidShow () - { -cout<<"Product:"<< m_a<<"!\t"<< m_b<<"!\t"<< m_c<<Endl; - } - in Private: - stringm_a; to stringM_b; + stringM_c; - }; the * classStbuilder $ {Panax Notoginseng Public: - Stbuilder () the { +M_pstproduct =Newstproduct (); A } the Virtual~Stbuilder () + { - Deletem_pstproduct; $ } $ - Virtual voidBuildparta () =0; - Virtual voidBUILDPARTB () =0; the Virtual voidBUILDPARTC () =0; - Wuyi Virtualstproduct*GetResult () the { - returnm_pstproduct; Wu } - Aboutstproduct*m_pstproduct; $ }; - - classStconcretebuilder: PublicStbuilder - { A Public: + Virtual voidBuildparta () the { -M_pstproduct->seta ("A Style"); $ } the the Virtual voidBUILDPARTB () the { theM_PSTPRODUCT->SETB ("B Style"); - } in the Virtual voidBUILDPARTC () the { AboutM_PSTPRODUCT->SETC ("C Style"); the } the }; the + classStdirector - { the Public:Bayi~Stdirector () the { the DeleteM_pstbuilder; - } - the voidSetbuilder (stbuilder*Psttmpbuilder) the { theM_pstbuilder =Psttmpbuilder; the } - thestproduct*Construct () the { theM_pstbuilder->Buildparta ();94M_pstbuilder->BUILDPARTB (); theM_pstbuilder->BUILDPARTC (); the the returnM_pstbuilder->GetResult ();98 } About -stbuilder*M_pstbuilder;101 };102 103 intMainintargcChar*argv[])104 { thestbuilder* Pstbuilder =NewStconcretebuilder ();106 107stdirector* Pstdirector =Newstdirector ();108Pstdirector->Setbuilder (pstbuilder);109 thestproduct* pstproduct = pstdirector->Construct ();111cout<<"Show ... .... ......."<<Endl; thePstproduct->Show ();113 the return 0; the } the //////////////////////////////////////117[Email protected] ~/learn_code/design_pattern/10_builder]$.Builder118 Show ... .... .......119Product:a style! B style! C Style
Reference: http://design-patterns.readthedocs.io/zh_CN/latest/creational_patterns/builder.html
Design pattern-builder Mode/Generator mode (c + + implementation)