The builder model of C + + design pattern _c language

Source: Internet
Author: User

Builder model

In Gof, the foundation of design pattern reusable object-oriented software, it is said that the construction of a complex object is separated from its representation, so that the same build process can create different representations.

This remark, indefinitely. The creation of a complex object, usually composed of a number of child objects, and if an object can be created directly, it is not called a complex object. Because of the changes in the requirements of the project, the various parts of this complex object are often drastically changed, but the fact that they are grouped together to form a complex object, no matter how they change, will not change. The builder model provides a "encapsulation mechanism" to isolate the changes in the objects, and ultimately the process of combining them into complex objects does not change.

In the book "Dahua Design Pattern", an example is given to illustrate a good example ———— build a villain. The construction of a villain, to be divided into six steps: Head, body, left hand, right hand, left foot and right foot. Unlike the abstract factory model, the builder model is constructed step-by-step under the control of the director, and in the construction process, the builder's model can be more finely controlled. Regardless of the person's head, body, the left hand, right hand, left foot or right foot how to change, but eventually by these parts together to form a person, although it is the same construction process, but this person will have a different expression, such as fat, skinny, a tall, a low and so on.

UML diagrams

The class diagram is as follows:

The sequence diagram is as follows:

Code implementation

Copy Code code as follows:

/*
* * Filename:builderpattern
* * author:jelly Young
* * DATE:2013/11/22
* * Description:more information, http://www.jb51.net
*/

#include <iostream>
using namespace Std;

typedef enum MANTYPETAG
{
Kfatman,
Kthinman,
Knormal
}mantype;

Class Mans
{
Public
void Sethead (Mantype type) {m_type = type;}
void Setbody (Mantype type) {m_type = type;}
void Setlefthand (Mantype type) {m_type = type;}
void Setrighthand (Mantype type) {m_type = type;}
void Setleftfoot (Mantype type) {m_type = type;}
void Setrightfoot (Mantype type) {m_type = type;}
void Showman ()
{
Switch (m_type)
{
Case Kfatman:
cout<< "I ' m a Fat man" <<endl;
Return

Case Kthinman:
cout<< "I ' m a Thin Man" <<endl;
Return

Default
cout<< "I ' m a normal man" <<endl;
Return
}
}

Private
Mantype M_type;
};

Builder
Class Builder
{
Public
virtual void Buildhead () {}
virtual void Buildbody () {}
virtual void Buildlefthand () {}
virtual void Buildrighthand () {}
virtual void Buildleftfoot () {}
virtual void Buildrightfoot () {}
Virtual Mans *getman () {return NULL;}
};

Fatmanbuilder
Class Fatmanbuilder:public Builder
{
Public
Fatmanbuilder () {M_fatman = new Man ();}
void Buildhead () {m_fatman->sethead (Kfatman);}
void Buildbody () {m_fatman->setbody (Kfatman);}
void Buildlefthand () {M_fatman->setlefthand (Kfatman);}
void Buildrighthand () {M_fatman->setrighthand (Kfatman);}
void Buildleftfoot () {m_fatman->setleftfoot (Kfatman);}
void Buildrightfoot () {m_fatman->setrightfoot (Kfatman);}
Mans *getman () {return M_fatman}

Private
Mans *m_fatman;
};

Thismanbuilder
Class Thinmanbuilder:public Builder
{
Public
Thinmanbuilder () {M_thinman = new Man ();}
void Buildhead () {m_thinman->sethead (Kthinman);}
void Buildbody () {m_thinman->setbody (Kthinman);}
void Buildlefthand () {M_thinman->setlefthand (Kthinman);}
void Buildrighthand () {M_thinman->setrighthand (Kthinman);}
void Buildleftfoot () {m_thinman->setleftfoot (Kthinman);}
void Buildrightfoot () {m_thinman->setrightfoot (Kthinman);}
Mans *getman () {return M_thinman}

Private
Mans *m_thinman;
};

