Design pattern C + + implementation (6)--builder mode __c++

Source: Internet
Author: User

Design patterns in the software domain provide a useful way for developers to use expert design experience. The design pattern uses the Object-oriented programming language the important characteristic: the encapsulation, the inheritance, the polymorphism, the true understanding design pattern essence is possibly a long process, needs the massive practical experience accumulation. Recently read the design pattern of the book, for each model, with C + + wrote a small example, deepen understanding. The main reference is "Dahua design pattern" and "design pattern: Reusable object-oriented Software Foundation" (DP) two books. This paper introduces the implementation of builder model.

The definition of a builder pattern separates the construction of a complex object from its representation, allowing the same build process to create different representations (DP). "Dahua Design Model" gave a good example-the construction of small people, the total need to build 6 parts, head, body, right hand, left and right foot. Unlike the factory model, the builder model constructs the product step-by-step under the control of the guiding person. To build a villain is to control the next step of construction. The creator pattern allows finer control over the build process, allowing finer control over the internal structure of the resulting product. A uml diagram of the builder pattern is given below to construct the villain as an example.


For the customer, just know the guide can, through the guide, customers can construct complex objects, without the need to know the specific construction process. The following gives the code implementation of the villain example.[CPP]  View plain copy print? class builder     {   public:       virtual  void buildhead ()  {}       virtual void buildbody ()  {}       virtual void buildleftarm () {}        virtual void buildrightarm ()  {}       virtual  Void buildleftleg ()  {}       virtual void buildrightleg ()  {}  };  //Construction thin person    class thinbuilder : public builder    {   public:       void buildhead ()  { cout< < "Build thin body" <<endl; }       void buildbody ()  { cout<< "Build thin head" <<endl; }     &nbSp; void buildleftarm ()  { cout<< "Build thin leftarm" &LT;&LT;ENDL;&NBSP;}        void buildrightarm ()  { cout<< "build thin  Rightarm "<<endl; }       void buildleftleg ()  { cout< < "Build thin leftleg" <<endl; }       void  Buildrightleg ()  { cout<< "Build thin rightleg" <<endl; }  };   //Construction Fat Man    class fatbuilder : public builder   {   public:       void buildhead ()  { cout<< "build fat  Body "<<endl; }       void buildbody ()  { cout<<" Build fat head "<<endl; }       void buildleftarm ()  { cout<< "bUild fat leftarm "<<endl; }       void buildrightarm ()  { cout<< "Build fat rightarm" <<endl; }        void buildleftleg ()  { cout<< "Build fat leftleg" <<endl; }        void buildrightleg ()  { cout<< "Build fat rightleg" <<endl; }  };  //Construction Commander    class director     {    private:       Builder *m_pBuilder;   public:        director (Builder *builder)  { m_pBuilder = builder;  }       void create () {            m_pbuilder->buildhead ();           m_pbuilder- >buildbodY ();           m_pbuilder->buildleftarm ();           m_pbuilder->buildrightarm ();  

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.