Writing in the front

  • Take notes on learning design patterns
  • Improve the flexible use of design patterns

Learning to address

www.bilibili.com/video/BV1G4…

www.bilibili.com/video/BV1Np…

Refer to the article

C.biancheng.net/view/1317.h…

Program source codeGitee.com/zhuang-kang…

8. Builder mode

8.1 Definition and characteristics of the Builder model

Definition of Builder pattern: It refers to the separation of the construction of a complex object from its representation, so that the same construction process can create different representations, such a design pattern is called the Builder pattern. It takes a complex object and breaks it down into simple objects and builds it step by step. It separates change from immutability, that is, the components of a product are constant, but each part can be chosen flexibly.

The main advantages of this mode are as follows:

  1. Good encapsulation, separation of build and presentation.
  2. Good expansibility, each specific builder is independent of each other, which is conducive to the decoupling of the system.
  3. The client does not have to know the details of the internal composition of the product, and the builder can gradually refine the creation process without any impact on other modules, facilitating the control of detail risk.

Its disadvantages are as follows:

  1. The components of the product must be the same, which limits its range of use.
  2. If the internal change of the product is complex, if the internal change of the product occurs, the builder also needs to synchronize the modification, and the later maintenance cost is large.

8.2 Structure and implementation of builder pattern

8.2.1 Builder mode structure

  1. A Product role: It is a complex object with multiple components that are created by a specific builder.
  2. Abstract Builder: This is an interface that contains an abstract method for creating the various child parts of a product, usually including a method getResult() that returns a complex product.
  3. Concrete Builder: to realize the Builder interface and complete the Concrete creation method of each part of complex products.
  4. Director: It calls the component construction and assembly methods in the builder object to create a complex object. There is no product-specific information in the Director.

8.2.2 Code implementation

House Product Roles

package com.zhuang.builder;

/ * * *@Classname House
 * @DescriptionProduct implementation classes *@Date 2021/3/20 11:49
 * @Created by dell
 */

public class House {
    private String ground;
    private String wall;
    private String roofed;

    public House(a) {}public House(String ground, String wall, String roofed) {
        this.ground = ground;
        this.wall = wall;
        this.roofed = roofed;
    }

    public String getGround(a) {
        return ground;
    }

    public void setGround(String ground) {
        this.ground = ground;
    }

    public String getWall(a) {
        return wall;
    }

    public void setWall(String wall) {
        this.wall = wall;
    }

    public String getRoofed(a) {
        return roofed;
    }

    public void setRoofed(String roofed) {
        this.roofed = roofed;
    }

    @Override
    public String toString(a) {
        return "House{" +
                "ground='" + ground + '\' ' +
                ", wall='" + wall + '\' ' +
                ", roofed='" + roofed + '\' ' +
                '} '; }}Copy the code

HouseBuilder

package com.zhuang.builder;

/ * * *@Classname HouseBuilder
 * @DescriptionAbstract Builder *@Date 2021/3/20 11:49
 * @Created by dell
 */

public abstract class HouseBuilder {
    // Create a product object
    protected House house = new House();

    // Make the product flow
    public abstract void buildGround(a);

    public abstract void buildWall(a);

    public abstract void buildRoofed(a);

    // Return the product object
    public House getHouse(a) {
        returnhouse; }}Copy the code

HighHouse concrete builder

package com.zhuang.builder;

/ * * *@Classname HighHouse
 * @DescriptionSpecific builder *@Date 2021/3/20 11:51
 * @Created by dell
 */

public class HighHouse extends HouseBuilder {

    @Override
    public void buildGround(a) {
        house.setGround("100");
        System.out.println("Tall Building: Laying the Foundation");
    }

    @Override
    public void buildWall(a) {
        house.setWall("Fifty meters");
        System.out.println("Tall building: 50 meters of walls");
    }

    @Override
    public void buildRoofed(a) {
        house.setRoofed("Skylight");
        System.out.println("Villa: Skylight cover"); }}Copy the code

VillaHouse concrete builder

package com.zhuang.builder;

/ * * *@Classname VillaHouse
 * @DescriptionSpecific builder *@Date 2021/3/20 11:51
 * @Created by dell
 */

public class VillaHouse extends HouseBuilder {

    @Override
    public void buildGround(a) {
        house.setGround("200");
        System.out.println("Villa: Laying the foundation");
    }

    @Override
    public void buildWall(a) {
        house.setWall("10 meters");
        System.out.println("Villa: 10 meters of wall");
    }

    @Override
    public void buildRoofed(a) {
        house.setRoofed("Ceiling");
        System.out.println("Villa: Ceiling"); }}Copy the code

HouseDirector commanders

package com.zhuang.builder;

/ * * *@Classname HouseDirector
 * @DescriptionProject Director *@Date 2021/3/20 11:50
 * @Created by dell
 */

public class HouseDirector {
    private HouseBuilder houseBuilder;

    public HouseDirector(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }

    public House build(a) {
        houseBuilder.buildGround();
        houseBuilder.buildWall();
        houseBuilder.buildRoofed();
        returnhouseBuilder.getHouse(); }}Copy the code

Client

package com.zhuang.builder;

/ * * *@Classname Client
 * @DescriptionProduct trial client *@Date 2021/3/20 11:51
 * @Created by dell
 */

public class Client {
    public static void main(String[] args) {
        House house1 = new HouseDirector(new VillaHouse()).build();
        System.out.println(house1);
        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        House house2 = new HouseDirector(newHighHouse()).build(); System.out.println(house2); }}Copy the code

8.3 Application scenarios of the Builder Mode

  • Same method, different order of execution, produce different results.
  • Multiple parts, or parts, can be assembled into an object but produce different results.
  • The product class is very complex, or different calls in the product class have different effects.
  • Initializing an object is extremely complex with many parameters, many of which have default values.

9. Creator mode comparison

9.1 Factory method mode vs. Builder mode

The factory method pattern focuses on how the whole object is created; The Builder pattern focuses on the process of component construction, aiming to create a complex object by constructing it precisely step by step.

Let’s take a simple example to illustrate the difference between the two. If we want to make a superman, if we use the factory method mode, we will directly produce a superman with infinite strength, flying ability and wearing underwear. In Builder mode, you assemble the hands, head, feet, torso, etc., then put on the underwear, and a superman is born.

9.2 Abstract Factory vs. Builder mode

Abstract factory pattern realizes the creation of a product family. A product family is a series of products: a combination of products with different classification dimensions. The abstract factory pattern does not need to care about the construction process, but only what products are produced by what factories.

The builder model requires that a product be built according to a specified blueprint. Its main purpose is to create a new product by assembling parts.

If you think of the abstract factory pattern as an auto parts production factory that produces the products of a product family, then the Builder pattern is an auto assembly factory that returns a complete car by assembling the parts.

  • The Builder pattern focuses more on the order in which methods are called, while the factory pattern focuses on creating objects.
  • Objects are created with different forces. Builder mode creates complex objects, made up of complex parts. Factory mode creates the same objects
  • The focus is different. The factory mode just creates the object, whereas the Builder mode not only creates the object, but also knows what parts the object is made of.
  • The builder pattern varies depending on the order of the build process and the final object component composition.

Write in the last

  • If my article is useful to you, please give me a click 👍, thank you 😊!
  • If you have any questions, please point them out in the comments section! 💪