I am writing a program to read data from a file, which can be in one of several formats (in fact, these are different versions of the same format), and I use reflection to call the corresponding function for each format. Assuming the file format is the number indicated in the first byte of the file:
Class DataFile extends Model { ... Blob file ... public void parse() throws Exception{ InputStream is = file.get(); Class c = Class.forName("models.DataFile"); Method m = c.getMethod("parse_v"+is.read(), (Class []) null); m.invoke(this, (Object []) null); } public void parse_v0() throws Exception{ ... } public void parse_v1() throws Exception{ ... } }
My question is: do I abuse / abuse thoughts? I have the feeling that I have to use inheritance and create another class for each file type with its own “parsing” procedure, but I don’t know the file type until I start parsing ... and then I can’t “lower” it , and just use something like ((DataFile_v1) this).parse() , so I lost a bit.
Thank you for your time!
source share