嵌套静态构建器类,对其管道的进一步解释

发布于 2024-12-04 15:37:09 字数 1257 浏览 1 评论 0原文

public class MyPojo{
    String required;
    Integer optionalOne;
    Integer optionalTwo;

    private MyPojo(Builder builder){
        this.required = builder.required
        this.optionalOne = builder.one;
        this.optionalTwo = builder.two;
    }

    public static class Builder{

        String required;
        Integer optionalOne =0;
        Integer optionalTwo =0;

        public Builder(String req){
            this.required = req;
            return this;
        }
        public Builder optionalOne(Integer one){
            this.one = one;
            return this;
        }
        public Builder optionalTwo(Integer two){
            this.two = two;
            return this;
        }
        public MyPojo build(){
            return new MyPojo(this);
        }
    }
}

然后这样称呼:

MyPojo myPojo = new MyPojo.Builder("req").optionalOne(1).optionalTwo(2).build();

这一切都很可爱,但我不明白其中的几个部分。

有两条New语句,一条在调用语句中,一条在build()方法中,但只创建了一个新对象?

另外,如果我在没有第二个可选参数的情况下第二次调用:

MyPojo myPojo = new MyPojo.Builder("req").opticalOne(1).build();

为什么 optionTwo 会恢复为默认值(零)。并且不保留第一次传递的值(2),它是一个静态类,因此所有 MyPojos 之间共享一个实例?

public class MyPojo{
    String required;
    Integer optionalOne;
    Integer optionalTwo;

    private MyPojo(Builder builder){
        this.required = builder.required
        this.optionalOne = builder.one;
        this.optionalTwo = builder.two;
    }

    public static class Builder{

        String required;
        Integer optionalOne =0;
        Integer optionalTwo =0;

        public Builder(String req){
            this.required = req;
            return this;
        }
        public Builder optionalOne(Integer one){
            this.one = one;
            return this;
        }
        public Builder optionalTwo(Integer two){
            this.two = two;
            return this;
        }
        public MyPojo build(){
            return new MyPojo(this);
        }
    }
}

Which is then called like this :

MyPojo myPojo = new MyPojo.Builder("req").optionalOne(1).optionalTwo(2).build();

Which is all lovely, but I don't understand a couple of parts.

There are two New statements one in the calling statement and one in the build() method, but there is only one new object created ?

Also, if I then call a second time , without second optional paremeter :

MyPojo myPojo = new MyPojo.Builder("req").optionalOne(1).build();

Why will optionalTwo revert back to default value (zero). And not keep the value passed in first time(2), its a static class so one instance shared between all MyPojos ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2024-12-11 15:37:09

这一点:

 new MyPojo().Builder("req")

应该是:

 new MyPojo.Builder("req")

因此,您首先创建一个 Builder,然后 Build 方法(顺便说一句,build 会更好)创建不可变的对象。

如果您创建第二个实例,它将与第一个实例完全分开。不要被声明为静态的 Builder 类所迷惑 - 这仅意味着 Builder 的实例没有隐式“父级”MyPojo< /code> 与其关联的类引用。单独的 Builder 实例的字段仍然是完全独立的。

This bit:

 new MyPojo().Builder("req")

should be:

 new MyPojo.Builder("req")

So you first create a Builder, and then the Build method (which would be better as build btw) creates the immutable object.

If you create a second instance, that's entirely separate from the first. Don't be fooled by the Builder class being declared as static - that just means that an instance of Builder doesn't have an implicit "parent" MyPojo class reference associated with it. The fields of separate Builder instances are still entirely separate.

随波逐流 2024-12-11 15:37:09

这里有几个错误。正如所写,它甚至无法编译。

查找 Fluent Interface 了解它的工作原理。现在考虑

MyPojo myPojo = new MyPojo.Builder(req).optionalOne(1).optionalTwo(2).Build();

(这不是您拥有的,这是您应该拥有的)

它是如何工作的:

  1. new MyPojo.Builder(req) 创建一个 MyPojo.Builder 对象并将传递的 req 分配给 this.required
  2. .optionOne(1) 将 1 分配给 MyPojo.Builder.OptionalOne。然后它返回相同的对象(return this)。这是关键,因为它允许下一步
  3. .optionOne(2) 将 2 分配给 MyPojo.Builder.OptionalTwoMyPojo.Builder上次调用返回的对象。请注意,我们仍在使用步骤 1 中创建的相同 Builder 对象。 this 再次返回
  4. Build() 调用 Build< Builder 上的 /code> 方法,该方法又调用 MyPojo 构造函数。此方法应称为 build 以符合 Java 命名约定。

编辑:代码不可编译。
EDIT2:详细解释

There are several errors here. As written it does not even compile.

Look up Fluent Interface for how it is supposed to work. Now consider

MyPojo myPojo = new MyPojo.Builder(req).optionalOne(1).optionalTwo(2).Build();

(this is not what you have, this is what you should have)

This is how it works:

  1. new MyPojo.Builder(req) creates a MyPojo.Builder object and assigns the passed req to this.required
  2. .optionOne(1) assigns 1 to MyPojo.Builder.optionalOne. It then returns the same Object (return this). This is the key, because it allows the next step
  3. .optionOne(2) assigns 2 to MyPojo.Builder.optionalTwo to the MyPojo.Builder object returned by the previous call. Note that we are still working with the same Builder object created in step 1. this is returned again
  4. Build() calls Build method on the Builder, which in turn calls the MyPojo constructor. This method should be called build to conform to Java naming conventions.

EDIT: The code is not compilable.
EDIT2: Detailed explanation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文