gpt4 book ai didi

java - 如何消除 2 个不同类的 util 类上的重复项

转载 作者:行者123 更新时间:2023-12-02 09:30:08 25 4
gpt4 key购买 nike

我声明了两个具有相同成员但由于某些原因分开的类。我实现了一个 util 类,将这 2 个类转换为 1 个相同的类,如下所示。

@Data
@Builder
public class VO1 {
private int id;
private String value1;
private String value2;
...
private String value20;
}

@Data
@Builder
public class VO2 {
private int id;
private String value1;
private String value2;
...
private String value20;
}

@Data
@Builder
public class DomainEntity {
private int id;
private String value1;
private String value2;
...
private String value15;
}

public class Converter {
public static DomainEntity fromVO1(VO1 vo1) {
return DomainEntity.builder.id(vo1.getId())
.value1(vo1.getValue1())
.value2(vo1.getValue2())
...
.value15(vo1.getValue15())
.build();
}

public static DomainEntity fromVO2(VO2 vo2) {
return DomainEntity.builder.id(vo2.getId())
.value1(vo2.getValue1())
.value2(vo2.getValue2())
...
.value15(vo2.getValue15())
.build();
}
}

现在我在“转换器”中有重复的代码,我想消除它们。当然我知道我可以使用继承来删除它。不过我也想使用“Lombok”,因为这些类有很多属性。并且使用继承使得“Lombok”的使用变得更加困惑。有谁知道解决这种情况的优雅方法?

谢谢。

最佳答案

我使用继承尝试了你的示例,我不觉得代码变得更困惑,我错过了什么吗?

对于继承,您可以使用实验性的 @SuperBuilder

       package lombok;

@Getter
@Setter
@SuperBuilder
public class BaseVO {


private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
private String s7;
private String s8;
private String s9;

}


package lombok;

@Getter
@Setter
@SuperBuilder
public class VO1 extends BaseVO{

private String vo1Specific;
}


package lombok;

@Getter
@Setter
@SuperBuilder
public class VO2 extends BaseVO {

private String vo2Specific;
}

package lombok;

@Builder
@ToString
public class DomainEntity {

private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
private String s7;
private String s8;
private String s9;
private String specific;

}


package lombok;

import lombok.DomainEntity.DomainEntityBuilder;

public class Converter {

public static <T extends BaseVO> DomainEntity fromVO(T basevo) {

DomainEntityBuilder builder = DomainEntity.builder();
builder.s1(basevo.getS1()).s2(basevo.getS2()).s3(basevo.getS3());

if (basevo instanceof VO1) {
VO1 vo1 = (VO1) basevo;
builder.specific(vo1.getVo1Specific());

}

if (basevo instanceof VO2) {
VO2 vo1 = (VO2) basevo;
builder.specific(vo1.getVo2Specific());

}

return builder.build();

}

}

package lombok;

public class App {

public static void main(String[] args) {

VO1 vo1 = VO1.builder().s1("VO1S1").vo1Specific("VO1 specific property").build();

VO2 vo2 = VO2.builder().s1("VO2S1").vo2Specific("VO2 specific property").build();

DomainEntity d1 = Converter.<VO1>fromVO(vo1);
System.out.println(" d1 " + d1);
DomainEntity d2 = Converter.<VO2>fromVO(vo2);

System.out.println(" d2 " + d2);

}

}

关于java - 如何消除 2 个不同类的 util 类上的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58054629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com