1. Noun explanation

Separating the construction of a complex object from its representation allows the same construction process to create different representations.

For example, a computer including host, monitor, keyboard and other peripherals, these components constitute a complete computer. How to assemble these parts into a complete computer and return it to the user is the problem that the builder model needs to solve. Builder mode, also known as generator mode, is a more complex and less frequently used creation mode, as can be seen from the noun. The Builder pattern does not return a simple product for the client, but a complex product composed of multiple parts.

2. Builder pattern UML diagram

builder

The figure above contains the four main roles of builder mode:

  • Builder: Specifies abstract methods for creating parts of a Product object. Two classes of methods are generally declared in this interface. One is buildPartX(), which is used to create parts of a complex object. Another class of methods is getResult(), which are used to return complex objects. A Builder can be an abstract class or an interface.
  • ConcreteBuilder: This implements the Builder abstract method, implementing concrete construction and assembly methods for parts, defining and specifying the complex objects it creates, and providing a method to return the created complex product objects. Depend on Product.
  • Product (Product role) : It is a complex object that is built, consisting of multiple components, and the concrete builder creates an internal representation of the Product and defines its assembly process.
  • Director: Director is also called Director class, which is responsible for arranging the construction order of complex objects. There is an association between Director and abstract builder. The component construction and assembly method of builder object can be called in construct() to complete the construction of complex objects. The client typically only needs to interact with the conductor, determine the type of the concrete builder on the client side, instantiate the concrete builder object (also via configuration files and reflection mechanisms), and pass that object into the conductor class via the constructor or Setter method of the conductor class.

3. Implementation of builder mode

The following code is based on a Builder pattern implemented in Java.

@data
class Product {  
    private String name;  
    private String type;  
    public void showProduct(a){  
        System.out.println("Name:"+name);  
        System.out.println("Model number:"+type); }}/ / abstract classes
abstract class Builder {  
    public abstract void buildPart(String arg1, String arg2);  
    public abstract Product getProduct(a);  
}  

// Concrete builder
class ConcreteBuilder extends Builder {  
    private Product product = new Product();  

    public Product getResult(a) {  
        return product;  
    }  

    public void buildPart(String arg1, String arg2) { product.setName(arg1); product.setType(arg2); }}/ / commanders
public class Director {  
    private Builder builder;

    public Director(Builder builder) {
       this.builder=builder;
    }

    public void setBuilder(Builder builder) {
       this.builder=builer;
    } 
    public Product construct(a){  
        builder.buildPart("lenvono"."Y470");  
        returnbuilder.getResult(); }}// client call
public class Client {  
    public static void main(String[] args){ 
        Builder builder = new ConcreteBuilder(); 
        Director director = newDirector(builder); Product product = director.construct(); product1.showProduct(); }}Copy the code

All four roles are included in the above implementation, and the client makes the call, as far as the client is concerned, only the specific builder. An abstract constructor type object can be injected into the commander class. The core of this construct method is construct(), which calls the constructor method of the Builder object and returns a product object.

4. Different from factory mode

The advantages of the Builder pattern are encapsulation and easy to extend. As mentioned above, for the client, only the specific builder is concerned. The builder mode can be used to preferentially encapsulate changes, product and Builder are relatively stable, and the main business logic encapsulation in the control class can achieve better stability for the whole. If you want to extend it, you just add a new builder, with no effect on the previous code.

The Builder pattern is generally used to create more complex objects than the factory pattern, because the creation of objects is more complex, so the creation of objects is separated into a new class, the director class. In other words, the factory mode encapsulates the entire creation process of the object in the factory class, which provides the final product to the client. In the Builder mode, the builder class generally only provides the construction of each component in the product class, and delivers the specific construction process to the director class. The leader class is responsible for assembling the components into a product according to specific rules and delivering the product to the client.

5. To summarize

This article describes the builder pattern in design patterns. The larger category is the creator pattern. Firstly, the concept of builder mode is introduced. Then the class diagram of the Builder pattern is given and the four roles involved are explained. Also gives the code based on Java implementation; Finally, the advantages and differences between the factory model are discussed. The Builder pattern is mainly suitable for creating complex objects where the order of construction between the internal components of the object is stable, but the internal components of the object are subject to complex changes.

Subscribe to the latest articles, welcome to follow my official account

Wechat official account


reference

  1. Java and Patterns, Yan Hong
  2. 23 Design patterns (4) : Builder patterns