当我们使用龙目岛建造者建立继承关系时,如何构建对象?

2022-09-03 04:51:05

在我的项目中,我使用龙目岛来避免为类编写 getter 和 setter。另外,我正在使用龙目岛。生成器来构建一个对象,而不是编写新的 Obeject(), 然后设置所有值。

但是,当我们具有继承关系并且想要使用龙目岛构建器构造子对象时,我没有得到父对象字段。

例如:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class Parent{
  private String nationality;
  .
  .
  // more columns
}

Child 类将如下所示:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class Child extends Parent{
   private String firstName;
   private String lastName;
   .
   .
}

在我的测试类中,我需要构建子对象

public class Test{

 public void testMethod(){
   Child child = Child.builder()
            .firstName("Rakesh")
            .lastName("SS")
            .nationality("some text")// I am not able to set nationality               
            .build();
 }


}

请让我知道,有没有办法在龙目岛处理这种情况。


答案 1

@Builder无法确定您希望公开哪些字段。Parent

当 放置在类上时,只有在该类上显式声明的字段才会添加到 .@Builder*Builder

当放置在静态方法或构造函数上时,结果将具有每个参数的方法。@Builder*Builder

另外,如果您正在使用,那么是否可以安全地假设至少意味着是不可变的?@BuilderChild

我举了两个例子,一个是可变的和不可变的,另一个是两者都是不可变的。ParentChildParentChild

不可变的父项和子项

import static org.junit.Assert.*;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;
import lombok.experimental.NonFinal;

import org.junit.Test;

public class So32989562ValueTest {

    @Value
    @NonFinal
    public static class Parent {

        protected final String nationality;

    }

    @Value
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper = true)
    public static class Child extends Parent {

        private final String firstName;

        private final String lastName;

        @Builder(toBuilder = true)
        private Child(String nationality, String firstName, String lastName) {
            super(nationality);
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }

    @Test
    public void testChildBuilder() {

        String expectedFirstName = "Jeff";
        String expectedLastName = "Maxwell";
        String expectedNationality = "USA";

        Child result = Child.builder()
            .firstName(expectedFirstName)
            .lastName(expectedLastName)
            .nationality(expectedNationality)
            .build();

        assertEquals(result.toString(), expectedFirstName, result.getFirstName());
        assertEquals(result.toString(), expectedLastName, result.getLastName());
        assertEquals(result.toString(), expectedNationality, result.getNationality());
    }
}

可变父项,不可变子项:

import static org.junit.Assert.*;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;

import org.junit.Test;

public class So32989562DataTest {

    @Data
    public static class Parent {

        protected String nationality;

    }

    @Value
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper = true)
    public static class Child extends Parent {

        private final String firstName;

        private final String lastName;

        @Builder(toBuilder = true)
        private Child(String nationality, String firstName, String lastName) {
            this.setNationality(nationality);
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }

    @Test
    public void testChildBuilder() {

        String expectedFirstName = "Jeff";
        String expectedLastName = "Maxwell";
        String expectedNationality = "USA";

        Child result = Child.builder()
            .firstName(expectedFirstName)
            .lastName(expectedLastName)
            .nationality(expectedNationality)
            .build();

        assertEquals(result.toString(), expectedFirstName, result.getFirstName());
        assertEquals(result.toString(), expectedLastName, result.getLastName());
        assertEquals(result.toString(), expectedNationality, result.getNationality());
    }
}

答案 2

上述解决方案有效,但是这需要太多的解决方法。此外,子类和父类中的任何更改都需要在任何地方更改构造函数参数。

龙目岛在版本1.18.2中引入了实验性功能,用于解决Builder注释面临的继承问题,并且可以通过@SuperBuilder注释解决,如下所示。

@SuperBuilder
public class ParentClass {
    private final String a;
    private final String b;
}

@SuperBuilder
public class ChildClass extends ParentClass{
    private final String c;
}

现在,可以使用如下所示的Builder类(这在@Builder注释中是不可能的)

ChildClass.builder().a("testA").b("testB").c("testC").build();

推荐