这是java中泛型类型安全的好做法吗?

h9a6wy2h  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(411)

这是我第一次尝试使用泛型,幸运的是,它对我很有用,但我不知道这是否真的应该是这样(下面),或者是否有其他更优雅的方法来解决这个嵌套/抽象。

如果是这样的话,你能给我一个提示/链接,或者如何在不破坏结构的情况下缓解这个问题?

这只是原始结构的简化,但它类似于嵌套在一个图中。原始代码是关于物理/力学的(我很抱歉在这个例子中缺少无意义的东西)。

我的工作对象是:

休眠->实体
java graphql->api/dto序列化
mapstruct->entities<-双向,带忽略等->dto
lombok->lombok.experimental足够好了
Spring

我的抽象类看起来像:

@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class City<HM extends Humanoides<LB>, AN extends Animal<LB>, LB extends Limbs> {
    SortedSet<HM> humans;
    SortedSet<AN> animals;
    SortedSet<LB> limbs;
}

@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class Mammal<LB extends Limbs> {
    private SortedSet<LB> extremities;
}

@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class Humanoides<LB extends Limbs> extends Mammal<LB> implements Comparable<Humanoides<LB>> {
    private int iq;

    //compareTo(Humanoides<LB> h)
}

@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class Animal<LB extends Limbs> extends Mamal<LB> implements Comparable<Animal<LB>> {
    private int speed;

    //compareTo(Animal<LB> a)
}

@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class Limbs implements Comparable<Limbs> {
    private int length;

    //compareTo(Limbs l)
}

我的实体是:

@Getter @Setter @NoArgsConstructor @SuperBuilder @IdClass(CityStuff.class)
public class CityEntity extends City<HumanoideEntity, AnimalEntity, LimbsEntity> {}

@Getter @Setter @NoArgsConstructor @SuperBuilder @IdClass(HumanoideStuff.class)
public class HumanoideEntity extends Humanoide<LimbsEntity> {}

@Getter @Setter @NoArgsConstructor @SuperBuilder @IdClass(AnimalStuff.class)
public class AnimalEntity extends Animal<LimbsEntity> {}

@Getter @Setter @NoArgsConstructor @SuperBuilder @IdClass(LimbsStuff.class)
public class LimbsEntity extends Limbs{}

我的dto是:

@Getter @Setter @NoArgsConstructor @SuperBuilder
public class CityDto extends City<HumanoideEntity, AnimalEntity, LimbsEntity> {
    //manipulate()
}

@Getter @Setter @NoArgsConstructor @SuperBuilder
public class HumanoideDto extends Humanoide<LimbsEntity> {
    //manipulate()
}

@Getter @Setter @NoArgsConstructor @SuperBuilder
public class AnimalDto extends Animal<LimbsEntity> {
    //manipulate()
}

@Getter @Setter @NoArgsConstructor @SuperBuilder
public class LimbsDto extends Limbs {
    //manipulate()
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题