Design mode: Builder mode (builder)

Source: Internet
Author: User

The builder pattern is somewhat similar to the previous abstract factory pattern, but more powerful than the abstract factory pattern, the builder pattern can be seen as two parts, one builder, and the other is that the basic idea of Director,builder is the abstract factory, While the director exists to make the use of builder more flexible, here is the Builder code example:

Class A {public:int i; A (): I (1) {};}; Class B {Public:char C; B (): C (' a ') {};}; Class AB {public:a A; b b;}; Class Builder {protected:ab* P;public:builder () {p = new AB ();} virtual void addABy1 () = 0;virtual void AddABy2 () = 0;virtual void addBBy1 () = 0;virtual void AddBBy2 () = 0; ab* Getab () {return p;}}; Class Builder1:public Builder {public:void addABy1 () {p->a.i + = 1;} void AddABy2 () {p->a.i + = 2;} void AddBBy1 () {P->B.C + = 1;} void AddBBy2 () {P->B.C + = 2;}}; Class Builder2:public Builder {public:void addABy1 () {p->a.i + = 1*3;} void AddABy2 () {p->a.i + = 2*3;} void AddBBy1 () {P->B.C + = 1*3;} void AddBBy2 () {P->B.C + = 2*3;}};

A, B, AB is not the focus of our attention, A, B can be seen as the components of AB, builder's role is to create AB objects, different builder according to their own needs in different ways to create AB objects, in the above code there is a base class builder, It provides a series of interfaces for derived classes, and the two derived classes Builder1 and Builder2 each redefine these interfaces, and other derived builder can be defined if the client programmer needs to create an AB object in another way. As you can see, builder is basically based on an abstract factory pattern.

The power of the builder is that there is also a director, noting that there are two ways to deal with a in builder: AddABy1 and AddABy2, which provides a choice between using the former or using the latter, in fact, the director is the master of the matter, So the presence of the Director makes the use of builder more flexible and can look at the director's code:

Class Constructor {builder *pc;public:constructor (builder *q): PC (q) {}void construct ();}; void Constructor::construct () {pc->addaby1 ();p c->addbby2 ();};
The constructor here is a director, which holds a pointer to a builder and uses the pointer in construct () to select the appropriate method to construct the AB object, which means that builder first made an AB blank, Provide a series of methods to deal with this blank, and finally by the constructor choose the method to deal with this blank, and finally get a finished product. Examples of use of the builder pattern are as follows:

int main () {Builder *p = new Builder1 (); Constructor c (P); c.construct (); return 0; }





Design mode: Builder mode (builder)

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.