Builder pattern definition
Builder mode Chinese called Builder mode, also known as generator mode, it belongs to the object creation mode, is to separate the construction of a complex object and its representation, so that the same construction process can create different representation. The Builder pattern, which creates a complex object step by step, allows users to build complex objects simply by specifying their type and content, without needing to know the specific build details inside. The following is a generic class diagram for the Builder pattern:
- Product: Product role
- Builder: An abstract Builder that defines a product interface
- ConcreteBuilder: ConcreteBuilder that implements the interface defined by the Builder and returns the assembled product
- -Dan: Well, I’m the Director. You go to the Director for whatever you need to produce.
Examples of builder pattern applications
Domestic outfit
Home decoration whether hardcover or simple decoration, its process is relatively fixed, so it is suitable for the builder mode. Let’s take home decoration as an example to learn about the builder model. The following is a simple UML diagram of the Home Improvement Builder pattern.
1. Home decoration object class
/** * Home decoration object class */
public class House {
/ / to buy home appliances
private String jiadian;
/ / buy the floor
private String diban;
/ / buy the paint
private String youqi;
public String getJiadian(a) {
return jiadian;
}
public void setJiadian(String jiadian) {
this.jiadian = jiadian;
}
public String getDiban(a) {
return diban;
}
public void setDiban(String diban) {
this.diban = diban;
}
public String getYouqi(a) {
return youqi;
}
public void setYouqi(String youqi) {
this.youqi = youqi;
}
@Override
public String toString(a) {
return "House{" +
"jiadian='" + jiadian + '\' ' +
", diban='" + diban + '\' ' +
", youqi='" + youqi + '\' ' +
'} '; }}Copy the code
2. The abstract Builder class
/** * Abstract builder */
public interface HouseBuilder {
/ / to buy home appliances
void doJiadian(a);
/ / buy the floor
void doDiBan(a);
/ / buy the paint
void doYouqi(a);
House getHouse(a);
}
Copy the code
3. Concrete Builder – Simple Builder category
/** * simple installation creator */
public class JianzhuangBuilder implements HouseBuilder {
private House house = new House();
@Override
public void doJiadian(a) {
house.setJiadian("Simple appliances are fine.");
}
@Override
public void doDiBan(a) {
house.setDiban("Ordinary floor");
}
@Override
public void doYouqi(a) {
house.setYouqi("Less polluting paint will do.");
}
@Override
public House getHouse(a) {
returnhouse; }}Copy the code
4. Concrete builder – Hardcover builder
Public implements HouseBuilder {private House House = new House(); @Override public voiddoJiadian() {
house.setJiadian("Without a second word, the best.");
}
@Override
public void doDiBan() {
house.setDiban("Without further ado, hardwood floors.");
}
@Override
public void doYouqi() {
house.setYouqi("Without further ado, give me zero contamination.");
}
@Override
public House getHouse() {
returnhouse; }}Copy the code
5. Commander – Home improvement Company category
/** * home decoration company, the value of the need to tell him hardcover or simple decoration */
public class HouseDirector {
public House builder(HouseBuilder houseBuilder){
houseBuilder.doDiBan();
houseBuilder.doJiadian();
houseBuilder.doYouqi();
returnhouseBuilder.getHouse(); }}Copy the code
6, test,
public class App {
public static void main(String[] args) {
house();
}
public static void house(a){
HouseDirector houseDirector = new HouseDirector();
/ / paperback
JianzhuangBuilder jianzhuangBuilder = new JianzhuangBuilder();
System.out.println("I want to keep it simple.");
System.out.println(houseDirector.builder(jianzhuangBuilder));
/ / the hardcover
jingzhuangBuilder jingzhuangBuilder = new jingzhuangBuilder();
System.out.println("I want a hardcover."); System.out.println(houseDirector.builder(jingzhuangBuilder)); }}Copy the code
The output
We take home decoration as an example, to achieve two specific builders, a simple builder, a hardcover builder. We just need to tell the home decoration company whether I need simple or hardcover, and he will help us arrange, I don’t need to know the specific details inside. How’s it going? Did you get back to builder mode?
Object to build
In your daily development, do you often see code like this:
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.curry.springbootswagger.controller"))
.paths(PathSelectors.any())
.build();
Copy the code
Isn’t it beautiful? Once you’ve learned the Builder pattern, you can also build objects this way. It is implemented through a variant of the Builder pattern. Without explanation, we will use the Builder mode to achieve the above object construction, using the student class as an example.
Student Object code:
public class Student {
private String name;
private int age;
private int num;
private String email;
// Provide a static Builder method
public static Student.Builder builder(a) {
return new Student.Builder();
}
// External calls to the property interface of the Builder class to set the value.
public static class Builder{
private String name;
private int age;
private int num;
private String email;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
public Builder num(int num) {
this.num = num;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Student build(a) {
// Pass the Builder object to the student constructor
return new Student(this); }}// Privatize constructor
private Student(Builder builder) {
name = builder.name;
age = builder.age;
num = builder.num;
email = builder.email;
}
@Override
public String toString(a) {
return "Student{" +
"name='" + name + '\' ' +
", age=" + age +
", num=" + num +
", email='" + email + '\' ' +
'} '; }}Copy the code
Calling code:
public static void student(a){
Student student = Student.builder().name("Crew head.").num(1).age(18).email("Crew head @163.com").build();
System.out.println(student);
}
Copy the code
As you can see, the mutant Builder mode includes the following:
- Create a static inner class Builder inside the class you want to build
- The parameters of the static inner class are the same as those of the build class
- The construction parameters of the build class are static inner classes, and the variables of the static inner class are assigned to the build class one by one
- The static inner class provides setter methods for parameters and returns the current Builder object
- Finally, a build method is provided to build an object of the build class, taking the current Builder object
You may say that this is too cumbersome to implement and requires a lot of extra code, but lombok has come to the rescue. All you need to do is introduce the Lombok plugin, add the @Builder annotation to the entity class, and you can use Builder mode to build objects.
Advantages and disadvantages of the Builder model
advantages
- In the Builder pattern, the client does not have to know the details of the product’s internal composition, decoupling the product itself from the product creation process so that the same creation process can create different product objects
- Each concrete builder is relatively independent of other concrete builders, so it is easy to replace concrete builders or add new concrete builders, and users can use different concrete builders to get different product objects
- The product creation process can be more finely controlled. Breaking down the creation steps of complex products into different methods makes the creation process clearer and easier to control programmatically
- Adding a new concrete builder does not need to modify the code of the original class library. The commander class is programmed for the abstract builder class, and the system is easy to expand and conforms to the “open and close principle”.
disadvantages
- The products created by the Builder mode generally have more in common and their components are similar. If the differences between the products are large, the builder mode is not suitable for use, so its application scope is limited to a certain extent.
- If the internal changes of the product are complex, many specific builder classes may need to be defined to implement the changes, resulting in a large system.
Article insufficient place, hope everybody gives directions a lot, common study, common progress
The last
Play a small advertisement, welcome to scan the code to pay attention to the wechat public number: “The technical blog of the flathead brother”, progress together.