1. Housing construction project requirements

  1. Houses need to be built: the process is piling, laying walls, and roofing.
  2. There are all kinds of houses, such as ordinary rooms, tall buildings, villas, all kinds of houses, although the process is the same, but the requirements are not the same.
  3. Write the program to fulfill the requirements.

2. Traditional ways to solve the demand for building houses

  1. Analysis of Ideas (Diagram)

3. Problem analysis of traditional methods

  1. Advantages are relatively easy to understand, simple and easy to operate.
  2. Design of the program structure, too simple, did not design the cache layer object, program expansion and maintenance is not good. In other words, this design solution encapsulates the product (i.e., the house) and the process of creating the product (i.e., the house building process) together, resulting in increased coupling.
  3. Solution: Decoupling the product from the product building process => Builder pattern.

4. Basic introduction to builder mode

  1. Builder Pattern, also known as Builder Pattern, is an object construction Pattern. It abstracts the construction process of complex objects (abstract categories) so that different implementations of this abstraction process can construct objects with different representations (properties).
  2. The Builder pattern is to create a complex object step by step, allowing the user to build complex objects simply by specifying their type and content, without the user having to know the internal details of the build.

The four roles in builder mode

  1. Product (Product Role) : A concrete Product object.
  2. Builder: Creates the interface/abstract class specified by the parts of a Product object.
  3. ConcreteBuilder: Implement the interface, build and assemble the parts.
  4. Director: Builds an object that uses the Builder interface. It is mainly used to create a complex object. It mainly has two functions, one is: the isolation of the customer and the object of the production process, the other is: responsible for the control of the product object of the production process.

Builder pattern schematic class diagram

7. Examples of application of builder model to solve building needs

  1. Houses need to be built: the process is piling, laying walls, and roofing. Both ordinary houses and villas need to go through these processes. Here we use the Builder Pattern to complete this process
  2. Idea analysis diagram (class diagram)
  3. Code implementation:

Products – > the Product:

public class House { private String basic; private String wall; private String roofed; . get/set }

Abstract builder:

// The abstract builder. Define the steps of the construction, the specific implementation by the subclass to achieve. public abstract class HouseBuilder { protected House house = new House(); Public abstract void buildBasic(); public abstract void buildBasic(); public abstract void buildWalls(); public abstract void roofed(); Public House buildHouse() {return House; }}

The commander completes the process of building the house:

public class HouseDirector { HouseBuilder houseBuilder; // public HouseDirector(HouseBuilder, HouseBuilder, HouseBuilder) {this.houseBuilder = HouseBuilder; } public void setHouseBuilder(houseBuilder, houseBuilder, houseBuilder) {this.houseBuilder = HouseBuilder houseBuilder; } public House Constructhouse () {houseBuilder. BuildBasic ();} public House Constructhouse () {houseBuilder. houseBuilder.buildWalls(); houseBuilder.roofed(); return houseBuilder.buildHouse(); }}

A concrete implementation of the abstract builder 1:

Public class CommonHouse extends HouseBuilder {@Override public void buildBasic() {System.out.println() "); } public void buildWalls() {System.out.println();} public void buildWalls() {System.out.println(); } public void roofed() {System.out.println(" roofed ");} public void roofed() {System.out.println(" roofed "); }}

A concrete implementation of the abstract builder 2:

Public void buildBasic() {public void buildBasic() {public void buildBasic(); public void buildBasic() {public void buildBasic(); public void buildBasic() "); } public void buildWalls() {System.out.println();} public void buildWalls() {System.out.println(); } public void roofed() {System.out.println();} public void roofed(); }}

Use: Depends on the concrete builder and commander:

Public class Client {public static void main(String[] args) {// commonHouse commonHouse = new commonHouse (); // HouseDirector = new HouseDirector(commonHouse); / / finish building a House, return the product (normal) House House House. = houseDirector constructHouse (); System.out.println("--------------------------"); // HighBuilding HighBuilding = new HighBuilding(); / / reset builder houseDirector setHouseBuilder (highBuilding); / / finish building a house, return the product (tall) houseDirector. ConstructHouse (); }}

Builder pattern in JDK application and source code analysis

  1. The builder pattern in java.lang.StringBuilder
  2. Code description +Debug source code
  3. Analysis of the role of builder mode in source code

The Appendable interface defines multiple Append methods (abstract methods), that is, Appendable defines abstract methods for the abstract builder.

AbstractStringBuilder implements the Appendable interface method, where the AbstractStringBuilder is already the builder, but cannot be instantiated.

StringBuilder acts as both the conductor and the builder. The implementation of the construction method is performed by AbstractStringBuilder, and StringBuilder inherits AbstractStringBuilder.

Considerations and details for builder mode

  1. The client (using the program) does not have to know the details of the internal composition of the product, decoupling the product itself from the creation process of the product, allowing the same creation process to create different product objects.
  2. Each concrete builder is relatively independent and independent of other concrete builders, so it is convenient to replace or add new concrete builders. Users can get different product objects by using different concrete builders.
  3. The product creation process can be more finely controlled. Decomposing the creation steps of a complex product into different approaches makes the creation process cleaner and easier to control programmatically.
  4. Adding a new concrete builder does not need to modify the code of the original class library. The commander class is programmed for the abstract builder class, which makes the system easy to expand and conforms to the “open closed principle”.
  5. The products created by the builder model generally have more in common, and their components are similar. If the products are very different, the builder model is not suitable for use, so its use scope is limited to a certain extent.
  6. If the internal variation of the product is complex, it may lead to the need to define many specific builder classes to implement the change, resulting in a large system, so consider choosing builder mode in such cases.
  7. Abstract Factory vs. Builder Pattern Abstract Factory pattern enables the creation of a product family. A product family is a set of products with different classification dimensions. The abstract factory pattern does not need to care about the construction process, only care about what products are produced in what factory. The Builder mode requires the product to be built according to a specified blueprint, and its main purpose is to assemble parts to produce a new product.