嵌套静态构建器类,对其管道的进一步解释
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这一点:
应该是:
因此,您首先创建一个
Builder
,然后Build
方法(顺便说一句,build
会更好)创建不可变的对象。如果您创建第二个实例,它将与第一个实例完全分开。不要被声明为静态的
Builder
类所迷惑 - 这仅意味着Builder
的实例没有隐式“父级”MyPojo< /code> 与其关联的类引用。单独的 Builder 实例的字段仍然是完全独立的。
This bit:
should be:
So you first create a
Builder
, and then theBuild
method (which would be better asbuild
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 ofBuilder
doesn't have an implicit "parent"MyPojo
class reference associated with it. The fields of separateBuilder
instances are still entirely separate.这里有几个错误。正如所写,它甚至无法编译。
查找 Fluent Interface 了解它的工作原理。现在考虑
(这不是您拥有的,这是您应该拥有的)
它是如何工作的:
new MyPojo.Builder(req)
创建一个 MyPojo.Builder 对象并将传递的 req 分配给this.required
.optionOne(1)
将 1 分配给MyPojo.Builder.OptionalOne
。然后它返回相同的对象(return this
)。这是关键,因为它允许下一步.optionOne(2)
将 2 分配给MyPojo.Builder.OptionalTwo
到MyPojo.Builder
上次调用返回的对象。请注意,我们仍在使用步骤 1 中创建的相同Builder
对象。this
再次返回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
(this is not what you have, this is what you should have)
This is how it works:
new MyPojo.Builder(req)
creates a MyPojo.Builder object and assigns the passed req tothis.required
.optionOne(1)
assigns 1 toMyPojo.Builder.optionalOne
. It then returns the same Object (return this
). This is the key, because it allows the next step.optionOne(2)
assigns 2 toMyPojo.Builder.optionalTwo
to theMyPojo.Builder
object returned by the previous call. Note that we are still working with the sameBuilder
object created in step 1.this
is returned againBuild()
callsBuild
method on theBuilder
, which in turn calls theMyPojo
constructor. This method should be calledbuild
to conform to Java naming conventions.EDIT: The code is not compilable.
EDIT2: Detailed explanation