Is it possible in Java to create a static factory method/class that uses an interface as the parameterized type and return an implementing class of this given interface?
Although my knowledge of Generics is limited, here is what I want to do:
// define a base interface:
public interface Tool {
// nothing here, just the interface.
}
// define a parser tool:
public interface Parser extends Tool {
public ParseObject parse(InputStream is);
}
// define a converter tool:
public interface Converter extends Tool {
public ConvertObject convert(InputStream is, OutputStream os);
}
// define a factory class
public class ToolFactory {
public static <? extends Tool> getInstance(<? extends Tool> tool) {
// what I want this method to return is:
// - ParserImpl class, or
// - ConverterImpl class
// according to the specified interface.
if (tool instanceof Parser) {
return new ParserImpl();
}
if (tool instanceof Converter) {
return new ConverterImpl();
}
}
}
I want to restrict the client code to only insert an interface 'type' into the getInstance() method that extends from the Tool interface I specified. This way I know for sure that the tooltype that is inserted is a legitimate tool.
Client code should look like this:
public class App {
public void main(String[] args) {
Parser parser = null;
Converter converter = null;
// ask for a parser implementation (without knowing the implementing class)
parser = ToolFactory.getInstance(parser);
// ask for a converter implementation
converter = ToolFactory.getInstance(converter);
parser.parse(...);
converter.convert(... , ...);
}
}
The factory should switch on the type of the interface (careless if it's null or not), defined before it is asked from the factory. I know this is not going to work the way I wrote this, but I hope one of the readers knows what I want to accomplish.
The return type of the getInstance method is the same as the incoming parameter, so when a Parser interface is passed, it also returns a Parser p = new ParserImpl(); return p;
Thanks in advance for helping me.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…