我创建了一个示例来介绍我的问题。
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;
}
}
所以,我想知道我是否应该定义我的接口来获取Files,或者我自己的文件阅读器,以确保数据的格式有效。显然,我希望能够将数据本身作为Data对象,但我在这方面受到限制。原因与数据非常大有关,我不得不为多个对象序列化它。在这种情况下,发送数据的路径而不是数据本身更实用。
无论如何,关于这个问题,我倾向于采用 Java文件实例的方法,因为它看起来更通用,但我想听听你的建议。谢谢!