Builder mode of Design Pattern

Source: Internet
Author: User

The design patterns in the software field provide developers with an effective way to use expert design experience. The design model utilizes the important features of the object-oriented programming language: encapsulation, inheritance, polymorphism, true understanding
The essence of the design pattern 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. For more information, see
Design patterns: the basis for reusable object-oriented software (DP. This article introduces the implementation of 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: Construction
Build six parts in total: Head, body, left and right hand, 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 under control step by step
Constructed. 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 Code Implementation of the villain example is given below

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 ();}};
Int main ()
{
Fatbuilder thin;
Director dire( & Thin );
Director. Create ();
Return 0;
}

  

Builder mode of Design Pattern

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.