1. Concept
Separating a complex build from its presentation allows the same build process to create different representations. [Construction and presentation separation, different representations of construction]
The difference from the abstract factory: in the builder model, 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 model can enforce a process of construction in stages.
The building pattern is to encapsulate complex internal creation inside, and for external calls, only incoming builders and build tools are needed, and the caller does not need to be concerned about how the interior is built to create the finished product.
A simple example, such as a car, a lot of parts, a wheel, a steering wheel, an engine, a variety of small parts, and so on, a lot of parts, but more than that, how to assemble these parts into a car, the assembly process is complicated (requires good assembly technology), and the builder mode is designed to separate parts from assembly.
2.UML figure
3. Code
public interface Builder {
void Buildparta ();
void Buildpartb ();
void Buildpartc ();
Product getresult ();
}
//Concrete Build Tool
public class ConcreteBuilder implements Builder {
Part parta, PartB, PARTC;
public void Buildparta () {
//Here is the code that specifically constructs Parta
};
public void Buildpartb () {
//Here is the code specifically for how to build PARTB
};
public void Buildpartc () {
//Here is the code specifically for how to build PARTB
};
Public Product GetResult () {
//returns the final assembly result
};
}
//builder
public class Director {
Private builder Builder;
Public Director (builder builder) {
This.builder = builder;
}
public void construct () {
Builder.buildparta ();
BUILDER.BUILDPARTB ();
BUILDER.BUILDPARTC ();
}
}
public interface Product {}
Public interface part {}
Here's how to call Builder:
ConcreteBuilder builder = new ConcreteBuilder ();
Product Product = Builder.getresult ();
4. Application Scenarios
This pattern is javamail used in Java applications.
Java Builder pattern (go)