Housing project needs

  1. Houses need to be built: this process involves piling, building walls, and capping.
  2. There are all kinds of houses, such as ordinary rooms, villas, etc., although the construction process of all kinds of houses is the same, but the requirements are not the same.

Common solution

public abstract class AbstractHouse { public abstract void buildBasic(); public abstract void buildWall(); public abstract void roofed(); public void build() { buildBasic(); buildWall(); roofed(); }}Copy the code
Public class CommonHouse extends AbstractHouse {@override public void buildBasic() {system.out.println (" base "); } @override public void buildWall() {system.out.println (" buildWall "); } @override public void roofed() {system.out.println (); }}Copy the code
public class HouseBuildTest { public static void main(String[] args) { CommonHouse commonHouse = new CommonHouse(); commonHouse.build(); VillaHouse villaHouse = new VillaHouse(); villaHouse.build(); }}Copy the code

General solution analysis

  • The advantage is that it is simple and easy to understand, but the design of the program structure, too simple, no design cache layer objects, program extension and maintenance is not good, that is, this design scheme, the product (i.e., the house) and the process of creating the product (i.e., building the house) together, the coupling is enhanced
  • Solution: Decouple the product from the product building process => Builder pattern.

Introduction to the Builder pattern

  • Builder Pattern, also known as generator Pattern, is an object building Pattern. It abstracts the building process of complex objects and makes different implementations of this abstract process construct objects with different representations (properties).
  • The Builder pattern creates a complex object step by step. It allows users to build complex objects simply by specifying their type and content, without having to know the inner details of the build.

Four roles in builder mode

  1. Product(Product role) : a specific Product object
  2. Builder: Creates an interface/abstract class specified by each part of a Product object.
  3. ConcreteBuilder: Implements interfaces, builds and assembler components.
  4. Director: Builds an object using the Builder interface. It is mainly used to create a complex object. It has two main purposes: one is to isolate the customer and the production process of the object, the other is responsible for controlling the production process of the product object.

Builder pattern schematic class diagram

Use the builder model to offer preferential building demand

@data public class House {/** * basicLength */ private float basicLength; /** * private float wallThickness; /** * roofType */ private String roofType; @Override public String toString() { return "House{" + "basicLength=" + basicLength + ", wallThickness=" + wallThickness + ", roofType='" + roofType + ''' + '}'; }}Copy the code
public abstract class HouseBuilder { protected House house = new House(); public abstract void buildBasic(); public abstract void buildWalls(); public abstract void roofed(); public House getResult() { return house; }}Copy the code
public class CommonHouseBuilder extends HouseBuilder { @Override public void buildBasic() { house.setBasicLength(5); } @Override public void buildWalls() { house.setWallThickness(6); } @override public void roofed() {house.setroofType (); }}Copy the code
public class HouseDirector { private HouseBuilder houseBuilder; public HouseDirector(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } public House build() { houseBuilder.buildBasic(); houseBuilder.buildWalls(); houseBuilder.roofed(); return houseBuilder.getResult(); }}Copy the code
public class HouseBuilderClient { public static void main(String[] args) { House commonHouse = new HouseDirector(new CommonHouseBuilder()).build(); House villaHouse = new HouseDirector(new VillaHouseBuilder()).build(); System.out.println(" commonHouse: "+ commonHouse); System.out.println(" villaHouse: "+ villaHouse); }}Copy the code

Application of builder pattern in JDK source code

JDK java.lang.StringBuilder uses builder mode, code descriptionThe Appendable class defines multiple Append ()(abstract methods), meaning that Appendable is the abstract builder. AbstractStringBuilder implements the Appendable interface method, where AbstractStringBuilder is already the builder but cannot be instantiated. The StringBuilder acts as both the conductor and the concrete builder. The implementation of the construction method is completed by AbstractStringBuilder, and StringBuilder inherits From AbstractStringBuilder.

Builder pattern considerations and details

  1. The client does not have to know the details of the internal composition of the product, and the product itself is decoupled from the creation process of the product, so that the same creation process can create different product objects.
  2. Each specific builder is relatively independent, and has nothing to do with the specific builder of gas, so it is easy to replace specific builders or add specific builders, and users can use different specific builders to get different product objects.
  3. The creation of a product can be more carefully controlled by breaking down the steps responsible for the creation of the product into different methods, making the creation process more clear and easier to use procedures to control the creation process.
  4. Adding new concrete creators do not need to modify the original class code, the commander for abstract builder class programming, system expansion is convenient, in line with the OCP principle.
  5. The builder pattern creates products that generally have more in common and have similar components. If there are large differences between products, the builder pattern is not suitable for use, so its scope of use is limited.
  6. If the internal changes in the product are complex, the system may become large because many specific builder classes need to be defined to implement the changes, so consider choosing the builder pattern in this case.
  7. Abstract Factory pattern vs. Builder pattern: The abstract factory pattern implements the creation of a family of products. A family of products is a collection of products with different classification dimensions. The abstract factory pattern does not care about the construction process, but only what factory the products are made from. The builder model, on the other hand, requires the product to be built according to a specified blueprint. Its main purpose is to produce a new product by assembling components.