java generics heaviness due to design choice
我有以下我想成为普通学生的学习课程:
1 2 3 4 5 | public abstract class Study< T1 extends Context, T2 extends Region, T3 extends Domain<T2>, T4 extends Solution> {... |
派生类的示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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> {... |
这种设计的结果是,类在主代码中的实例化变得很重,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 | ContemporaryMainstreamAmericanCultureStudy< ContemporaryMainstreamAmericanCultureContext, ContemporaryMainstreamAmericanCultureRegion, ContemporaryMainstreamAmericanCultureDomain< ContemporaryMainstreamAmericanCultureRegion>, ContemporaryMainstreamAmericanCultureSolution> study = new ContemporaryMainstreamAmericanCultureStudy< ContemporaryMainstreamAmericanCultureContext, ContemporaryMainstreamAmericanCultureRegion, ContemporaryMainstreamAmericanCultureDomain< ContemporaryMainstreamAmericanCultureRegion>, ContemporaryMainstreamAmericanCultureSolution>() ; |
有人能帮忙吗?谢谢
在这种情况下,仿制药真的是必要的吗?例如,是否可能只接受一组从
如果这是不可能的,那么您至少可以通过创建一个中间的
1 2 3 4 5 6 7 | class Culture< C extends Context, R extends Region, D extends Domain<R>, S extends Solution> { ... } |
你需要一个杂乱无章的声明,比如说,
在某种程度上,动态地做一些事情是值得的,比如在文件或数据库中存储下面这样的外部表:
1 2 3 |
然后,您可以为这些实体定义操作/数据,例如打印内容时要使用的字体、应该重定向的页面、允许多少学生参与学习等。
你的物品看起来像
类型安全性很好,但是如果您想要管理许多和/或不可预见的实体,那么您不应该以这样的方式编码,即每次添加/更改/删除实体时都需要触摸代码。