Ten years of JAVA moving bricks - Builder Pattern (Builder Pattern)

definition:

The Builder Pattern is a creational design pattern used to decouple the construction process of an object from its representation. It allows complex objects to be built incrementally while maintaining flexibility and readability.

Common examples:

public class Person {
    private final String name;
    private final int age;
    private final String address;
     private Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
     public String getName() {
        return name;
    }
     public int getAge() {
        return age;
    }
     public String getAddress() {
        return address;
    }
     public static class PersonBuilder {
        private String name;
        private int age;
        private String address;
         public PersonBuilder setName(String name) {
            this.name = name;
            return this;
        }
         public PersonBuilder setAge(int age) {
            this.age = age;
            return this;
        }
         public PersonBuilder setAddress(String address) {
            this.address = address;
            return this;
        }
         public Person build() {
            return new Person(name, age, address);
        }
    }
}
//具体使用
Person person = new Person.PersonBuilder()
    .setName("John")
    .setAge(30)
    .setAddress("123 Main St")
    .build();

advantage:

  1. Provides a clear separation between the construction and presentation of objects.
    2. Allows the gradual construction of complex objects, making the code more readable and maintainable.
  2. Provides the flexibility to set different properties of an object without requiring multiple constructors or overloaded methods.
  3. Ensures that objects are fully constructed before use to avoid creating incomplete or inconsistent objects.
  4. Allows creation of immutable objects, providing thread safety and preventing accidental modification.

shortcoming:

  1. A separate builder class needs to be implemented, which can add complexity to the codebase.
  2. For simple objects with a few properties, the builder pattern may not be necessary as it may introduce unnecessary overhead.
  3. The builder pattern can lead to an increase in the number of classes and files in the project, which can make the codebase harder to navigate

scenes to be used:

  1. Create an object with multiple optional parameters: Through the builder pattern, you can avoid using multiple constructors or methods to handle different parameter combinations, making the code clearer and easier to maintain.
  2. Create immutable objects: By using the builder pattern, you can ensure that the object after construction is immutable, that is, the state of the object cannot be changed. This improves object safety and thread safety.
  3. Create objects with a consistent build process: The builder pattern allows us to define a unified build process, ensuring that all necessary steps are performed in a standardized manner.
  4. Create objects with different representations: The builder pattern can be used to create objects with different representations or configurations according to the same build process. This is useful when you need to create objects of different variants without duplicating the build logic.

Guess you like

Origin blog.csdn.net/weixin_43485737/article/details/131898041