I'm currently using my factory like this:
public class AbstractFactory
{
public static AbstractHeader parseHeader(File file)
{
if(AFactory.canRead(file))return AFactory.parseHeader(file);
if(BFactory.canRead(file))return BFactory.parseHeader(file);
throw new UnsupportedOperationException("File ["+file+"] not supported");
}
public static AbstractContent parseContent(AbstractHeader h)
{
if(h instanceof AHeader){
return AFactory.parseContent((AHeader) h);
}
if(h instanceof BHeader){
return BFactory.parseContent((BHeader) h);
}
throw new UnsupportedOperationException("Header not supported");
}
}
the parseHeader() will return an instance of either AHeader or BHeader, and in a later time will ask for the AbstractContent. Is there a better way to do this ? Get away with the instanceof checks ?