Design pattern -- Builder pattern -- Builder

reference resources: https://blog.csdn.net/weixin_48052161/article/details/119083633

GOF defines the construction pattern as separating the construction of a complex object from its representation, so that the same construction process can create different representations

This sentence is very abstract and difficult to understand. In fact, its meaning can be understood as decoupling the process of constructing complex objects from the components of objects

Difference between builder mode and factory mode:
The advantages of builder mode are similar to factory mode. They are used to create an object, but they are still very different. The main differences are as follows:

The builder pattern pays more attention to the calling sequence of methods, and the factory pattern pays more attention to the creation of complete objects
The builder model can create different products according to different product parts and sequences, while the products created by the factory model are the same
The user of maker mode needs to know which parts of the product are composed, while the user of factory mode does not need to know, just create it directly

The creation mode focuses on gradually assembling components into a finished product and providing finished products to the outside,
The abstract factory pattern focuses on getting multiple related product objects in the product family

Applicable scenarios:
The builder mode is applicable to a complex product creation process with many parts, and the components of the product often change, or need to support dynamic changes, but the types of parts are generally stable:

With the same method, different execution sequences need to produce different execution results
Product classes are very complex. Different products need to be obtained after calling different parts or assembling products in different order
When initializing an object is very complex, and many parameters have default values
advantage:
Good encapsulation and separation of creation and use
It has good expansibility and independence between construction classes, realizing decoupling to a certain extent:
Disadvantages:
Generate redundant Builder objects
When the product changes internally, the builder needs to modify it, and the cost is large

Or code demonstration!

Build the Terrain model object Terrain,
The terrain is composed of Wall, Fort and Mine
Each construction object has four directional indicator attributes: X, y, W and H

Facade class: Terrain depends on Wall, Mine and Fort

package com.example.dtest.design23.builder;

public class Terrain {

    Wall w; //wall
    Fort f; //fortress
    Mine m; //mine

    @Override
    public String toString() {
        return "Terrain{" +
                "w=" + w +
                ", f=" + f +
                ", m=" + m +
                '}';
    }

}

Wall class:

package com.example.dtest.design23.builder;

//wall
public class Wall {

    int x,y,w,h;
    public Wall(int x,int y,int w,int h){

        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;

    }

    @Override
    public String toString() {
        return "Wall{" +
                "x=" + x +
                ", y=" + y +
                ", w=" + w +
                ", h=" + h +
                '}';
    }
}

Fort class:

package com.example.dtest.design23.builder;

//fortress
public class Fort {

    int x, y, w, h;

    public Fort(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }

    @Override
    public String toString() {
        return "Fort{" +
                "x=" + x +
                ", y=" + y +
                ", w=" + w +
                ", h=" + h +
                '}';
    }


}

Mine class:

package com.example.dtest.design23.builder;

//mine
public class Mine {

    int x, y, w, h;

    public Mine(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }

    @Override
    public String toString() {
        return "Mine{" +
                "x=" + x +
                ", y=" + y +
                ", w=" + w +
                ", h=" + h +
                '}';
    }

}

Building abstract classes:

package com.example.dtest.design23.builder;

public interface TerrainBuilder {

    TerrainBuilder buildWall();
    TerrainBuilder buildFort();
    TerrainBuilder buildMine();
    Terrain build();

}

Construction implementation class:

package com.example.dtest.design23.builder;

public class ComplexTerrainBuilder implements TerrainBuilder{

    Terrain terrain = new Terrain();

    @Override
    public TerrainBuilder buildWall() {
        terrain.w = new Wall(10, 10, 10, 10);
        return this;
    }

    @Override
    public TerrainBuilder buildFort() {
        terrain.f = new Fort(20, 20, 20, 20);
        return this;
    }

    @Override
    public TerrainBuilder buildMine() {
        terrain.m = new Mine(50, 50, 50, 50);
        return this;
    }

    @Override
    public Terrain build() {
        return terrain;
    }
}

Test class:

package com.example.dtest.design23.builder;

public class builderTest {

    public static void main(String[] args) {

        TerrainBuilder builder = new ComplexTerrainBuilder();
        Terrain terrain = builder.buildFort().buildMine().buildWall().build();

        System.out.println(terrain);

    }

}

Chain writing
Note: the last return value of each created product is this, so the chain writing method can be realized
Instead of calling line by line as before, build directly through chain writing, and finally call the build() method to return the complete product object.

How to use Builder Design Pattern in Netty:
As shown in the figure below, we remember that there is a startup auxiliary class ServerBootStrap when the server is started. We call the group method and channel method to set parameters. Chain programming is also used to set relevant parameters.

Posted by mikeq on Tue, 02 Nov 2021 05:44:12 -0700