Hello. I have created an example to introduce my question.
public class Algorithm
{
// This is the best, but circumstances prevent me from doing this.
/*public static void computeSomething(Data data)
{
// Compute some stuff
}*/
public static void computeSomething(DataFileReader reader) throws IOException
{
// Compute some stuff.
}
public static void computeSomething(File file) throws IOException, DataFormatException
{
DataFileReader = DataFileReaderFactory.newDataFileReader(file);
// Compute some stuff.
}
}
public class DataFileReaderFactory
{
private enum FileExtension { XML, UNSUPPORTED_EXTENSION }
private static final String XMLExtension = ".xml";
public static DataFileReader newDataFileReader(File file) throws DataFormatException
{
switch(computeFileExtension(file))
{
case XML : return new XMLFileReader(file);
default : throw new DataFormatException();
}
}
private static FileExtension computeFileExtension(File file)
{
if(file.getName().endsWith(XMLExtension))
return FileExtension.XML;
else
return FileExtension.UNSUPPORTED_EXTENSION;
}
}
So, I would like to know if I should define my interface to take Files, or my own file readers, which ensure that the data is in a valid format. Obviously, I would like to be able to take the data itself as a Data object, but I am limited in this regard. The reason has to do with the data being very large and me having to serialize it for multiple objects. In this case, it is more practical to send a path to the data rather than the data itself.
Anyhow, in regards to the question, I am leaning toward the method which takes an instance of Java's File, as it seems more general, but I want hear your advice. Thanks!