The implementation of the builder model in C + + design pattern programming _c language

Source: Internet
Author: User

Builder pattern: Separates the construction of a complex object from its representation, allowing the same build process to create different representations. This is the standard expression of the builder's model, but looking confusing, what is the separation of construction and representation? After an object is constructed using a constructor, it is not fixed, is it the only way to change its properties? And the same build process to make different representations, how is it possible? Write a few more constructors?

Actually write a few more constructors, depending on the parameters to set the different properties of the object, you can also achieve this effect, but this is very troublesome, each time to add a representation is added a constructor, the future constructor will be too much even remember, this violates the open-closed principle.

Or you can only design a few set functions, each time the property is different, I will construct an object, and then use the SET function to change the object's properties. This also can achieve the effect. It's just that the code will be very redundant, every place to use this object, write a few sentences, once the object a little bit of change, but also to change everywhere, so it is easy to make mistakes, then others look at this god logic and God code estimates will collapse. And it goes against the principle of reliance on reversal.

So the great God began to think, can not add a lot of constructors, and can not directly use a bunch of set functions, and then found that some of the construction of objects is a fixed number of steps, like a pipeline, any product is through each of the fixed steps together. For example, say a mobile phone, first put the motherboard, then put the screen, then put the battery, then put the shell, paste a film can sell thousands of, every time the introduction of new products, to change a better motherboard, change a larger screen, and then the whole large-capacity battery, paste a super cow B high permeable film, and can sell a new price. That is to say, none of these steps has changed, just the things of each part.

This is the great God, through the phenomenon to see the essence, basically have changed, there is no change, that is good, the object-oriented one of the important guiding ideology is to package the isolation of change, leaving unchanged. So they used a builder class to encapsulate each part of the step, the main function of this class is to produce each component, and then to abstract the elevation of ascension, so that rely on the reverse, so that each time you just need to add a class, this class or a few parts, but the internal implementation is not the same, This satisfies the principle of openness-closure. But there is still a problem, light has builder class not yet, although each part of the product has a corresponding function, but the use of words, or with the previous set function, like, a use of a large number of functions, that is, the change is encapsulated, but this unchanged thing has not been left out. At this point, add a director class, this class is specifically set to assemble the product steps, so as long as to tell director use which builder, can produce different products, for the client, only to see the use of a director construct function, is very convenient.

And in turn look at the definition of the builder pattern, construction refers to the production of a product of the steps, that is, each product part of the specific implementation, through the Director package steps, through the builder package product part of the implementation, and then put him two isolated, can be isolated, leaving unchanged for the client to use.

As you can see in the picture, product is a must know, there is no abstraction, but the product can be composed of different parts. Director in the construct is also fixed, no abstraction, if you want to change the steps, but also add a function, or a diector, so the builder model is generally applied to the steps do not occur large changes, and the product will change greatly.

Common scenarios
the StringBuilder in C # is an example of a builder, but only a builder, a director is missing, not a complete builder model. The builder pattern is generally applied to the process of building a product (also known as an algorithm) unchanged, and the exact implementation of each step changes dramatically.

Advantages
1. Isolate the construction steps and specific implementations, providing flexibility for the specific implementation of the product.

2. Encapsulates and abstracts the implementation of each step to achieve the dependency reversal principle.

3. Encapsulates the specific steps to reduce the redundancy of the code.

Disadvantages
1. The steps required to build a product (algorithm) are not drastic changes, preferably unchanged, thus affecting flexibility.

Instance

#include "stdafx.h" #include <stdlib.h> #include <iostream> using namespace std; 
 Abstract class, which is used to arrange the specific process of creating a person, other classes must follow this process, but can implement class Cpersonbuilder {public:virtual void buildhead () = 0. 
 virtual void buildbody () = 0; 
 virtual void buildarmleft () = 0; 
 virtual void buildarmright () = 0; 
 virtual void buildlegleft () = 0; 
virtual void buildlegright () = 0; 
  
};  Create Skinny class Cthinpersonbuilder:public Cpersonbuilder {Public:cthinpersonbuilder () {cout<< "is creating 
 Thin Person "<<endl<<endl; 
 } ~cthinpersonbuilder () {cout<< "is finished to thin person" <<endl<<endl; 
 } public:void Buildhead () {cout<< "Buildhead" <<endl; 
 } void Buildbody () {cout<< "buildbody (thin)" <<endl; 
 } void Buildarmleft () {cout<< "Buildarmleft" <<endl; 
 } void Buildarmright () {cout<< "buildarmright" <<endl; } void Buildlegleft () {cout<< "Buildlegleft" << Endl; 
 } void Buildlegright () {cout<< "buildlegright" <<endl; 
 } 
}; Create a fat class cfatpersonbuilder:public Cpersonbuilder {Public:cfatpersonbuilder () {cout<< "is creating f 
 At person "<<endl; 
 } ~cfatpersonbuilder () {cout<< ' is finished for fat person ' <<endl; 
 } public:void Buildhead () {cout<< "Buildhead" <<endl; 
 } void Buildbody () {cout<< "buildbody (Fat)" <<endl; 
 } void Buildarmleft () {cout<< "Buildarmleft" <<endl; 
 } void Buildarmright () {cout<< "buildarmright" <<endl; 
 } void Buildlegleft () {cout<< "Buildlegleft" <<endl; 
 } void Buildlegright () {cout<< "buildlegright" <<endl; 
  
} 
}; 
 Conductor class, used to command the creation of the person is thin or fat class Cpersondirector {public:cpersondirector (Cpersonbuilder *p) {this->m_p=p; 
 const void Createperson (void) const {m_p->buildhead (); 
 M_p->buildbody (); M_p->buildarMleft (); 
 M_p->buildarmright (); 
 M_p->buildlegleft (); 
 M_p->buildlegright (); 
} Private:cpersonbuilder *m_p; 
  
  
  
 
}; int _tmain (int argc, _tchar* argv[]) {cout<< "---------Builder mode test Case------------------------" <<endl<< 
  
 Endl 
 Cthinpersonbuilder *p_tp=new Cthinpersonbuilder (); 
 Cpersondirector *p_dtp=new Cpersondirector (P_TP); 
 P_dtp->createperson (); 
 Delete p_tp; 
 Delete P_DTP; 
 P_tp=null; 
 P_dtp=null; 
 
 cout<<endl<<endl; 
 Cfatpersonbuilder *p_fp=new Cfatpersonbuilder (); 
 Cpersondirector *p_dfp=new Cpersondirector (P_FP); 
 P_dfp->createperson (); 
 Delete p_fp; 
 Delete P_DFP; 
 P_fp=null; 
 P_dfp=null; 
 System ("pause"); 
return 0; 
 }

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.