可选的Java构建器函数

huangapple go评论76阅读模式
英文:

Optional Java Builder Functions

问题

我正在使用一个导入的JAR包。在那个JAR包中,他们只允许使用构建器来创建对象。

例如:

public PersonBuilder oneRelation() {
    Person person = PersonBuilder.builder()
                      .firstName("Tom")
                      .lastName("Smith")
                      .relation(relationObject.builder().Name("Cousin").build());
}

public PersonBuilder twoRelation() {
    return PersonBuilder.builder()
                      .firstName("Tom")
                      .lastName("Smith")
                      .relation(relationObject.builder().Name("Parent").build())
                      .relation(relationObject.builder().Name("Sibling").build());
}

我不得不创建多个方法来处理拥有多个关系的情况。不幸的是,如果我向关系传递空值,它会给我一个错误。

有没有人成功地动态创建过一个构建器?我已经稍微修改了它,以便将一个关系传递给它,并使函数利用关系数组的大小。然而,我仍然需要在所有可能的关系数量上重复firstName和lastName。

英文:

I'm working with an imported jar. In that Jar, they only allow builders to create objects.

I.E.

public PersonBuilder oneRelation(){
person person = PersonBuilder.builder()
                  .firstName("Tom")
                  .lastName("Smith")
                  .relation(relationObject.builder().Name("Cousin").build())}

public PersonBuilder twoRelation(){
return PersonBuilder.builder()
                  .firstName("Tom")
                  .lastName("Smith")
                  .relation(relationObject.builder().Name("Parent").build())
                  .relation(relationObject.builder().Name("Sibling").build())}

I have to create multiple methods to handle having multiple relationships. Unfortunately, if I pass a null to the relation it gives me an error.

Has anyone had any luck dynamically creating a builder? I have modified it a bit to pass in a relation and have the function utilize the size of the relation array. However, I'm still duplicating the firstName and lastName over the total number of relationships I can have.

答案1

得分: 0

一个构建器只是一个对象,因此只需创建一个它的局部变量,并通过链式的 .relation() 方法进行循环。

public PersonBuilder relations(List<RelationObject> relations) {
  PersonBuilder builder = PersonBuilder.builder()
                  .firstName("Tom")
                  .lastName("Smith");
  relations.forEach(builder::relation);
  return builder;
}

你可以这样调用它:

relations(Arrays.asList(relationObject.builder().Name("Cousin").build()))

relations(Arrays.asList(relationObject.builder().Name("Parent").build(), 
                        relationObject.builder().Name("Sibling").build()))

你还可以使用可变参数(varargs),这样就不必创建列表:

public PersonBuilder relations(RelationObject... relations) {
  PersonBuilder builder = PersonBuilder.builder()
                  .firstName("Tom")
                  .lastName("Smith");
  for (RelationObject relation : relations) {
    builder.relation(relation);
  }
  return builder;
}

你可以这样调用它:

relations(relationObject.builder().Name("Cousin").build())

relations(relationObject.builder().Name("Parent").build(), 
          relationObject.builder().Name("Sibling").build())
英文:

A builder is just an object so just create a local variable of it and loop through the chained .relation() method.

public PersonBuilder relations(List&lt;RelationObject&gt; relations) {
  PersonBuilder builder = PersonBuilder.builder()
                  .firstName(&quot;Tom&quot;)
                  .lastName(&quot;Smith&quot;);
  relations.forEach(builder::relation);
  return builder;
}

And you can call it like this:

relations(Arrays.asList(relationObject.builder().Name(&quot;Cousin&quot;).build()))

relations(Arrays.asList(relationObject.builder().Name(&quot;Parent&quot;).build(), 
                        relationObject.builder().Name(&quot;Sibling&quot;).build()))

You can also use varargs so you don't have to create a list:

public PersonBuilder relations(RelationObject... relations) {
  PersonBuilder builder = PersonBuilder.builder()
                  .firstName(&quot;Tom&quot;)
                  .lastName(&quot;Smith&quot;);
  for (RelationObject relation : relations) {
    builder.relation(relation);
  }
  return builder;
}

And you can call it like this:

relations(relationObject.builder().Name(&quot;Cousin&quot;).build())

relations(relationObject.builder().Name(&quot;Parent&quot;).build(), 
          relationObject.builder().Name(&quot;Sibling&quot;).build())

huangapple
  • 本文由 发表于 2020年9月26日 00:53:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定