This is the 13th day of my participation in Gwen Challenge

What is chain programming

Chain programming is the method that can be passed. The principle is to return a this object, that is, return itself, to achieve the chain effect.

The StringBuffer that we use all the time implements the chain notation.

It’s nice to use this to return the current this object after the value is set.

StringBuffer#append()Source:

Lombok chain programming

Introduction of depend on

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.2</version>
</dependency>
Copy the code

@Accessors(chain = true)

Lombok provides an implementation of this way of writing, simply by adding an @accessors (chain = true) to the Bean object.

@Accessors(chain = true)
@Data
public class Student {
    private String name;
    private int age;
}
Copy the code

By looking at it, you can see that the set method returns itself, which makes the code much cleaner and allows chained programming.

Lombok’s static chained programming

Advantages of static chained programming over regular chained programming:

  • Creating objects is simpler;
  • Can be used in static methods, static code blocks, and so on;
  • For mandatory fields, you can make them mandatory.
import lombok.*;
import lombok.experimental.Accessors;

@Accessors(chain = true)
@Data
@RequiredArgsConstructor(staticName = "of")
public class Student {
	@NonNull
	private String name;
	private int age;
}
Copy the code

Used as follows, this achieves not only chained programming, but also static creation.

@Test
public void test1(a){
    Student ss = Student.of("zhangsan").setAge(22);
    System.out.println(ss.getAge());
    System.out.println(ss.getName());
}
Copy the code

Chain beans in Builder mode

The chain Bean principle of Builder mode is: Create a static Builder method and a static internal Builder class inside the bean. Create a builder class by calling the static Builder method and then create a bean directly by calling the Build method in the Builder class.

@Data
public class StudentBean {
    private String name;
    private int age;

    public static Builder builder(a) {
            return new Builder();
    }

    public static class Builder{
            private String name;

            private int age;

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

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

            public StudentBean build(a) {
                    StudentBean studentBean = new StudentBean();
                    studentBean.setName(name);
                    studentBean.setAge(age);
                    returnstudentBean; }}}Copy the code

Use:

StudentBean studentBean = StudentBean.builder().name("zhangsan").age(11).build();
Copy the code

Lombok implements builder pattern chain beans

This implements a Chain bean in Builder mode. Using Lombok is all about annotations.

import lombok.*;

@Builder
@Data
public class StudentBean {
	private String name;
	
	private int age;
}
Copy the code

👍 ❤️ Don’t get lost

The article continues to update every week, you can search wechat “ten minutes to learn programming” the first time to read and urge more, if this article is not bad, feel something if you support and recognition, is the biggest power of my creation, we will see the next article!