1. Concept
Separates a complex build from its representation so that different representations can be created during the same build process. [Construction and representation separation, different representations are constructed in the same way]
Difference from Abstract Factory: In the builder mode, there is a mentor who manages the builder. the user is in contact with the mentor, and the mentor contacts the builder to get the product. That is, the construction mode can enforce a step-by-step construction process.
The construction mode encapsulates complex internal creation in the Internal. For external callers, they only need to pass in the builder and construction tool. The caller does not need to care about how the internal creation is finished.
For example, there are many components in a car, such as many parts, wheels, steering wheel, engine, and other small parts. But how can we assemble these components into a car, this assembly process is also very complex (requires good assembly technology). The Builder mode is to separate parts from assembly.
2. UML diagram
3.Code
Public Interface Builder {
Void Buildparta ();
Void Buildpartb ();
Void Buildpartc ();
Product getresult ();
}
// Specific construction tool
Public Class Concretebuilder Implements Builder {
Part parta, partb, partc;
Public Void Buildparta (){
// Here is how to build the parta code
};
Public Void Buildpartb (){
// Here is how to build the partb code
};
Public Void Buildpartc (){
// Here is how to build the partb code
};
Public Product getresult (){
// Returns the final assembly result.
};
}
// Builder
Public Class Director {
Private Builder;
Public Director (Builder ){
This . Builder = builder;
}
Public Void Construct (){
Builder. buildparta ();
Builder. buildpartb ();
Builder. buildpartc ();
}
}
Public Interface Product {}
Public Interface Part {}
The following describes how to call builder:
Concretebuilder =NewConcretebuilder ();
Director dire=NewDirector (builder );
Director. Construct ();
Product = builder. getresult ();
4. Application scenarios
This mode is used in Java applications.