Builder pattern definition: Separate the construction of a complex object from its representation, so that the same construction process can create different representations.
The Builder pattern is to create a complex object step by step, which allows users to build them just by specifying the type and content of complex objects. Users do not know the specific internal construction details. The Builder mode is very similar to the abstract factory mode. The subtle differences can only be experienced in repeated use.
Why use builder mode
It is to decouple the process of building complex objects from its components. Note: It is the decoupling process and components.
Because a complex object not only has many large amounts of components, such as cars, there are many parts: wheels, steering wheel, engine, and various small parts, etc. There are many parts, but it is far more than these. How to assemble these parts into a car For automobiles, this assembly process is also very complicated (requires good assembly technology), and the Builder model is to separate the components and the assembly process.
How to use Builder Mode
First, let's assume that a complex object is composed of multiple components. The Builder pattern separates the creation of complex objects and the creation of components, and is represented by the Builder class and the Director class respectively.
First, an interface is needed that defines how to create various components of complex objects:
The code copy is as follows:
public interface Builder {
//Create component A, for example, create car wheels
void buildPartA();
//Create component B, such as creating a car steering wheel
void buildPartB();
//Create component C, such as creating a car engine
void buildPartC();
//Return the final assembly result (return to the last assembled car)
//The assembly process of the finished product is not carried out here, but is transferred to the Director class below.
//This will realize decoupling process and components
Product getResult();
}
Use Director to build the last complex object, and the above Builder interface encapsulates how to create components (complex objects are composed of these components), that is, how the content of Director is how to finally assemble the components into finished products:
The code copy is as follows:
public class Director {
private Builder builder;
public Director( Builder builder) {
this.builder = builder;
}
// Finally, partA partB partC is used to form a complex object
//This is the process of assembling the wheel steering wheel and engine into a car
public void construct() {
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
}
}
The specific implementation of Builder ConcreteBuilder:
1. Build or assemble product components by completing the interface Builder;
2. Define and clarify what specific things it wants to create;
3. Provide an interface that can reacquire the product.
The code copy is as follows:
public class ConcreteBuilder implements Builder {
Part partA, partB, partC;
public void buildPartA() {
//Here is the specific code of how to build partA
};
public void buildPartB() {
//Here is the specific code of how to build partB
};
public void buildPartC() {
//Here is the specific code of how to build partB
};
public Product getResult() {
//Return to the final assembly result
};
}
Complex Object: Product:
The code copy is as follows:
public interface Product { }
Components of complex objects:
The code copy is as follows:
public interface Part { }
Let's see how to call the Builder mode:
The code copy is as follows:
ConcreteBuilder builder = new ConcreteBuilder();
Director director = new Director( builder);
director.construct();
Product product = builder.getResult();
Builder mode application
In actual Java use, we often use the concept of "pool". When the resource provider cannot provide sufficient resources and these resources need to be shared repeatedly by many users, we need to use the pool.
"Pool" is actually a piece of memory. When there are some "breaking limbs" of complex resources in the pool (such as the database connection pool, maybe sometimes a connection will be interrupted), if these "breaking limbs" are recycled, memory usage will be improved. Efficiency, improve pool performance. Modify the Director class in Builder mode to diagnose which component the "breaking limb" is broken, and then repair this component.