Builder mode of Design Pattern

Source: Internet
Author: User

Builder

Purpose:

Separate the construction of a complex object from its representation so that
The same construction process can create different representations.

Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

Comparison with the factory Model

The builder mode and abstractfactory mode have similar functions because they are used to create large and complex objects. The difference between them is that the builder mode emphasizes step-by-step object creation, different result objects can be obtained through the same creation process. Generally, objects in builder mode are not directly returned. In the abstractfactory mode, objects are directly returned. The abstractfactory mode emphasizes that the same interface is provided for creating multiple mutually dependent objects. The main function of the builder mode is to arrange the call sequence of the basic methods, that is, these basic methods have been implemented, while the factory method focuses on creation. What object do you want? I will create an object, the assembly sequence is not his concern.

C ++ implementation:

There are at least four categories: product, abstract construction, specific construction, and director. Product provides specific implementation interfaces, and abstract construction provides unified methods for specific construction classes, A specific construction class contains a commodity object and implements a specific method. The director class contains an abstract construction class object, which uniformly arranges a specific construction class method through a public method.

Sample Code:

// Builder. h # include <iostream >#include <string> using namespace STD; Class product {PRIVATE: String m_parta; string m_partb; string m_partc; public: void setparta (string ); void setpartb (string B); void setpartc (string C); string getparta (); string getpartb (); string getpartc () ;}; class abstractbuilder {public: virtual void buildparta () = 0; virtual void buildpartb () = 0; virtual void buildpartc () = 0; virtu Al product getproduct () = 0; // difference between the returned pointer and the returned variable}; Class concretebuilder1: Public abstractbuilder {public: Virtual void buildparta (); Virtual void buildpartb (); virtual void buildpartc (); product getproduct (); Private: Product m_product;}; Class concretebuilder2: Public synchronized actbuilder {public: Virtual void buildparta (); Virtual void buildpartb (); virtual void buildpartc (); product getproduct (); protected: privat E: Product m_product;}; class director {public: Director (abstractbuilder * builder );~ Director (); void construct (); // Private: abstractbuilder * m_builder;}; // builder. CPP # include "builder. H "Void product: setparta (string a) {m_parta = A;} void product: setpartb (string B) {m_partb = B;} void product :: setpartc (string c) {m_partc = C;} string product: getparta () {return m_parta;} string product: getpartb () {return m_partb;} string product :: getpartc () {return m_partc;} void concretebuilder1: buildpart A () {m_product.setparta ("build1parta");} void concretebuilder1: buildpartb () {m_product.setpartb ("build1partb");} void detail: buildpartc () {m_product.setpartc ("build1partc");} product concretebuilder1: getproduct () {return this-> m_product;} void partition: buildparta () {m_product.setparta ("build2parta ");} void concretebuilder2: buildpartb () {m_product.setpartb ("build2partb");} void con Cretebuilder2: buildpartc () {m_product.setpartc ("build2partc");} product concretebuilder2: getproduct () {return this-> m_product;} void Director: Construct () {m_builder-> buildparta (); m_builder-> buildpartb (); m_builder-> buildpartc ();} Director: Director (abstractbuilder * builder) {This-> m_builder = builder;} Director ::~ Director () {If (m_builder! = NULL) {Delete m_builder; m_builder = NULL ;}// main. CPP # include "builder. H "Void main () {Director * pdirector = new director (New concretebuilder1 (); pdirector-> construct (); cout <pdirector-> m_builder-> getproduct (). getparta (); Director * pdirector1 = new director (New concretebuilder2 (); pdirector1-> construct (); cout <Endl <pdirector1-> m_builder-> getproduct (). getparta () <Endl; return ;}
View code

 

References

1 C ++ design mode implementation-Builder

2 C ++ design mode-builder Mode

3. Design Mode-builder Mode

 

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.