lundi 10 août 2015

Extend an interface or aggregate interfaces

I'm in a dilemma. I have an interface Parser and classes that implement it will parse concrete file formats. For example - CSVParser will parse CSV files, XMLParser will parse XML files and so on.

So, interface Parser would be:

public interface Parser{

    public SomeObject parseFile(String pathToFile);

}

On the other hand, some parsers will have additional parameter, for example, an array that will tell them which lines to skip in a file. Now this confuses me, should I extend Parser and add the method, something like:

public interface BetterParser extends Parser{
    public SomeObject parseFileConsideringParameter(String pathToFile, int[] whichLinesToSkip)
} 

Or should I aggregate them so that my class needs to implement both:

public class concreteParser implements Parser, BetterParser{
}

I would like to have parser agnostic part in pipline, where you would say:

Parser parser = ParserFactory.giveMeParser(type);
SomeObject so = parser.parseFile(path);

The thing is, in some case I won't have the parameter that tells me which lines to skip.

I would really like to avoid bounding to concrete implementation, but how do I overcome this problem? Maybe I'm missing something?

Aucun commentaire:

Enregistrer un commentaire