I'm trying to do a factory with devices depending on a fileName
with the appropriate device corresponding to it.
My first intuitive approach to avoid creating instance was to define static methods to validate the fileName into its different classes, let's say MyDevice1.validFilePath(fileName) return new MyDevice()
but it seems that its not possible to force the implementation of a static method from the Device
ancestor.
So I went for polymorphism, and surprise, it seems that compiler complains because he cannot convert MyDevice
to Device<Number>
. What should I do? Polymorphism applies at compilation because MyDevice
is a valid class, but MyDevice
cannot be a Device<Number>
?
public abstract class device<T extends Number>{
protected List<T> samples;
public abstract boolean validFileName(String fileName);
}
public class MyDevice extends device<Double>{
public boolean validFileName(String fileName){
return fileName.contains("MyDevice");
}
}
public class DeviceFactory{
public static Device<Number> getDevice(String fileName){
Device<Number> res;
MyDevice md = new MyDevice();
if (md.validFileName(fileName)){
res = md;// invalid??!!!
}else{
throw new UnsupportedOperationException();
}
return res;
}
}
It seems that changing the Device<Number>
with Device
passes at compilation, but complaining about Device is a raw type. References to generic type Device<T> should be parameterized
. Should I just ignore this error in that case? What is the way to go?