JavaBuilder模式实现

Posted JaxYoun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaBuilder模式实现相关的知识,希望对你有一定的参考价值。

一、借助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模式实现的主要内容,如果未能解决你的问题,请参考以下文章

java builder模式用法[重复]

用于从 cloudkit 检索单列的代码模式/片段

你可以在graphql服务器模式文件中使用片段吗

如何使用对象列表在片段中实现newinstace模式[重复]

尝试使用片段保存夜间模式状态

在 recyclerview 片段中实现上下文操作模式的问题