I am new to Java and I had the following problem:
I created several classes that all implement the Parser interface. I have JavaParser, PythonParser, CParser and finally TextParser.
I am trying to write a method, so it will take either a file or a string (representing the file name) and will return the appropriate parser based on the file extension.
Here is the psuedo code of what I'm basically trying to do:
public Parser getParser(String filename) { String extension = filename.substring(filename.lastIndexOf(".")); switch(extension) { case "py": return new PythonParser(); case "java": return new JavaParser(); case "c": return new CParser(); default: return new TextParser(); } }
In general, is this the right way to handle this situation? Also, how should I handle the fact that Java doesn't allow strings to be included? Should I use the .hashcode () value of strings?
I feel like there is some kind of design template or something for this, but it eludes me. So will you do it?
java
Siracuse
source share