“This is the 14th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
What is the Builder model
Constructor pattern definition: Separating the construction of a complex object from its representation so that the same construction process can create different representations. In essence, it is the process of constructing a complex object, and flexibly choose to construct different objects. For example, in the process of building a computer, you need to configure CPU, keyboard, mouse, screen, memory, graphics card, etc. Different collocation can configure different computers. For example, you can use Haier CPU, Haier graphics card and other components of haier type of computer (as if there is no Haier computer), or use changhong CPU, keyboard changhong computer, or some computers can not configure the graphics card, but the computer assembly is basically similar. But there is a wide selection of accessories to build different computers.
Realization of builder mode
@data public class Computer {// Computer needs mouse, keyboard, CPU, screen. private String screen; private String cpu; private String keyboard; }Copy the code
Product, in this case computer
public abstract class AbstractBuilder { Computer computer = new Computer(); public abstract void buildMouse(); public abstract void buildScreen(); public abstract void buildCpu(); public abstract void buildKeyboard(); public Computer getComputer(){ return computer; }}Copy the code
The abstract builder, which contains the build method of the parts of the product and the method of obtaining the product.
public class HaierBuilder extends AbstractBuilder { @Override public void buildMouse() { computer.setMouse("Haier Mouse"); } @Override public void buildScreen() { computer.setScreen("Haier Screen"); } @Override public void buildCpu() { computer.setCpu("Haier Cpu"); } @Override public void buildKeyboard() { computer.setKeyboard("Haier Keyboard"); }}Copy the code
The concrete builder, Haier builder, realized the composition method of haier’s various components.
public class ChanghongBuilder extends AbstractBuilder{
@Override
public void buildMouse() {
computer.setMouse("Changhong Mouse");
}
@Override
public void buildScreen() {
computer.setScreen("Changhong Screen");
}
@Override
public void buildCpu() {
computer.setCpu("Changhong Cpu");
}
@Override
public void buildKeyboard() {
computer.setKeyboard("Changhong Keyboard");
}
}
Copy the code
Changhong constructor.
public class Director { private AbstractBuilder builder; public Director(AbstractBuilder builder) { this.builder = builder; } public Computer construct() { builder.buildMouse(); builder.buildScreen(); builder.buildCpu(); builder.buildKeyboard(); return builder.getComputer(); }}Copy the code
Conductor: Calls the constructor’s methods to complete the construction of a complex object. The director embodies the consistency of the construction process, which means that the computer is assembled in a similar way.
public static void main(String[] args) {
AbstractBuilder builderA = new HaierBuilder();
Director directorA = new Director(builderA);
Computer computerA = directorA.construct();
System.out.println(computerA);
AbstractBuilder builderB = new ChanghongBuilder();
Director director = new Director(builderB);
Computer computerB = director.construct();
System.out.println(computerB);
}
Copy the code
Same build, but different objects are constructed by different builders.
Third, summary
Advantages: good encapsulation, structure and performance separation. Extensibility is good, and individual concrete constructors are independent of each other.
Disadvantages: The internal composition of the product is complex, if there is a change in the product, then all builders have to change.
Usage scenarios: such as game character construction, or email construction.
The difference between the builder model and the factory model: the builder model is mainly reflected in the construction of complex products. Designed to construct different objects, the factory pattern builds the same objects.