1, What is the builder model
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).
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.
Four roles of builder model
Product role: a specific product object.
Builder (Abstract builder): create an interface / abstract class specified by each part of a Product object.
ConcreteBuilder: implement interfaces, build and assemble various components.
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.
UML diagram
2, Code
Building demand
1) This is the process of building and piling the house
2) There are all kinds of houses, such as ordinary houses, high-rise buildings and villas. Although the process of all kinds of houses is the same, the requirements are different.
Traditional way
UML diagram
Analysis of traditional ways to solve the problem of building demand
1) The advantage is that it is easy to understand and 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
Application example of builder mode to solve building demand
Next, we use the builder pattern to complete
UML diagram
House products
public class House { private String basic; private String wall; private String roofed; public String getBasic() { return basic; } public void setBasic(String basic) { this.basic = basic; } 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; } }
HouseBuilder Abstract builder
public abstract class HouseBuilder { protected House house = new House(); /** * Write the construction process and abstract method */ abstract void builderBasic(); abstract void builderWall(); abstract void roofed(); /** * Build a house and return the product (house) * @return */ public House buildHouse(){ return house; } }
Two specific builders
public class CommonHouse extends HouseBuilder{ @Override void builderBasic() { System.out.println(" Ordinary house foundation "); } @Override void builderWall() { System.out.println(" Ordinary house wall "); } @Override void roofed() { System.out.println(" Building ordinary houses "); } }
public class HighHouse extends HouseBuilder{ @Override void builderBasic() { System.out.println(" The foundation of high-rise houses "); } @Override void builderWall() { System.out.println(" Wall high-rise houses "); } @Override void roofed() { System.out.println(" Building high-rise houses "); } }
HouseDirector
public class HouseDirector { HouseBuilder houseBuilder; /** * Construction method setting builder * @param houseBuilder */ public HouseDirector(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } /** * setter Method to set builder * @param houseBuilder */ public void setHouseBuilder(HouseBuilder houseBuilder) { this.houseBuilder = houseBuilder; } /** * House building process * @return */ public House constructHouse(){ this.houseBuilder.builderBasic(); this.houseBuilder.builderWall(); this.houseBuilder.roofed(); return this.houseBuilder.house; } }
test
public class BuilderClient { public static void main(String[] args) { CommonHouse commonHouse = new CommonHouse(); HouseDirector houseDirector = new HouseDirector(commonHouse); House house = houseDirector.constructHouse(); HighHouse highHouse = new HighHouse(); houseDirector.setHouseBuilder(highHouse); houseDirector.constructHouse(); } }
3, Precautions and details of builder mode
1) The client (user program) does not need to know the details of the internal composition of the product, and 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 more convenient 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 have similar components. If there are great differences between products, it is not suitable to use the builder mode, 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 realize this change, resulting in a huge system. Therefore, in this case, it is necessary to consider whether to choose the builder mode.
4, Abstract factory pattern VS builder pattern
Abstract factory mode realizes the creation of product family. 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.