Вопрос

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