This is the 23rd day of my participation in Gwen Challenge


👉 Design mode directory

What is the Builder pattern
1. The concept

The Builder pattern is also known as the Builder pattern. The builder pattern is designed to make object creation easier. There are usually two classes designed. One is the target class, which is the class we need. The other class is the builder class, which makes it easier to create the target class.

2. Usage scenarios for the builder pattern
  1. The target class has too many attributes. When we need to set parameters, we need to set them one by one, or overload constructors, the former makes writing code very tedious; The latter have difficulty overloading the constructor they want.
  2. The attribute structure of the target class is complex. If we had to manually set properties and care about their internal structure every time we used the target class, the experience of using the target class would be poor.
3. The advantages
  1. High scalability.
  2. Easy to use.
4. The drawback
  1. You need to create a builder class.
  2. High dependence. Whenever the target class changes, the builder needs to change as well.
The principle of 5.

“+” for compliance, “-” for non-compliance or irrelevant

The principle of Open the closed Single responsibility Di milt Replacement on the Richter scale Dependency inversion Interface segregation Synthesis of reuse
+
Simple builder pattern implementation
Code 1.

User

package build;

/ * * *@authorXXJ * User */
public class User {
    private String name;
    private int age;
    private String sex;
    public User(a) {}public User(UserBuild build) {
        this.age=build.age;
        this.name=build.name;
        this.sex=build.sex;
    }
     @Override
    public String toString(a) {
        return "User{" +
                "name='" + name + '\' ' +
                ", age=" + age +
                ", sex='" + sex + '\' ' +
                '} '; }}Copy the code

UserBuild

package build;

/ * * *@authorXXJ * User builder */
public class UserBuild {
    public String name;
    public int age;
    public String sex;
    public UserBuild(a){}
    public UserBuild name(String name){
        this.name=name;
        return this;
    }
    public UserBuild age(int age){
        this.age=age;
        return this;
    }
    public UserBuild sex(String sex){
        this.sex=sex;
        return this;
    }
    public User build(a){
        return new User(this); }}Copy the code
2. Use
package test;

import build.User;
import build.UserBuild;

public class test {
    public static void main(String[] args){
        UserBuild build=new UserBuild();
        build.name("xiaoming").age(18).sex("Male");
        User user=newUser(build); System.out.println(user.toString()); }}Copy the code

The results of

3. Pay attention to
  1. When the builder is created, note that its methods return this.
  2. The builder can also set default values for properties, which saves much of the default parameter setting.
4. The last

I found this builder to be a little bit easier, without creating redundant objects at all

/ * * *@authorXuxiaobai * user */
public class User {
    private String name;
    private int age;
    private String sex;
    
    public User(a) {}public String getName(a) {
        return name;
    }

    public User setName(String name) {
        this.name = name;
        return this;
    }

    public int getAge(a) {
        return age;
    }

    public User setAge(int age) {
        this.age = age;
        return this;
    }

    public String getSex(a) {
        return sex;
    }

    public User setSex(String sex) {
        this.sex = sex;
        return this;
    }

    @Override
    public String toString(a) {
        return "User{" +
                "name='" + name + '\' ' +
                ", age=" + age +
                ", sex='" + sex + '\' ' +
                '} '; }}Copy the code

Each set method here returns the current object, eliminating the need for the UserBuild class and making it easy to use. IDEA allows us to quickly produce templates.

IDEA Builder steps:

  1. Hold Alt + Insert, or right click

  1. Go to the Getter and Setter

  1. Change the template for the Set method

  1. Finally, select all the properties and click Create.
Third, summary

The idea behind the builder pattern is to make it easier to create an object. As you can see from the example above, there is no need to create a builder when there are fewer attributes, but this is exactly the idea: Create a builder to build an object. As properties become more complex, the role of the builder comes into play.

— — — — — — — — — — — — — — —

The more you know, the more you don’t know.

If you have any questions about the content of this article, please comment directly or email me. If you think my writing is good, a “like” is also a sign of support

Shall not be reproduced without permission!