Java 构建器模式示例

发布时间:2024年01月21日

手写实现

@Data
public class Student {

    private String name;

    private int age;

    private String clazz;

    private String course;

    private int score;

    public Student(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.clazz = builder.clazz;
        this.course = builder.course;
        this.score = builder.score;
    }

    public static Builder builder() {
        return new Student.Builder();
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", clazz='" + clazz + '\'' +
                ", course='" + course + '\'' +
                ", score=" + score +
                '}';
    }

    public static class Builder {
        private String name;

        private int age;

        private String clazz;

        private String course;

        private int score;

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

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

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

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

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

        public Student build() {
            return new Student(this);
        }
    }
}

使用:

public static void main(String[] args) {
        List<Student> students = Stream.of(
                Student.builder().name("小张").age(16).clazz("高一1班").course("历史").score(88).build(),
                Student.builder().name("小李").age(16).clazz("高一3班").course("数学").score(12).build(),
                Student.builder().name("小王").age(17).clazz("高二1班").course("地理").score(44).build(),
                Student.builder().name("小红").age(18).clazz("高二1班").course("物理").score(67).build(),
                Student.builder().name("李华").age(15).clazz("高二2班").course("数学").score(99).build(),
                Student.builder().name("小潘").age(19).clazz("高三4班").course("英语").score(100).build(),
                Student.builder().name("小聂").age(20).clazz("高三4班").course("物理").score(32).build()
        ).collect(Collectors.toList());
        
		System.out.println(students);
}

lombok 实现

@Data
@Builder
public class Student {
    private String name;

    private int age;

    private String clazz;

    private String course;

    private int score;
}

使用:

public static void main(String[] args) {
        List<Student> students = Stream.of(
                Student.builder().name("小张").age(16).clazz("高一1班").course("历史").score(88).build(),
                Student.builder().name("小李").age(16).clazz("高一3班").course("数学").score(12).build(),
                Student.builder().name("小王").age(17).clazz("高二1班").course("地理").score(44).build(),
                Student.builder().name("小红").age(18).clazz("高二1班").course("物理").score(67).build(),
                Student.builder().name("李华").age(15).clazz("高二2班").course("数学").score(99).build(),
                Student.builder().name("小潘").age(19).clazz("高三4班").course("英语").score(100).build(),
                Student.builder().name("小聂").age(20).clazz("高三4班").course("物理").score(32).build()
        ).collect(Collectors.toList());
        
		System.out.println(students);
}
文章来源:https://blog.csdn.net/weixin_43987408/article/details/135731690
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。