[Design Mode] Builder Mode

builder mode

Introduce a case of building a house

  1. A house needs to be built: the process is piling, building walls, capping
  2. There are various kinds of houses, such as ordinary houses, high-rise buildings, villas, although the process of various houses is the same, but the requirements are not the same.

The traditional way to solve the housing needs

Code

base class for house

public abstract class AbstractHouse {
   
   //lay the foundation
   public abstract void buildBasic();
   //build walls
   public abstract void buildWalls();
   //capped
   public abstract void roofed();
   
   public void build() {
      buildBasic();
      buildWalls();
      roofed();
   }
   
}

specific housing subclasses

public class CommonHouse extends AbstractHouse {

   @Override
   public void buildBasic() {
      // TODO Auto-generated method stub
      System.out.println(" ordinary house foundation ");
   }

   @Override
   public void buildWalls() {
      // TODO Auto-generated method stub
      System.out.println(" ordinary house wall ");
   }

   @Override
   public void roofed() {
      // TODO Auto-generated method stub
      System.out.println(" ordinary house capped ");
   }

}

client

public class Client {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      CommonHouse commonHouse = new CommonHouse();
      commonHouse.build();
   }

}

Problem Analysis in the Traditional Way

  1. The advantage is that it is easier to understand and easy to operate.
  2. The designed program structure is too simple, the cache layer object is not designed, and the program expansion and maintenance are not good That is to say, this design scheme packages the product (i.e., the house) and the process of creating the product (i.e., the building process), and the coupling is enhanced
  3. Solution: Decouple product and product building process => Builder pattern.

Basic introduction to builder mode

  1. The Builder Pattern, also known as the Builder Pattern, is an object construction pattern. It can abstract the construction process of complex objects (abstract category), so that different implementation methods of this abstract process can construct objects with different performance (attributes).
  2. The builder pattern is to create a complex object step by step, it allows users to build complex objects only by specifying their type and content, the user does not need to know the specific construction details inside.

The Four Characters of Builder Mode

  1. Product: A specific product object.
  2. Builder (abstract builder): Creates an interface/abstract class specified by each component of a Product object.
  3. ConcreteBuilder (concrete builder): implements the interface, builds and assembles the various components.
  4. Director: Constructs an object using the Builder interface. It is mainly used to create a complex object. It has two main functions, one is to isolate the production process of customers and objects, and the other is to control the production process of product objects.

Applications

Top-level builder base class

public abstract class HouseBuilder {

   protected House house = new House();
   
   //Write the construction process, abstract method
   public abstract void buildBasic();
   public abstract void buildWalls();
   public abstract void roofed();
   
   //Build the house well, return the product (house)
   public House buildHouse() {
      return house;
   }
   
}

Concrete builder implementation class

public class CommonHouse extends HouseBuilder {

   @Override
   public void buildBasic() {
      // TODO Auto-generated method stub
      System.out.println(" Ordinary house lays foundation 5 meters ");
   }

   @Override
   public void buildWalls() {
      // TODO Auto-generated method stub
      System.out.println(" ordinary house wall 10 cm ");
   }

   @Override
   public void roofed() {
      // TODO Auto-generated method stub
      System.out.println(" ordinary house roof ");
   }

}
public class HighBuilding extends HouseBuilder {

   @Override
   public void buildBasic() {
      // TODO Auto-generated method stub
      System.out.println(" The foundation of high-rise building is 100 meters ");
   }

   @Override
   public void buildWalls() {
      // TODO Auto-generated method stub
      System.out.println(" Building walls 20 cm ");
   }

   @Override
   public void roofed() {
      // TODO Auto-generated method stub
      System.out.println(" Transparent roof of tall building ");
   }

}

Commander, according to the builder to formulate the process

//Commander, go here to specify the production process and return to the product
public class HouseDirector {
   
   HouseBuilder houseBuilder = null;

   //The constructor passes in houseBuilder
   public HouseDirector(HouseBuilder houseBuilder) {
      this.houseBuilder = houseBuilder;
   }

   //Pass in houseBuilder via setter
   public void setHouseBuilder(HouseBuilder houseBuilder) {
      this.houseBuilder = houseBuilder;
   }
   
   //How to handle the process of building a house, leave it to the conductor
   public House constructHouse() {
      houseBuilder.buildBasic();
      houseBuilder.buildWalls();
      houseBuilder.roofed();
      return houseBuilder.buildHouse();
   }
   
   
}

Application and source code analysis of builder pattern in JDK

Builder pattern in java.lang.StringBuilder

stringBuilder implements the AbstractStringBuilder abstract class

But in AbstractStringBuilder has actually implemented the append method of Appendable

Analysis of the role of the builder mode in the source code

  1. The Appendable interface defines multiple append methods (abstract methods), that is, Appendable is an abstract builder and defines abstract methods
  2. AbstractStringBuilder implements the Appendable interface method, where AbstractStringBuilder is already a builder, but cannot be instantiated
  3. StringBuilder acts as a conductor and a specific builder. The implementation of the construction method is completed by AbstractStringBuilder.
    into, and StringBuilder inherits AbstractStringBuilder

Builder Mode Notes and Details

  1. The client (using program) does not need to know the details of the internal composition of the product, decoupling the product itself from the product creation process, so that the same creation process can create different product objects

  2. Each specific builder is relatively independent and has nothing to do with other specific builders, so it is easy to replace specific builder or add new specific builder, and users can get different product objects by using different specific builder

  3. More granular control over the product creation process. Breaking down the steps of creating complex products into different methods makes the creation process clearer and easier to use programs to control the creation process

  4. Adding new concrete builders does not need to modify the code of the original class library, the commander class is programmed for the abstract builders class, the system is easy to expand, and conforms to the "open-closed principle"

  5. Products created by the builder mode generally have more in common, and their components are similar. If the differences between the products are large, the builder mode is not suitable for use, so its scope of use is limited.

  6. If the internal changes of the product are complex, it may lead to the need to define many specific builder classes to implement such changes, resulting in a very large system, so in this case, consider whether to choose the builder mode.

  7. Abstract Factory Pattern VS Builder Pattern

    The abstract factory pattern realizes the creation of a product family. A product family is a series of products: product combinations with different classification dimensions. Using the abstract factory pattern does not need to care about the construction process, but only cares about which products are produced by which factories. The builder mode is to build a product according to a specified blueprint, and its main purpose is to generate a new product by assembling spare parts

Tags: Java Design Pattern

Posted by brash on Mon, 05 Sep 2022 02:33:21 +0930