码迷,mamicode.com
首页 > 编程语言 > 详细

JavaBuilder模式实现

时间:2021-06-17 16:46:29      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pac   pil   user   传参   com   file   oid   参与   ack   

一、借助lombok之@Builder注解

  1. User类源码:
package com.yang.webflux.controller;

import lombok.Builder;

/**
 * @author: Yang
 * @date: 2017/3/26 23:55
 * @description:
 */
@Builder
public class User {

    private long id;

    private String name;

    private int age;

}
  1. lombok插件配合@Builder注解介入编译再反编译的源码:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.yang.webflux.controller;

public class User {

    private long id;

    private String name;

    private int age;

    User(long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public static User.UserBuilder builder() {
        return new User.UserBuilder();
    }

    public static class UserBuilder {

        private long id;

        private String name;

        private int age;

        UserBuilder() {
        }

        public User.UserBuilder id(long id) {
            this.id = id;
            return this;
        }

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

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

        public User build() {
            return new User(this.id, this.name, this.age);
        }

        public String toString() {
            return "User.UserBuilder(id=" + this.id + ", name=" + this.name + ", age=" + this.age + ")";
        }
    }

}
  1. 使用:
public static void main(String[] args) {
        User user = User.builder()
                .id(1L)
                .name("yang")
                .age(3)
                .build();
}

二、手动实现:

package com.yang.webflux.controller;

import lombok.Data;

/**
 * @author: Yang
 * @date: 2017/3/26 23:41
 * @description:
 */
public class MyDsl {

    public static void main(String[] args) {
        Dog yang = Dog.builder().age(1).id(3L).name("yang").build();
        System.out.println(yang.getAge());
        System.out.println(yang.getId());
        System.out.println(yang.getName());
    }

}

@Data
class Dog {

    private long id;

    private String name;

    private int age;

    /**
     * 此构造函数的传参与lombok的有所不同
     */
    private Dog(Builder builder) {
        this.id = builder.id;
        this.name = builder.name;
        this.age = builder.age;
    }

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

    public static final class Builder {

        private long id;

        private String name;

        private int age;

        public Builder() {
        }

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

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

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

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

}

三、两种实现的源码差异较小,需要手动实现的场景里,可以参考。

JavaBuilder模式实现

标签:pac   pil   user   传参   com   file   oid   参与   ack   

原文地址:https://www.cnblogs.com/JaxYoun/p/14891725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!