Builder pattern

Housing Project Requirements

  1. A house needs to be built through these processes: piling, walls, roofing
  2. There are many kinds of houses, such as ordinary houses, high buildings, villas; These houses are built the same way, but each with a different approach

Traditional way

/***
 * @author shaofan
 * @Description Traditional Solutions to Build a House
 */
public class BuildHouse {
    public static void main(String[] args) {
        AbstractHouse house = new CommonHouse();
        house.build();
    }
}

/***
 * House abstraction
 */
abstract class AbstractHouse{
    /**
     * Building process, template mode
     */
    void build(){
        buildBasic();
        buildWall();
        roofed();
    }
    abstract void buildBasic();
    abstract void buildWall();
    abstract void roofed();
}

/***
 * Ordinary house
 */
class CommonHouse extends AbstractHouse{

    @Override
    void buildBasic() {
        System.out.println("Foundation for ordinary houses");
    }

    @Override
    void buildWall() {
        System.out.println("Ordinary house walls");
    }

    @Override
    void roofed() {
        System.out.println("Ordinary house roof");
    }
}

problem analysis

  1. The design of the program structure is too simple, there is no design cache layer object, the program expansion and maintenance is not good, this design scheme encapsulates the house itself and the process of building the house, and improves the coupling
  2. Builder mode is required to decouple product and product building processes

Builder pattern

Basic Introduction

  1. Builder mode, also known as generator mode, is an object construction mode. He can abstract the building process of complex objects, which can be implemented in different ways to construct objects with different representations
  2. Builder mode is a step-by-step process for creating complex objects that allows users to build them by specifying only the type and content of complex objects, without paying attention to internal build details

Four roles in the builder model

  1. Product: A specific product object
  2. Builder: Creates an interface/abstract class specified by each part of a Product Object
  3. ConcreteBuilder: Specific builder, implements interfaces, builds and assembles components
  4. Director: Build an object using the Builder interface, which is mainly used to create a complex object. It has two main functions: one is to isolate the production process of customers and objects; Second, responsible for controlling the production process of product objects

Builder Mode Solves Building Problem

/***
 * @author shaofan
 * @Description Builder Mode Solves Building Problem
 */
public class BuildHouse {
    public static void main(String[] args) {
        CommonBuilder commonBuilder = new CommonBuilder();
        Director director = new Director(commonBuilder);
        House house = director.build();
    }
}

class House{

}

/***
 * Builder abstraction
 */
abstract class Builder{
    protected House house = new House();
    abstract void buildBasic();
    abstract void buildWall();
    abstract void roofed();
    public House buildHouse(){
        return house;
    }
}

/***
 * Ordinary Builder
 */
class CommonBuilder extends Builder{

    @Override
    void buildBasic() {
        System.out.println("common build basic");
    }

    @Override
    void buildWall() {
        System.out.println("common build wall");
    }

    @Override
    void roofed() {
        System.out.println("common roofed");
    }
}

/***
 * Tall building builder
 */
class HighBuilder extends Builder{

    @Override
    void buildBasic() {
        System.out.println("high build basic");
    }

    @Override
    void buildWall() {
        System.out.println("high build wall");
    }

    @Override
    void roofed() {
        System.out.println("high roofed");
    }
}

/***
 * Commander, responsible for grouping the various construction processes
 */
class Director{
    private Builder houseBuilder;
    public Director(Builder houseBuilder){
        this.houseBuilder = houseBuilder;
    }
    public void setHouseBuilder(Builder houseBuilder){
        this.houseBuilder = houseBuilder;
    }
    public House build(){
        houseBuilder.buildBasic();
        houseBuilder.buildWall();
        houseBuilder.roofed();
        return houseBuilder.buildHouse();
    }
}

Source Code Analysis

In jdk, java.lang.StringBuilder uses the Builder mode

  • The Appendable interface defines several append methods, that is, Appendables are abstract builders and define abstract methods
  • AbstractStringBuilder implements the Appendable interface method, which is already the builder but cannot be instantiated
  • StringBuilder acts as the commander and replaces AbstractStringBuilder as the specific builder. The implementation of the construction method is accomplished by AbstractStringBuilder, where Override simply calls the method of the parent class

summary

  1. Clients do not need to know the details of the internal composition of the product. They decouple the product itself and the creation process of the product. The same creation process can create different product objects.
  2. Each individual builder is relatively independent and independent of other specific builders, so it's easy to slow down specific builders or add new ones, and users can get different product objects from different specific builders.
  3. The process of creating a product can be controlled more finely, and the steps of creating a complex product can be broken down into different methods, which makes the creation process clearer and easier to use the program to control the creation process
  4. Adding a new concrete builder without modifying the code of the original class library. The commander class is programmed for the abstract builder class. The system is easy to expand and complies with the open and close principle.

Tags: Java Design Pattern programming language

Posted by phlow on Thu, 22 Sep 2022 02:15:45 +0930