Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

An overview of the

Building a complex object step by step from multiple simple objects is analogous to the @Builder annotation in Lambok

Build an object by pulling out some unchanging conditions

Encapsulates the construction of an object and allows step-by-step construction

Usage scenarios

When a class has more than four constructor arguments, and some of those arguments are optional, consider using the constructor pattern.

For example,

For example, building a house, steel bar, cement, wood is necessary, you can start from steel bar, cement, wood to build a house object. Potato chips, fried chicken, Cola chicken nuggets, can be constructed into a variety of meals

For example, when you build an object with the @Builder annotation, you can assign values to each property in the class, that is, assign values to each simple property, and build a complete object

Person.builder.name("tom").age(20).color("write").build;
Copy the code

Logic implementation

  1. Create A class A with many attributes and A variety of constructors
  2. Construct A static inner class ABuilder in A whose properties are the same as those of A
  3. The constructor parameters of ABuilder are required for the construction of A
  4. There are many setXXX() releases in ABuilder, and the return parameter is still ABuilder
  5. There is A build method with A return parameter of A

Code implementation

public class A{
    private final String color;/ / must
    private final String shape;/ / must
    private final int price;/ / is optional
    private final String detail;/ / is optional
    private final String tips;/ / is optional

    private A(ABuilder aBuilder){
        this.color=aBuilder.color;
        this.shape=aBuilder.shape;
        this.price=aBuilder.price;
        this.detail=aBuilder.detail;
        this.tips=aBuilder.tips;
    }
    public static class ABuilder{
        private String color;/ / must
        private String shape;/ / must
        private int price;/ / is optional
        private String detail;/ / is optional
        private String tips;/ / is optional

        public ABuilder(String color,String shape){
            this.color=color;
            this.shape=shape;
        }

        public ABuilder setPrice(int price) {
            this.price = price;
            return this;
        }
        public ABuilder setDetail(String detail) {
            this.detail = detail;
            return this;
        }
        public ABuilder setTips(String tips) {
            this.tips = tips;
            return this;
        }        
        public A build(a){
            return new A(this); }}// omit the getter method
}


A a=new A.ABuilder("red"."Circle")
                .setPrice(1)
                .setDetail("Big")
                .setTips("very nice")
                .build();
Copy the code

Lombok’s Builder annotations do just that

Also see Append and StringBuffer for StringBuilder

There is also a more formal implementation pattern: the Builder pattern through Client, Director, Builder, and Product

But the essential characteristics and the logic is consistent, but the implementation of more in line with the design characteristics. More abstract