catalogue
Traditional way to solve the housing demand
Problem analysis of traditional methods
Basic introduction to builder mode
The four roles of the builder model
Schematic class diagram of builder mode
Application example of builder mode to solve building demand
Application and source code analysis of builder pattern in JDK
Notes and details of builder mode
Builder pattern
Housing project demand
1) Need to build a house: this process is piling, building walls and capping
2) There are all kinds of houses, such as ordinary houses, high-rise buildings, villas. Although the process of all kinds of houses is the same, the requirements are not the same
3) Please write a program to complete the requirements
Traditional way to solve the housing demand
1) Train of thought analysis (illustration)
2) Look at the demonstration of the code
public abstract class AbstractHouse { //Laying foundation public abstract void buildBasic(); //Build a wall public abstract void buildWalls(); //Capping public abstract void roofed(); public void build() { buildBasic(); buildWalls(); roofed(); } } public class CommonHouse extends AbstractHouse { @Override public void buildBasic() { // TODOAuto-generated method stub System.out.println(" Laying foundation for ordinary house "); } @Override public void buildWalls() { // TODOAuto-generated method stub System.out.println(" Ordinary house walling "); } @Override public void roofed() { // TODOAuto-generated method stub System.out.println(" Ordinary house capping "); } } package com.atguigu.builder; public class Client { public static void main(String[] args) { // TODOAuto-generated method stub CommonHouse commonHouse = new CommonHouse(); commonHouse.build(); } }
Problem analysis of traditional methods
1) The advantages are easy to understand, simple and easy to operate.
2) The designed program structure is too simple, there is no cache layer object, and the program expansion and maintenance are not good In other words, this design scheme encapsulates the product (i.e. house) and the process of creating the product (i.e. house building process), and the coupling is enhanced.
3) Solution: decouple product and product construction process = > builder mode
Basic introduction to builder mode
Basic introduction
1) Builder Pattern, also known as Builder Pattern, is an object construction pattern. It can abstract the construction process of complex objects (abstract categories), so that different implementation methods of this abstract process can construct objects with different representations (attributes).
2) Builder mode is to create a complex object step by step. It allows users to build complex objects only by specifying their types and contents. Users do not need to know the specific internal construction details.
The four roles of the builder model
1) Product role: a specific product object.
2) Builder (Abstract builder): create an interface / abstract class specified by each part of a Product object.
3) ConcreteBuilder: implement interfaces, build and assemble various components.
4) Director: build an object that uses 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; the other is to control the production process of product objects.
Schematic class diagram of builder mode
Application example of builder mode to solve building demand
1) Need to build a house: this process is piling, building walls and capping. Both ordinary houses and villas need to go through these processes. Next, we use builder pattern to complete them
2) Train of thought analysis diagram (class diagram)
3) Code implementation
improve.zip
public class Client { public static void main(String[] args) { //Build an ordinary house CommonHouse commonHouse = new CommonHouse(); //The commander ready to create the house HouseDirector houseDirector = new HouseDirector(commonHouse); //Finish building a house and return to the product (ordinary house) House house = houseDirector.constructHouse(); //System.out.println("output process"); System.out.println("--------------------------"); //Build a tall building HighBuilding highBuilding = new HighBuilding(); //Reset builder houseDirector.setHouseBuilder(highBuilding); //Finish building the house and return to the product (high-rise building) houseDirector.constructHouse(); } } public class CommonHouse extends HouseBuilder { @Override public void buildBasic() { // TODOAuto-generated method stub System.out.println(" The foundation of an ordinary house is 5m "); } @Override public void buildWalls() { // TODOAuto-generated method stub System.out.println(" Ordinary house walling 10 cm "); } @Override public void roofed() { // TODOAuto-generated method stub System.out.println(" Ordinary house roof "); } } public class HighBuilding extends HouseBuilder { @Override public void buildBasic() { // TODOAuto-generated method stub System.out.println(" The foundation of the tall building is 100m "); } @Override public void buildWalls() { // TODOAuto-generated method stub System.out.println(" Wall construction of tall buildings 20 cm "); } @Override public void roofed() { // TODOAuto-generated method stub System.out.println(" The transparent roof of a tall building "); } } //Product - > Product public class House { private String baise; private String wall; private String roofed; public String getBaise() { return baise; } public void setBaise(String baise) { this.baise = baise; } public String getWall() { return wall; } public void setWall(String wall) { this.wall = wall; } public String getRoofed() { return roofed; } public void setRoofed(String roofed) { this.roofed = roofed; } } // Abstract builder public abstract class HouseBuilder { protected House house = new House(); //Write the construction process and abstract method public abstract void buildBasic(); public abstract void buildWalls(); public abstract void roofed(); //Build a house and return the product (house) public House buildHouse() { return house; } } //Commander, go here to specify the production process and return the product public class HouseDirector { HouseBuilder houseBuilder = null; //Constructor passed into houseBuilder public HouseDirector(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //Pass in houseBuilder through setter public void setHouseBuilder(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //How to deal with the process of building a house and leave it to the commander public House constructHouse() { houseBuilder.buildBasic(); houseBuilder.buildWalls(); houseBuilder.roofed(); return houseBuilder.buildHouse(); } }
Application and source code analysis of builder pattern in JDK
1) java. Builder pattern in lang.stringbuilder
2) Code description + Debug source code
3) Role analysis of builder pattern in source code
The Appendable interface defines multiple append methods (abstract methods), that is, Appendable is the abstract builder and defines abstract methods
The AbstractStringBuilder implements the Appendable interface method. The AbstractStringBuilder here is already the builder, but it can't
instantiation
StringBuilder acts as both a commander and a specific builder. The implementation of the construction method is completed by AbstractStringBuilder
And StringBuilder inherits AbstractStringBuilder
Notes and details of builder mode
1) The client (user program) does not need to know the details of the internal composition of the product, decouples 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. Therefore, it is convenient to replace or add new specific builders. Users can get different product objects by using different specific builders
3) You can more finely control the product creation process. The creation steps of complex products are decomposed into different methods, which makes the creation process clearer and easier to use programs to control the creation process
4) Adding a new specific builder does not need to modify the code of the original class library. The commander class is programmed for the abstract builder class, which is convenient for system expansion and conforms to the "opening and closing principle"
5) The products created by the builder mode generally have more in common and their components are similar. If there are great differences between products, 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 huge system. Therefore, in this case, it is necessary to consider whether to select the builder mode
7) Abstract factory pattern VS builder pattern
Abstract factory mode realizes the creation of product families. A product family is a series of products: product combinations with different classification dimensions. Adopting abstract factory mode does not need to care about the construction process, but only about what products are produced by what factory. The builder model requires the product to be built according to the specified blueprint. Its main purpose is to produce a new product by assembling spare parts