Question

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);
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top