美文网首页Crazy_4J 菜园子
设计模式之-lombok中builder

设计模式之-lombok中builder

作者: Crazy_4J | 来源:发表于2018-08-05 12:17 被阅读0次
package com.example.model;

import lombok.*;

import java.util.Date;

/**
 * Copyright © 2018年08月05日 北京自如信息科技有限公司. All rights reserved.
 *
 * @Prject: demo_20180801
 * @Package: com.example.model
 * @Description: 设计模式之-lombok中builder
 * @author: Crazy4J
 * @date: 2018年08月05日 10:46
 * @version: V1.0
 */
@Data
@AllArgsConstructor
//@Builder
public class Student {
    private Integer id;
    private String name;
    private String address;
    private Date birthday;

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

   public static class Builder {
        private Integer id;
        private String name;
        private String address;
        private Date birthday;

        public Builder id(Integer id) {
            this.id = id;
            return this;
        }

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

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

        public Builder birthday(Date birthday) {
            this.birthday = birthday;
            return this;
        }

        public Student build() {
            return new Student(this.id, this.name, this.address, this.birthday);
        }
    }

    public static void main(String[] args) {
        Student student = Student.builder().id(1000).name("Crazy_4j")
                .address("北京").birthday(new Date()).build();
        System.out.println(student);
    }
}

相关文章

网友评论

    本文标题:设计模式之-lombok中builder

    本文链接:https://www.haomeiwen.com/subject/chkbvftx.html