samedi 21 mars 2015

Handle conversion from multiple file formats avoiding coupling

I'm working on a Java (RCP) project in which I need to convert different source file formats (let's call them SF-1..N) to another two different destination formats (DF-A, DF-B) Right now the input files can be CSVs, XLSXs and XMLs (with different schemas). The application should find the right converter and it's necessary to read the file content to do that. Each converter normalizes the content of the file and creates 1..N instances of an DF objects that get converted to either DF-A or DF-B record.


So I have a few converters that get the file content and return a collection of DFs objects. Each converter should also be able to tell if the file content is the one it supports.


The only strategy I can think of is asking each converter if it supports the content of the file and if it doesn't try the next one. If there no converter suitable, return an error to the user.


It's also likely I'll be asked to add support from/to new formats and I'd like to be able to add more converters without modifying unnecessary code.


I'm thinking about registering each converter in a service-locator object but I'm not sure how to do that with minimal coupling. I thought about registering to the service locator in the static initializer but that gets called only after loading the class.


How can I do that? Is there a better approach for what I want to accomplish?


Aucun commentaire:

Enregistrer un commentaire