Design Mode C ++ implementation (6) -- builder Mode

Source: Internet
Author: User

the design patterns in the software field provide developers with an effective way to use expert design experience. In the design model, an important feature of the object-oriented programming language is used: encapsulation, inheritance, and polymorphism, understanding the essence of the design model is a long process that requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: the basis for reusable object-oriented software" (DP. This article introduces the builder mode .

The builder mode separates the construction of a complex object from its representation, so that different representations (DP) can be created during the same construction process ). The "big talk Design Model" provides a good example: Building a villain requires six parts: Head, body, left and right hands, and left and right feet. Unlike the factory model, the builder model constructs a product step by step under the control of the pilot. Building a villain is constructed step by step under control. The Creator mode can control the building process more precisely, so as to control the internal structure of the obtained product more precisely. The following is a UML diagram of the builder mode to build a small artificial instance.


For the customer, you only need to know the guiding person. Through the guiding person, the customer can construct complex objects without knowing the specific construction process. The following is an example of a villain.Code.

Class builder {public: Virtual void buildhead () {} virtual void buildbody () {} virtual void buildleftarm () {} virtual void buildrightarm () {} virtual void buildleftleg () {} virtual void buildrightleg () {}}; // construct a thin class thinbuilder: Public builder {public: void buildhead () {cout <"build thin body" <Endl;} void buildbody () {cout <"build Thin head" <Endl;} void buildleftarm () {cout <"build thin leftarm" <Endl;} void buildrightarm () {cout <"build thin rightarm" <Endl;} void buildleftleg () {cout <"build thin leftleg" <Endl;} void buildrightleg () {cout <"build thin rightleg" <Endl ;}}; // construct the 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 ;}; // constructor 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 (); m_pbuilder-> buildleftleg (); m_pbuilder-> buildrightleg ();}};

Customer usage:

 
Int main () {fatbuilder thin; Director ctor (& Thin); Director. Create (); Return 0 ;}

So far,The five creation modes in design patterns: the basis for reusable object-oriented software are introduced, factory method mode, abstract factory mode, prototype mode, builder mode, and Singleton mode.

Design Mode C ++ implementation (1) -- factory Mode

 Design Mode C ++ Implementation (4) -- Singleton Mode

Design Mode C ++ implementation (5)-prototype mode and template method mode 


 I have a blogArticleCopyright, reprinted please indicate the source

Http://blog.csdn.net/wuzhekai1985

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.