package study;


import lombok.Data;

@Data
public class Student  {
    Integer age;
    String name;
    String address ;
    public Student() {
    }

    public Student(Builder builder){
        this.name = builder.name;
    }

    public static  Builder builder() {
        return new Builder();
    }
    public static final class Builder{
        String name;
        Integer age;

        public Builder age (Integer age){
            this.age  = age;
            return this;
        }
        public Builder name (String name){
            if (name ==null){
                return this;
            }
            this.name  = name;
            return this;
        }

        public Student build(){

            return new Student(this);
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}

  

package patterns.builder;

import study.Student;

public class Test {
    public static void main(String[] args) {
        Student empty = new Student(); //student1
        Student student =  Student.builder()
                .name(empty.getAddress())
                .age(18)
                .build();
        System.out.println("student = " + student);

    }
}

  

 

posted on 2023-09-08 23:30  mljqqh  阅读(4)  评论(0编辑  收藏  举报