Design pattern-builder Mode/Generator mode (c + + implementation)

Source: Internet
Author: User

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-&GT;SETB ("B Style"); -         } in  the         Virtual voidBUILDPARTC () the         { AboutM_PSTPRODUCT-&GT;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)

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.