gpt4 book ai didi

java - 让构建器返回具有相同字段的两个不同 POJO 的对象的最佳方法是什么

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

POJO1

@Builder
Class POJO1
{
private String astring;
private String bstring;
}

POJO2

@Builder
Class POJO2
{
private String astring;
private String bstring;
}

构建器方法

private POJO1 buildPOJO()
{
return POJO1.builder().withaString().withbString().build();
}

我想要什么

POJO1 pojo = buildPOJO();
POJO2 pojo = buildPOJO(); // Same method to build both pojo's
<小时/>

我的代码中有这两个 pojo。我必须在几个地方 build 它们。两个 pojo 总是包含相同的字段,并且字段的数量也很大。我必须构建一个 buildPOJO 方法,通过它我可以创建两个 POJO 的对象。我没有明确的想法,但因为这些领域总是相似的。类型转换或任何其他方式有助于实现这一目标吗?

最佳答案

通过代理包装器,您可以使用通用接口(interface),而无需在类中实现它。将公共(public) setter 提取到接口(interface)并在构建器中使用它来设置公共(public)字段。

代理代码最少:

public class Wrapper<T> implements InvocationHandler {
private final T delegate;

public Wrapper(T delegate) {
this.delegate = delegate;
}

public static <T, I> I proxy(T delegate, Class<I> facade) {
return (I) Proxy.newProxyInstance(
facade.getClassLoader(),
new Class[] { facade },
new Wrapper(delegate)
);
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return delegate.getClass().getDeclaredMethod(method.getName(), method.getParameterTypes()).invoke(delegate, args);
}
}

并且足够简单,可以在构建器中使用:

class PojoBuilder {
private String foo;
private int bar;
private String a, b;

public PojoBuilder withFoo(String foo) {
this.foo = foo;
return this;
}

public PojoBuilder withBar(int bar) {
this.bar = bar;
return this;
}

public PojoBuilder withA(String a) {
this.a = a;
return this;
}

public PojoBuilder withB(String b) {
this.b = b;
return this;
}

public PojoA buildA() {
PojoA a = new PojoA();
buildCommon(Wrapper.proxy(a, Pojo.class));
a.setA(this.a);
return a;
}

public PojoB buildB() {
PojoB b = new PojoB();
buildCommon(Wrapper.proxy(b, Pojo.class));
b.setB(this.b);
return b;
}

private void buildCommon(Pojo common) {
common.setFoo(foo);
common.setBar(bar);
}
}

// Common setters for all pojos
interface Pojo {
void setFoo(String foo);
void setBar(int bar);
}

// One of the pojos.
// Note that this doesn't actually implement Pojo
class PojoA {
private String foo;
private int bar;
private String a;

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}

public int getBar() {
return bar;
}

public void setBar(int bar) {
this.bar = bar;
}

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}
}

class PojoB {
private String foo;
private int bar;
private String b;

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}

public int getBar() {
return bar;
}

public void setBar(int bar) {
this.bar = bar;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}
}

关于java - 让构建器返回具有相同字段的两个不同 POJO 的对象的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46550750/

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