Builder Mode C + + implementation 1 definition
Separating the construction of a complex object from his presentation allows the same build process to create different representations
Note: In the template method, the function of the parent class calling the subclass method is implemented, and the selective invocation of the method is implemented by the hooks. But the order of the whole is fixed, what to do first and what to do without the blocking by hooks.
The creator model is a pattern that adjusts this fixed order to make it work better.
2 class Diagram
Role Division:
Product class, implemented through template method patterns, with basic methods and template methods
Builder abstract Builder, which regulates the components of a product, is generally implemented by subclasses
ConcreteBuilder the concrete builder, implements all the methods of the abstract class definition and returns a well-created object
Director class, responsible for arranging the order of the existing modules and then telling the builders to start building
3 implementation
Class Product
{
Public
Product ();
~product ();
Private
String Productparta;
String PRODUCTPARTB;
String PRODUCTPARTC;
};
Class Builder
{
Public
Virtual ~builder ();
virtual void Buildparta () = 0;
virtual void buildpartb () = 0;
virtual void buildpartc () = 0;
Virtual product* getproduct () = 0;
Protected
Builder ();
Private
product* _pro;
};
Class Concretebuilder:public Builder
{
Public
ConcreteBuilder ();
~concretebuilder ();
void Buildparta ();
void Buildpartb ();
void Buildpartc ();
product* getproduct ();
};
Class Director
{
Public
Director (builder* bld);
~director ();
void Construct ()
{
_bld->buildparta ();
_BLD->BUILDPARTB ();
_BLD->BUILDPARTC ();
_bld->getproduct ();
}
Protected
Private
builder* _bld;
};
3 Applications
① Advantages
Encapsulation of
The builder is independent and easy to expand--"is the specific description of this so-called different order
Easy to control the details of the risk, the details of the problem encapsulated in the builder, so the non-impact
② Usage Scenarios
Same method, different order
Multiple parts can be assembled, but the resulting results are not
Product classes are very complex or different in order of invocation, resulting in different functions
When object creation is used in other objects in the system, these objects can be used in the builder mode to soar the creation of the object when the product object is not easily available for creation. This is a compensation method.
③ precautions
The builder model focuses on the part type and assembly process (order), which is the biggest difference between him and the factory model. Template methods focus on subclass control parent class
4 extensions
Use the template method in builder mode. It's already been used before.
The main function of the builder mode is the sequence of calls to the basic method, the assembly of the existing parts, and the factory model to build the wheels.
Design pattern-builder Mode C + + implementation