I am storing and parsing xml files, those files evolve over time so when stored I also store the version of the format.
When I load an xml I get the version of the file and based on this version I use a specific java class to process it. The xml format is changing every few weeks so a new class is being created and added to the list of classes. I can load any version of an xml any time (e.g. today I can load xml files created two years ago with version v02 while currently is at version v26)
Is there a design pattern for this kind of problem ? Is there a design pattern using dependency injection for this kind of problem ?
Currently I have a java interface defining a
DocumentParser and I have several DocumentParsers each in different package (e.g.
com.parsers.v1.DocumentParserImpl) and I have a provider class
DocumentParserProvider to supply the proper parser using the version (e.g.
public DocumentParser get(String version); ). The interface api never changes, that is we always ask the same thing from our interface.
Is there a better way to do it or is there a name for this problem?
It sounds like a strategy pattern as you want to use different implementations of a
Loader class to load different XML files.
You can instantiate multiple
Loader instances with application startup one-by-one (or all at once reflectively) and store them in a factory. Then you can ask the factory to give you a
Loader based on the XML version.
If you want to use DI, you can have a
Map<String, Loader> object that simply maps the version of XML with its loader.