Director
Class Director
{
Public
Director (Builder *builder) {m_builder = Builder;}
void Createman ();

Private
Builder *m_builder;
};

void Director::createman ()
{
M_builder->buildhead ();
M_builder->buildbody ();
M_builder->buildlefthand ();
M_builder->buildrighthand ();
M_builder->buildlefthand ();
M_builder->buildrighthand ();
}

int main (int argc, char *argv[])
{
Builder *builderobj = new Fatmanbuilder ();
Director directorobj (builderobj);
Directorobj.createman ();
Man *manobj = Builderobj->getman ();
if (manobj = NULL)
return 0;

Manobj->showman ();
Delete builderobj;
Builderobj = NULL;

return 0;
};

The above example is miscellaneous, but it is also an application of the builder model. The following example is the most common and simplest way to implement a builder:

Copy Code code as follows:

/*
* * Filename:builderpattern
* * author:jelly Young
* * DATE:2013/11/23
* * Description:more information, http://www.jb51.net
*/

#include <iostream>
#include <vector>
using namespace Std;

Class Builder;

Product
Class Product
{
Public
void Addpart (const char *info) {m_partinfovec.push_back (info);}
void Showproduct ()
{
for (std::vector<const char *>::iterator item = M_partinfovec.begin ();
Item!= m_partinfovec.end (); ++item)
{
cout<<*item<<endl;
}
}

Private
Std::vector<const Char *> M_partinfovec;
};

Builder
Class Builder
{
Public
virtual void Buildparta () {}
virtual void Buildpartb () {}
Virtual Product *getproduct () {return NULL;}
};

ConcreteBuilder
Class Concretebuilder:public Builder
{
Public
ConcreteBuilder () {m_product = new Product ();}
void Buildparta ()
{
M_product->addpart ("Parta completed");
}

void Buildpartb ()
{
M_product->addpart ("PARTB completed");
}

Product *getproduct () {return m_product;}

Private
Product *m_product;
};

Director
Class Director
{
Public
Director (Builder *builder) {m_builder = Builder;}
void Createproduct ()
{
M_builder->buildparta ();
M_BUILDER-&GT;BUILDPARTB ();
}

Private
Builder *m_builder;
};

Main
int main ()
{
Builder *builderobj = new ConcreteBuilder ();
Director directorobj (builderobj);
Directorobj.createproduct ();
Product *productobj = Builderobj->getproduct ();
if (productobj = NULL)
{
return 0;
}
Productobj->showproduct ();
Delete builderobj;
Builderobj = NULL;
}

By comparing the two examples above, it is easy to abstract the skeleton of the builder pattern.

Use points

1. The objects generated by the builder model have complex internal structures that will be structured in a step-by-step way to construct a complex object, the number of steps to be determined, and the implementation of each step is different and may change frequently;

2. In the above example, we all see that the resulting man and product have no abstract class, this, in turn, derives from the application of a builder, when a complex object is created in a process where there is not much common characteristic of the complex object, it is difficult to abstract it, and when the assembly of the complex object has a certain similarity, The builder's model can play a role. To put it simply, the builder model may be used, and the final object may not be much of a relationship, and in this regard, it is most appreciated to read the builder model in the design pattern reusable object-oriented software base.

Summarize

A complex object is composed of multiple parts, and the builder pattern is to separate the creation of complex objects from the creation of parts, respectively, with builder classes and director classes. The last complex object is built with director, and the builder interface above encapsulates how to create a widget (a complex object is made up of these parts), that is, Director is responsible for assembling the part at the end of the product. This builder model allows decoupling of design and implementation.

It is easiest to confuse builders with abstract factory patterns when it comes to the builder model. Because this is all part of the creation design pattern, so there is a common point between the two, but the builder model focuses on the object mix, that is, different small objects form a whole complex large object, while the abstract factory model for interface programming, only to provide the factory interface to create objects, not responsible for the processing after the object.

Builder model, is a more complex, not easy to balance the design pattern. You should read more open source code to understand how others use the pattern. Learn the design pattern from the practical application.

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.