Design patterns in the software domain provide a useful way for developers to use expert design experience. The design pattern uses the Object-oriented programming language the important characteristic: the encapsulation, the inheritance, the polymorphism, the true understanding design pattern essence is possibly a long process, needs the massive practical experience accumulation. Recently read the design pattern of the book, for each model, with C + + wrote a small example, deepen understanding. The main reference is "Dahua design pattern" and "design pattern: Reusable object-oriented Software Foundation" (DP) two books. This paper introduces the implementation of builder model.
The definition of a builder pattern separates the construction of a complex object from its representation, allowing the same build process to create different representations (DP). "Dahua Design Model" gave a good example-the construction of small people, the total need to build 6 parts, head, body, right hand, left and right foot. Unlike the factory model, the builder model constructs the product step-by-step under the control of the guiding person. To build a villain is to control the next step of construction. The creator pattern allows finer control over the build process, allowing finer control over the internal structure of the resulting product. A uml diagram of the builder pattern is given below to construct the villain as an example.
For the customer, just know the guide can, through the guide, customers can construct complex objects, without the need to know the specific construction process. The following gives the code implementation of the villain example.[CPP] View plain copy print? class builder { public: virtual void buildhead () {} virtual void buildbody () {} virtual void buildleftarm () {} virtual void buildrightarm () {} virtual Void buildleftleg () {} virtual void buildrightleg () {} }; //Construction thin person class thinbuilder : public builder { public: void buildhead () { cout< < "Build thin body" <<endl; } void buildbody () { cout<< "Build thin head" <<endl; } &nbSp; void buildleftarm () { cout<< "Build thin leftarm" <<ENDL;&NBSP;} void buildrightarm () { cout<< "build thin Rightarm "<<endl; } void buildleftleg () { cout< < "Build thin leftleg" <<endl; } void Buildrightleg () { cout<< "Build thin rightleg" <<endl; } }; //Construction 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; } }; //Construction Commander 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 ();