Life is short, only a few hundred months; Life is very long, about 3.536 billion seconds, in the premise of ensuring the health, learn more things, dispel their own ignorance…

Scientific and perfect explanation

Separating the creation and display of a complex object allows the same build process to create different representations. For example, a User class has attributes like name, age, address, email, job… If you want to create a User object and passing in all the attributes is too long, you can use builder mode, passing in a parameter and setting only the value of the corresponding attribute.

Application Scenario

If a class has many attributes, we can use the constructor with the set() method to prevent the long list of arguments from affecting the readability and usability of the code. However, if there are any of the following, we should consider using the Builder pattern.

  • We place the mandatory property of the class in the constructor, which is set when the object is created. If you have a lot of required attributes and put them all in the constructor, the constructor will have a long argument list again. If we set the required properties through the set() method, there is no logic to verify that the required properties have been filled in.
  • If there are dependencies or constraints between attributes of a class, we continue to use constructors with the set() method, and the validation logic for those dependencies or constraints is nowhere to be found. (such as set up A property, you must set properties B | | A attribute value is 1, B the value of the attribute must be A 2)
  • If we want to create immutable objects, that is, objects cannot change their internal property values once they are created, we cannot expose the set() method in the class to do this. The way the constructor works with the set() method to set property values is not applicable.

Code implementation

/ * * *@author a small asshole
 * @version 1.0
 * @descriptionConstructor mode * in the following ConstructorArg class, when isRef is true, arg represents a refBeanId of type String. Type does not need to be set; * When isRef is false, arg and type need to be set. * Complete the ConstructorArg class according to this requirement. *@date in 15:10 2020/2/27
 * @since1.0 * /
public class ConstructorArg {
    private boolean isRef;
    private Class type;
    private Object arg;

    public boolean isRef(a) {
        return isRef;
    }

    public Class getType(a) {
        return type;
    }

    public Object getArg(a) {
        return arg;
    }

    private ConstructorArg(Builder builder){
        this.arg = builder.arg;
        this.type = builder.type;
        this.isRef = builder.isRef;
    }

    public static Builder builder(a){
        return new Builder();
    }
    public static class Builder {
        private boolean isRef;
        private Class type;
        private Object arg;

        public Builder isRef(boolean isRef) {
            this.isRef = isRef;
            return this;
        }

        public Builder type(Class type) {
            this.type = type;
            return this;
        }

        public Builder arg(Object arg) {
            this.arg = arg;
            return this;
        }

        public ConstructorArg build(a) {
            if(! isRef) {if (arg == null || type == null) {
                    throw new IllegalArgumentException("Properties set incorrectly, please check the properties set."); }}if(! (arginstanceof String) || "".equalsIgnoreCase((String)arg)){
                throw new IllegalArgumentException("Properties set incorrectly, please check the properties set.");
            }
            return new ConstructorArg(this); }}public static void main(String[] args) {
        ConstructorArg constructorArg = ConstructorArg.builder()
                .isRef(true)
                .arg(123) .type(Resources.class) .build(); }}Copy the code

Additional notes

Builder mode is relatively easy to use. Many frameworks use Builder mode, such as Lombok’s @Builder annotation.

conclusion

The best way to learn a skill is to take it slow and try it yourself.