Entity というルート クラスがあります。エンティティのサブタイプに対して CRUD 操作を行うサービス クラスもあります。したがって、私のサービスクラス定義はこのパターンに従います
class Entity {
//the root class
}
class Service<T extends Entity> {
//root service class containing common CRUD operations
}
class SomeEntity extends Entity {
}
class SomeService extends Service<SomeEntity> {
}
私はエンティティクラスのマップを維持します->のような対応するサービスへ
Map<Class<? extends Entity>, Service<? extends Entity>> = serviceMap
new HashMap<Class<? extends Entity>, Service<? extends Entity>>();
ただし、上記は、クラスとそれに対応する Service が同じ階層に属していることを強制するものではありません。したがって、キャストした後でも、クライアント コードは再度キャストする必要があります。以下を回避するには?そして、キーと値の間に厳密な関係を強制しますか?
public <T extends Entity> Service<T> get(Class<T> clazz) {
return (Service<T>) serviceMap.get(clazz);
}
//example of client code
SomeService service = (SomeService) serviceRegistry.get(SomeEntity.class);