Builder Mode Definition:
The construction of a complex object is separated from its representation, allowing the same build process to create different representations.
When I first came into contact with this model, I couldn't understand what it meant. So, the internet Google a lap, and finally get this universally accepted explanation:
The build pattern is a step-by-step creation of a complex object that allows users to build them only by specifying the type and content of complex objects, and the user does not know the specifics of the build inside.
Here is an example to illustrate the use of this pattern, the code is as follows:
Import java.util.ArrayList;
Interface builder{
public void Buildparta ();
public void Buildpartb ();
public void Buildpartc ();
Public Product getproduct ();
}
Class product{
Private arraylist<string> parts=new arraylist<string> ();
public void Add (String part) {
Parts.add (part);
}
public void Show () {
SYSTEM.OUT.PRINTLN ("product has the following components:");
for (String s:parts) {
System.out.println (s);
}
}
}
Class Worker implements builder{
Private product product;
public void Buildparta () {
Product=new Product ();
Product.add ("Part A");
}
public void Buildpartb () {
Product.add ("Part B");
}
public void Buildpartc () {
Product.add ("Part C");
}
Public Product getproduct () {
return product;
}
}
Class designer{
public void Order (Builder Builder) {
Builder.buildparta ();
BUILDER.BUILDPARTB ();
BUILDER.BUILDPARTC ();
}
}
public class Test {
public static void Main (string[] args) {
Designer designer=new Designer ();
Builder builder=new Worker ();
Designer.order (builder);
Product product =builder.getproduct ();
Product.show ();
}
}
The output results are as follows:
Product consists of the following parts:
Part A
Part B
Section C
From this example we can see that the builder model is a part of the process of building objects.
Summary: The builder model is primarily designed to decouple the process of building a complex object and its components. So that we don't have to care about how each part is assembled.