Design mode (5. Builder mode)

Posted by amir1985 on Sun, 23 Jan 2022 06:26:10 +0100

Builder mode (generator mode): used to decouple product and product construction process;

Mode 1: 4 roles:
1. Product - specific object
2. Abstract Builder - defines the builder's public method
3. Specific Builder - specific construction realization
One product attribute, multiple construction methods, and one return product method;
The product is used as an attribute, the construction method is passed in, multiple construction methods modify the product, and a get product method returns the final product
4. Commander - call the builder method to construct specific products
A method to create product objects is passed to the abstract constructor, the constructor calls several construction methods, and finally calls the builder's product return method to get the product.
Usage: create the commander object, call the method to create the product object, and return the specific object built by the commander calling the builder;
Mode 2: three roles:
1. Product - specific object
2. Abstract Builder - defines the builder's public method
3. Specific Builder - specific construction realization
One product attribute, multiple construction methods (each method returns this), and one return product method
The product is used as an attribute, and the construction method is passed in. Multiple construction methods modify the product and then return this to facilitate chain call. A get product method returns the final product built;
Use: new a builder, chain calls the construction method, and finally calls the builder's product return method to get the product.
Difference: mode 2 saves the commander, which is more flexible and more in line with the definition. The change content can be defined freely according to user needs, and there is no need to change the specific construction method. Can produce different complex products;

In this paper, demo code implements the builder mode of < mode 2 and 3 roles >.

Story background:

At 10:00 p.m. one day in Shenzhen Science and Technology Park, the programmer Xiao Li just finished sending the version and got off work. He was hungry. He thought that the version was OK this time and the problems he encountered had been solved. He took out his mobile phone and prepared to see where the accessories were delicious. At a glance, he saw the landlord's information, "Hello, the rent of x month is 2xxx, please come before xx...", Xiao Li looked at the balance of his card, I've been in Shenzhen for more than a year. I know that a fried rice noodle shop behind the company is good. Let's go there;
When he came to the roadside fried noodles stall, the "authentic Chaoshan beef noodles" right above the stall was particularly obvious. He had been here many times. This time, the distribution was smooth. Xiao Li said to the stall owner, "handsome boy, have a fried rice noodles, add beef, ham sausage, eggs, bean sprouts, cabbage and spicy"

UML class diagram:

Code implementation:

import lombok.Data;

/**
 * Builder pattern 
 */

//Specific products - fried noodles
//I stole a lazy and neat code here and used lombok
@Data
class Product {
    private String buildA;  //River flour
    private String buildB;  //Fine powder
    private String buildC;  //powder
    private String buildD;  //beef
    private String buildE;  //Ham sausage
    private String buildF;  //egg
    private String buildG;  //Bean sprouts
    private String buildH;  //Chinese cabbage
}

//Abstract builder
abstract class Builder {
    //Stir-Fried Rice Noodles
    abstract Builder bulidA(String mes);

    //Fried fine powder
    abstract Builder bulidB(String mes);

    //Fried noodles
    abstract Builder bulidC(String mes);

    //Add beef
    abstract Builder bulidD(String mes);

    //Add ham sausage
    abstract Builder bulidE(String mes);

    //Add eggs
    abstract Builder bulidF(String mes);

    //Add bean sprouts
    abstract Builder bulidG(String mes);

    //Add cabbage
    abstract Builder bulidH(String mes);

    //Get fried flour
    abstract Product build();
}

//Specific Builder - fried chicken
class ConcreteBuilder extends Builder {
    //Stir-Fried Rice Noodles
    private Product product;

    public ConcreteBuilder() {
        product = new Product();
    }

    @Override
    Product build() {
        return product;
    }

    @Override
    Builder bulidA(String mes) {
        product.setBuildA(mes);
        return this;
    }

    @Override
    Builder bulidB(String mes) {
        product.setBuildB(mes);
        return this;
    }

    @Override
    Builder bulidC(String mes) {
        product.setBuildC(mes);
        return this;
    }

    @Override
    Builder bulidD(String mes) {
        product.setBuildD(mes);
        return this;
    }

    @Override
    Builder bulidE(String mes) {
        product.setBuildE(mes);
        return this;
    }

    @Override
    Builder bulidF(String mes) {
        product.setBuildF(mes);
        return this;
    }

    @Override
    Builder bulidG(String mes) {
        product.setBuildG(mes);
        return this;
    }

    @Override
    Builder bulidH(String mes) {
        product.setBuildH(mes);
        return this;
    }
}

public class BuilderMode {
    public static void main(String[] args) {
        //Specific Builder - fried flour brother
        Builder builder1 = new ConcreteBuilder();
        //Fried noodles - high configuration version
        Product productHigh = builder1.bulidA("River flour")
                .bulidD("beef")
                .bulidE("Ham sausage")
                .bulidF("egg")
                .bulidG("Bean sprouts")
                .bulidH("Chinese cabbage").build();
        System.out.println("Fried rice noodles: ");
        System.out.println(productHigh);
        
        //Specific Builder - fried flour brother
        Builder builder2 = new ConcreteBuilder();
        //Fried noodles - low configuration version
        Product productLow = builder2.bulidA("River flour")
                .bulidF("egg")
                .bulidG("Bean sprouts").build();
        System.out.println("Fried rice noodles with low matching version: ");
        System.out.println(productLow);
    }
}

Topics: Java Design Pattern