문제

What is a rule of thumb to define a generic factory? What is difference in implementation and usage?

Option 1:

public interface ScheduleFactory<V, T extends Period<V>> {
     Schedule<V,T> fromConfigurationString(String configurationString);
}

Option 2:

public interface ScheduleFactory {
     <V, T extends Period<V>> Schedule<V,T> fromConfigurationString(String configurationString);
}
도움이 되었습니까?

해결책

In the first way, you create a ScheduleFactory with a specific V and T, and thereafter that factory object can only return Schedule<V, T>.

In the second way, you create a ScheduleFactory object, and that single factory object can then create Schedule<V, T> for different Vs and Ts each call.

다른 팁

The main difference is Type inference

Generic method : it helps the java compiler to look at the generic method invocation to determine which type argument that make the invocation are applicable.

Generic class : the constraint of type affects to whole class/interface and each method you declared.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top