如何使用其构建器创建泛型类的实例?

问题描述 投票:0回答:1

我正在编写一些代码,其中有许多

Child...
类继承了
Parent
ChildA
ChildB
类。我正在使用 Lombok 的
@SuperBuilder
注释
为所有这些类生成构建器。代码看起来像这样 -

Parent.java

package test;

import java.util.Random;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
public class Parent {
    protected int parentVariable;
    protected Random random;

    Parent() {
        random = new Random();
    }
}

ChildA.java

package test;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
public class ChildA extends Parent {
    private int childAInteger;
    private String childAString;

    public ChildA execute() {
        int someInteger = random.nextInt(10);
        return buildResponse(someInteger);
    }

    private static ChildA buildResponse(int someInteger) {
        if (someInteger == 1) {
            return ChildA.builder().childAInteger(0).childAString("").build();
        }
        return ChildA.builder().parentVariable(1).build();
    }
}

ChildB.java

package test;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
public class ChildB extends Parent {
    private float childBFloat;
    private long childBLong;

    public ChildB execute() {
        int someInteger = random.nextInt(10);
        return buildResponse(someInteger);
    }

    private static ChildB buildResponse(int someInteger) {
        if (someInteger == 1) {
            return ChildB.builder().childBFloat(0.0F).childBLong(0L).build();
        }
        return ChildB.builder().parentVariable(1).build();
    }
}

如您所见,

buildResponse
方法在
Child...
类中有一些重复的代码。因为,我有很多
Child...
类,这种重复会增加。

我正在尝试重构此代码,以便将

buildResponse
方法的公共部分移至
Parent
类中,并为其提供一个方法来为所有
Child...
类运行其代码的独特部分。

类似这样的事-

Parent.java

package test;

import java.util.Random;
import java.util.function.Supplier;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
public class Parent {
    protected int parentVariable;
    protected Random random;

    Parent() {
        random = new Random();
    }

    protected static <T extends Parent> T buildResponse(int someInteger, Supplier<T> supplier) {
        if (someInteger == 1) {
            return supplier.get();
        }
        return T.builder().parentVariable(1).build();
    }
}

ChildA.java

package test;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Getter
public class ChildA extends Parent {
    private int childAInteger;
    private String childAString;

    public ChildA execute() {
        int someInteger = random.nextInt(10);
        return buildResponse(someInteger, ChildA::buildChildA);
    }

    private static ChildA buildChildA() {
        return ChildA.builder().childAInteger(0).childAString("").build();
    }
}

显然,

return T.builder().parentVariable(1).build();
类中的
Parent
行是不正确的。我的问题是我该如何进行这项工作?还可能吗?

谢谢!

java generics inheritance refactoring lombok
1个回答
0
投票

如果你使用组合而不是继承,你可以轻松解决这个问题:

@Builder
@Getter
class Parent {
  // parent fields
}

@Builder
@Getter
class ChildA {
  // @Delegate or something? I'm pretty sure Lombok can do that too.
  Parent parent; // supply this using `ParentBuilder`
  // other fields
}
© www.soinside.com 2019 - 2024. All rights reserved.