我有以下想要通用的学习课程:

public abstract class Study<
    T1 extends Context,
    T2 extends Region,
    T3 extends Domain<T2>,
    T4 extends Solution> {...


派生类的示例如下:

public class AmericanCultureStudy<
    T1 extends AmericanCultureContext,
    T2 extends AmericanCultureRegion,
    T3 extends AmericanCultureDomain<T2>,
    T4 extends AmericanCultureSolution>
extends Study<T1, T2, T3, T4> {...

public class ContemporaryAmericanCultureStudy<
    T1 extends ContemporaryAmericanCultureContext,
    T2 extends ContemporaryAmericanCultureRegion,
    T3 extends ContemporaryAmericanCultureDomain<T2>,
    T4 extends ContemporaryAmericanCultureSolution>
extends AmericanCultureStudy<T1, T2, T3, T4> {...

public class ContemporaryMainstreamAmericanCultureStudy<
    T1 extends ContemporaryMainstreamAmericanCultureContext,
    T2 extends ContemporaryMainstreamAmericanCultureRegion,
    T3 extends ContemporaryMainstreamAmericanCultureDomain<T2>,
    T4 extends ContemporaryMainstreamAmericanCultureSolution>
extends ContemporaryAmericanCultureSolution<T1, T2, T3, T4> {...


这种设计的结果是,在主代码中实例化一个类变得很繁琐,如下所示:

ContemporaryMainstreamAmericanCultureStudy<
    ContemporaryMainstreamAmericanCultureContext,
    ContemporaryMainstreamAmericanCultureRegion,
    ContemporaryMainstreamAmericanCultureDomain<
        ContemporaryMainstreamAmericanCultureRegion>,
        ContemporaryMainstreamAmericanCultureSolution>
    study = new ContemporaryMainstreamAmericanCultureStudy<
        ContemporaryMainstreamAmericanCultureContext,
        ContemporaryMainstreamAmericanCultureRegion,
        ContemporaryMainstreamAmericanCultureDomain<
            ContemporaryMainstreamAmericanCultureRegion>,
        ContemporaryMainstreamAmericanCultureSolution>() ;


Study中包含的所有类(尽管有所不同)都具有相同的关注类型,因此必须有一种方法可以减少Study发布的类型数量,从而减轻负担。

有人可以帮忙吗?
谢谢

最佳答案

在这种情况下,实际上是否需要泛型?例如,是否可以仅将构造函数中的一组对象归入ContextRegionDomainSolution中?如果可能的话,那绝对是正确的方法。

如果那不可能,您可以通过创建一个中间的Culture类,至少隐藏一些沉重的负担:

class Culture<
    C extends Context,
    R extends Region,
    D extends Domain<R>,
    S extends Solution> {
  ...
}


您需要一个混乱的声明,例如ContemporaryAmericanCulture,但是您的Study实例化可能是Study<ContemporaryAmericanCulture> study = new Study<ContemporaryAmericanCulture>()

09-27 00:26