An introduction to the Builder pattern of Java design patterns

  • 2020-04-01 03:41:26
  • OfStack

Builder pattern definition: separates the construction of a complex object from its presentation so that the same build process can create different representations.

The Builder pattern is to create a complex object step by step, allowing users to build complex objects simply by specifying their type and content. The user does not know the specifics of the internal build. The Builder pattern is very similar to the abstract factory pattern, with subtle differences that are probably only experienced over and over again.

Why use the builder pattern

Is to decouple the process of building a complex object from its parts. Note: decoupling processes and components.

Not only because of a complex object, there are a lot of a lot of components, such as cars, there are a lot of parts: wheel, the steering wheel, engine, there are all sorts of small parts, etc., many parts, but much more than that, how will these parts assembled into a car, the assembly process is also very complex (need good assembly technology), Builder pattern is to separate parts and assembly process.

How to use the builder pattern

First, it is assumed that a complex object is composed of multiple parts. The Builder pattern is to separate the creation of complex objects from the creation of parts, which is represented by Builder class and Director class respectively.

First, you need an interface that defines how to create the various parts of a complex object:


public interface Builder {
 //Create part A like the car wheel
 void buildPartA();
 //Create part B such as the car steering wheel
 void buildPartB();
 //Create part C such as car engine
 void buildPartC();
 //Returns the final assembly result (returns the last assembled car)
 //The assembly of the finished product is not done here, but is moved to the Director class below  //To achieve decoupling process and parts
 Product getResult();
}

Director is used to build the final complex object, while the Builder interface above encapsulates how to create parts (complex objects are composed of these parts), that is to say, Director is how to assemble the parts into finished products:


public class Director {
 private Builder builder;
 public Director( Builder builder ) {
  this.builder = builder;
 }
 //The part partA partB partC ends up as a complex object
 //Here is the process of assembling the wheels, steering wheel and engine into a car  public void construct() {
  builder.buildPartA();
  builder.buildPartB();
  builder.buildPartC();
 }
}

Concrete implementation of Builder:

1. Build or assemble parts of the product through specific completion of interface Builder;
2. Define and specify what it is creating;
3. Provide an interface to retrieve the product.


public class ConcreteBuilder implements Builder {
 Part partA, partB, partC;
 public void buildPartA() {
  //Here's the code for exactly how to build a partA
 };
 public void buildPartB() {
  //Here's the code for how exactly to build a partB
 };
 public void buildPartC() {
  //Here's the code for how exactly to build a partB
 };
 public Product getResult() {
  //Returns the final assembly result
 };
}

Complex object: Product Product:


    public interface Product { }

Parts of complex objects:

    public interface Part { }

Let's see how to invoke Builder mode:

ConcreteBuilder builder = new ConcreteBuilder();
Director director = new Director( builder );
director.construct();
Product product = builder.getResult();

Application of Builder mode

In practical use of Java, we often use the concept of "pools", which are used when the resource provider cannot provide enough resources and these resources need to be Shared repeatedly by many users.

A "pool" is actually a piece of memory, and when there are some "broken limbs" of complex resources in the pool (such as the connection pool of the database, and sometimes a connection will be broken), recycling these "broken limbs" will improve the efficiency of memory usage and improve the performance of the pool. Modify the Director class in Builder mode to diagnose which part the "broken limb" is on and then repair that part.


Related articles: