Problem:
If we want to write a builder class, we can write as follows:
Class Person
{
Public:
Void BuildHead ();
Void BuildBody ();
Void BuildArmLetf ();
Void BuildArmRight ();
Void BuildLegLeft ();
Void BuildLegRight ();
};
But what should we do when we want to build a thin, fat, and tall person?
Of course, we will think of defining the class of the builder above as an abstract class, and then defining the construction of thin human, fat human, and high human to implement pure virtual functions of abstract classes in turn.
In this way, we can separate the constructed object from its different performance, so that the same construction process can create different performance. This is the characteristic of the builder mode.
If we use the builder mode, users only need to specify the types to be built to get them, and the specific construction process and details do not need to be known.
Builder mode ):
Building a complex object is separated by its performance, so that the same building process can create different performance.
Builder mode (Builder) structure diagram:
Summary:
What is Builder: an abstract interface for creating sub-parts of a Product object
ConcreteBuilder: it is a specific Builder that implements the Builder interface and constructs and assembles sub-parts.
Product: specific Product roles
Director: The conductor who builds an object using the Builder interface. It is mainly used to create complex objects. The building sequence between these objects is usually stable, however, the internal construction of objects is usually subject to complex changes.
Benefits of builder mode:
The Construction Code is separated from the representation code. Because the builder hides how the product is assembled, if you need to change the internal representation of a product, you only need to define a specific builder.
Basic builder mode code:
Class Product
{
Public:
Void Add (String part); // Add product parts
};
Builder class ------- abstract Builder class, determine that the product consists of two parts: PartA and PartB, and declare a method to get the product build result: GetResult.
Class Builder
{
Public:
Virtual void BuildPartA () = 0;
Virtual void BuildPartB () = 0;
Virtual void GetResult () = 0;
};
ConcreteBuilder1 class ------- specific builder class
Class ConcreteBuilder1: public Builder
{
Private:
Product product = new Product ();
Public:
Void BuildPartA ()
{
Product. Add ("Part ");
}
Voidbucket partb ()
{
Product. Add ("Part B ");
}
Void GetResult ()
{
Return product;
}
};
ConcreteBuilder2 class ------- specific builder class
Class ConcreteBuilder2: public Builder
{
Private:
Product product = new Product ();
Public:
Void BuildPartA ()
{
Product. Add ("Part X ");
}
Voidbucket partb ()
{
Product. Add ("part Y ");
}
Void GetResult ()
{
Return product;
}
};
Ctor ctor class -------- conductor class
Class Director
{
Public:
Void Construct (Builder * pbuilder)
{
Pbuilder-> BuildPartA (); // used to command the construction process
Pbuilder-> BuildPartB ();
}
};
Client code. The customer does not need to know the specific construction process.
Void main ()
{
Director dire= newDirector ();
Builder * b1 = new ConcreteBuilder1 ();
Builder * b2 = new ConcreteBuilder2 ();
Director. Construct (b1 );
Director. Construct (b2 );
Product p1 = b1.GetResult (); // The conductor uses ConcreteBuilder1 to build the Product.
Product p2 = b2.GetResult (); // The conductor uses the ConcreteBuilder2 method to build the Product.
}