1.1BUILDER 1. It is intended to separate the construction of a complex object from its representation, so that different representations can be created during the same construction process. The construction is implemented by calling the abstract interface of the generator. The type (Representation) of the constructed product is determined by the sub-class instance of the generator. In this way, the pilot only needs
1.1 BUILDER 1. Intention to separate the construction of a complex object from its representation, so that the same construction process can create different representations. The construction is implemented by calling the abstract interface of the generator. The type (Representation) of the constructed product is determined by the sub-class instance of the generator. In this way, the pilot only needs
1.1 BUILDER
1. Intention
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
The construction is implemented by calling the abstract interface of the generator. The type (Representation) of the constructed product is determined by the sub-class instance of the generator. In this way, the pilot only needs to call the abstract interface of the generator to generate the product. The specific product is related to the specific generator instance configured by the pilot.
Finally, I understood how it separated the construction of complex objects from its representation, and why it was the same building process.
2. Motivation
3. Applicability
When an algorithm for creating a complex object is independent of the composition of the object and their assembly method, the composition and assembly method of the object are represented by the abstract interface of the generator. The algorithms created for objects are implemented by sub-classes of generators.
When the constructor must allow the constructed object to have different representations -- that is, different products can be generated in the same constructor. This is implemented by configuring the sub-class instance of the generator of the Guide.
4. Structure
5. Participants
L Builder: Specifies an abstract interface for each part of a product object. These components ultimately constitute the product, and the call to these abstract interfaces is the assembly of the product operation, the number of calls, input parameters, and so on, the final generated product is also different. The construction process of the product itself may be very complicated. However, these complexities are hidden from ctor. These abstract Interfaces describe the product composition and assembly methods.
L ConcreteBuilder: implements the Builder interface to construct and assemble each part of the product, defines and clarifies the representation it creates, and provides an interface for retrieving the product. This class implements the abstract interface of Builder, so that different representations can be created, but the composition and assembly process are the same.
L Director: constructs an object using the Builder abstract interface. More objects are assembled and produced based on the Builder interface.
L Product: a complex object to be constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process. classes that define components include interfaces that assemble these components into the final product.
6. Collaboration
L The customer creates a ctor object and configures it with the Builder object it wants.
L once the product is not only generated, the guide will notify the generator.
L The generator processes the pilot request and adds parts to the product.
L The customer retrieves the product from the generator.
7. Effect
L it enables you to change the internal representation of a product. The abstract interface provided by Builder to ctor allows the Builder to hide the representation and internal structure of this product. It also hides how the product is assembled. Because the product is constructed through an abstract interface, when you change the internal representation of the product, all you need to do is to define a new generator.
L it separates the constructor code from the representation code. The Builder mode encapsulates the creation and representation of a complex object to improve the modularity of the object. Each ConcreteBuilder contains all the code for creating and assembling a specific product.
L it allows you to control the construction process more precisely.
8. Implementation
L an abstract Builder class is usually required by the Guide to define an operation for each component created. These operations do not do anything by default. A ConcreteBuilder class is interested in creating a build to redefine these operations.
L assembly and construction interfaces: Generally, the result of the Construction request is only added to the product. In special cases, it must be returned to the Guide.
L there is no abstract class for the product: Generally, they do not have a public part. You can also set an abstract class.
L the default method in Builder is null.
9. Sample Code
Class MazeBuilder {
Public:
Virtual void BuildMaze () {}// component constructor
Virtual void BuildRoom (int room ){}
Virtual void BuildDoor (int roomFrom, int roomTo ){}
Virtual Maze * GetMaze () {return 0 ;}
Protected:
MazeBuilder ();
}; // MazeBuilder is a generator.
Class StandardMazeBuilder: public MazeBuilder {
Public:
StandardMazeBuilder ();
/*
*/
Virtual void BuildMaze ();
Virtual void BuildRoom (int );
Virtual void BuildDoor (int, int );
/*
*/
Virtual Maze * GetMaze ();
Private:
Direction CommonWall (Room *, Room *);
Maze * _ currentMaze;
}; // StandardMazeBuilder is a ConcreteBuilder that provides the Component Construction Code.
Maze * MazeGame: CreateMaze (MazeBuilder & builder ){
Builder. BuildMaze ();
Builder. BuildRoom (1 );
Builder. BuildRoom (2 );
Builder. BuildDoor (1, 2 );
Return builder. GetMaze ();
} // CreateMaze is the guide, which calls the abstract interface of the generator to complete the product construction process.
// The following Code describes the product construction process.
Maze * maze; // The final product
MazeGame game; // Director, Navigator
StandardMazeBuilder builder; // ConcreteBuilder, which is a construction class.
Game. CreateMaze (builder); // start assembly
Maze = builder. GetMaze (); // obtain the assembled product