엔티티라는 루트 클래스가 있습니다. 또한 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>>();
처럼> 해당 서비스에를 그 클래스와 해당 서비스는 동일한 계층 구조에 속합니다. 따라서 캐스팅 후에도 클라이언트 코드를 다시 캐스팅해야합니다. 다음을 피하는 방법? 그리고 열쇠와 가치 사이에 엄격한 관계를 강요합니까?
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);
엔티티와 서비스간에 항상 1 : 1 매핑이 있습니까? 또는 특정 시점에 하나의 서비스를 사용하지만 나중에 다른 서비스를 사용할 필요가 있습니까? 즉, 언제지도를 업데이트해야합니까? –
http://stackoverflow.com/questions/11720065/how-to-work-with-a-generic-map-whose-key-and-values-belong-to-a-ident-hierarchy의 가능한 (거의) 복제본 ? rq = 1 –