1, Builder mode
1. Basic introduction
- Builder moms, also known as generator pattern, is an object construction pattern. It can abstract the construction process of complex objects, so that different implementation methods of this abstract process can construct objects with different expressions.
- 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.
2. Four roles of construction mode
- Product role: a specific product object.
- Builder (Abstract builder): create an interface specified by each part of a Product object.
- Concretebuilder: implement interfaces, build and assemble various components.
- Director: build an object using the Builder interface, which 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.
2, Application examples
1. Product (product role)
//Product - Product (product role)
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;
}
}
2. Builder (Abstract builder)
// Builder (Abstract builder)
public abstract class HouseBuilder {
protected House house = new House();
//Write the construction process and abstract the method
//Laying foundation
public abstract void buildBasic();
//Build a wall
public abstract void buildWalls();
//Capping
public abstract void roofed();
//Build a house and return the product
public House buildHouse(){
return house;
}
}
3. Concretebuilder (specific builder)
- Ordinary house
public class CommonHouse extends HouseBuilder{
@Override
public void buildBasic() {
System.out.println(" The foundation of an ordinary house is 5m");
}
@Override
public void buildWalls() {
System.out.println(" Ordinary house wall 10 meters");
}
@Override
public void roofed() {
System.out.println(" Ordinary house capping");
}
}
- Tall buildings
public class HighBuilding extends HouseBuilder{
@Override
public void buildBasic() {
System.out.println(" High building foundation 100 m");
}
@Override
public void buildWalls() {
System.out.println(" Building wall 20 cm");
}
@Override
public void roofed() {
System.out.println("The roof of a tall building");
}
}
4. Director
// Concretebuilder specifies the specific production process
public class HouseDirecter {
HouseBuilder houseBuilder = null;
//Constructor incoming
public HouseDirecter(HouseBuilder houseBuilder){
this.houseBuilder = houseBuilder;
}
//Through setter method
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();
}
}
5. Testing
public class Client {
public static void main(String[] args) {
//Build an ordinary house
CommonHouse commonHouse = new CommonHouse();
//The commander ready to create the house
HouseDirecter houseDirecter = new HouseDirecter(commonHouse);
House house = houseDirecter.constructHouse();
}
}
3, Precautions
- The client does not 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
- 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
- The product creation process can be controlled more finely, and the creation steps of complex products can be decomposed into different methods, which makes the creation process clearer and more convenient to use programs to control the creation process.
- Adding a new specific builder does not need to modify the code of the original class library. The commander programs for the abstract builder class, which is convenient for system expansion and conforms to the "opening and closing principle".