introduce
Builder pattern (Builder Pattern), also known as generator 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 performances (attributes).
Builder mode is to create a complex object step by step, which allows users to build them only by specifying the type and content of complex objects, and users do not need to know the specific internal construction details.
The Four Roles of Builder Mode
- Product (product role): A specific product object.
- Builder (abstract builder): Create an interface/abstract class specified by each component of a Product object.
- ConcreteBuilder (concrete builder): implements the interface, builds and assembles various components.
- Director: Build 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 the customer and the object, and the other is to be responsible for controlling the production process of the product object.
Building project needs
- Need to build a house: the process is piling, wall building, roofing
- There are various types of houses, such as ordinary houses, high-rise buildings, and villas. Although the process of all kinds of houses is the same, the requirements should not be the same
traditional way
- The advantage is that it is easy to understand and easy to operate.
- The designed program structure is too simple, and the cache layer object is not designed, so the expansion and maintenance of the program are not good. In other words, this design scheme, the product
The product (ie: house) and the process of creating the product (ie: the process of building a house) are packaged together, and the coupling is enhanced. - Solution: Decouple the product and the product building process => builder mode
public abstract class AbstractHouse { //Lay the foundation public abstract void buildBasic(); //build a wall public abstract void buildWalls(); //Capped public abstract void roofed(); public void build() { buildBasic(); buildWalls(); roofed(); } }
public class CommonHouse extends AbstractHouse { @Override public void buildBasic() { System.out.println(" common house foundation "); } @Override public void buildWalls() { System.out.println(" common house wall "); } @Override public void roofed() { System.out.println(" Common House Capped "); } }
public class Client { public static void main(String[] args) { CommonHouse commonHouse = new CommonHouse(); commonHouse.build(); } }
builder mode
- Create a House class corresponding to the Product product role
//Product Correspondence -> product public class House { private String basic; private String walls; private String roofed; public String getBasic() { return basic; } public void setBasic(String basic) { this.basic = basic; } public String getWalls() { return walls; } public void setWalls(String walls) { this.walls = walls; } public String getRoofed() { return roofed; } public void setRoofed(String roofed) { this.roofed = roofed; } }
- Create a HouseBuilder class corresponding to the Builder (abstract builder)
//abstract builder public abstract class HouseBuilder { public House house = new House(); //Write down the construction process and lay the foundation public abstract void buildBasic(); //build a wall public abstract void buildWalls(); //Capped public abstract void roofed(); //Build the house well, return the house public House build() { return house; } }
- Create CommonHouse, corresponding to the specific builder
public class CommonHouse extends HouseBuilder { @Override public void buildBasic() { System.out.println(" Common house foundation 5 meters "); } @Override public void buildWalls() { System.out.println(" Ordinary house wall 10 cm "); } @Override public void roofed() { System.out.println(" common house roof "); } }
- Create HighBuilding
public class HighBuilding extends HouseBuilder { @Override public void buildBasic() { System.out.println(" Lay the foundation of tall buildings 100 meters "); } @Override public void buildWalls() { System.out.println(" Walls of tall buildings 20 cm "); } @Override public void roofed() { System.out.println(" transparent roof of tall building "); } }
- Create HouseDirector, corresponding to Director
public class HouseDirector { HouseBuilder houseBuilder = null; //The constructor is passed to houseBuilder public HouseDirector(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //pass in houseBuilder via setter public void setHouseBuilder(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } //How to deal with the process of building a house and hand it over to the commander public House constructHouse() { houseBuilder.buildBasic(); houseBuilder.buildWalls(); houseBuilder.roofed(); return houseBuilder.buildHouse(); } }
- Create Client
public class Client { public static void main(String[] args) { //build an ordinary house CommonHouse commonHouse = new CommonHouse(); //Preparing to create a house conductor HouseDirector houseDirector = new HouseDirector(commonHouse); //Finish building the house and return to the product (ordinary house) House house = houseDirector.constructHouse(); //System.out.println("Output process"); System.out.println("--------------------------"); //build tall buildings HighBuilding highBuilding = new HighBuilding(); //reset builder houseDirector.setHouseBuilder(highBuilding); //Finish building the house and return to the product (high-rise) houseDirector.constructHouse(); } }
Application of builder mode in JDK
- Builder pattern in java.lang.StringBuilder
- Code description + Debug source code
- Analysis of the role of builder mode in the source code
The Appendable interface defines multiple append methods (abstract methods), that is, Appendable is an abstract builder and defines abstract methods
AbstractStringBuilder implements the Appendable interface method, here AbstractStringBuilder is already a builder, but it cannot
instantiate
StringBuilder acts as the commander and the concrete builder at the same time. The realization of the construction method is completed by AbstractStringBuilder
into, and StringBuilder inherits AbstractStringBuilder
Abstract Factory Pattern VS Builder Pattern
- The abstract factory pattern realizes the creation of product families. A product family is such a series of products: a product combination with different classification dimensions. 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 pattern requires building products according to a specified blueprint, and its main purpose is to produce a new product by assembling spare parts.