Factory or something else?

 
50   50
Jun 10, 2010
 
I'm trying to think of a solution but not sure which way to go. Here's the problem:

Its going to be a library for parsing multiple XML files. So I know what classes to make and how they are going to be nested. A year or two from now, the software that emits the XML file may change the syntax of the XML file. And at that point of time, my solution has to handle two different set of XML files based on the version written into the main XML file. What I would like my solution to do is,
a.) Check the version of the software emitting XML files and call the appropriate Parse routine, say Version1.Parse() or Version2.Parse(), that then calls the parse routine of different files, like ParseFileA() or ParseFileB().

b.) There is a possibility that of the 20 XML files, syntax for only 5 of them has changed (ver 1.1) and rest remain the same. In that case, I would like to call the different file parsing routines. I guess, I should be able to call the appropriate method based on the version read in the previous step through a conditional branching (just wanted to let know).

From the look of it, Abstract factory seems to be the pattern to go ahead with. But I also feel that I should be defining the parse methods through an Interface rather than through an abstract class.

Am I right? What approach would you have taken?
Thanks. 



70   95.9
Jun 11, 2010
After reading the description above, I understood there are different algorithm for reading different XML files.I think you should investigate more on strategy pattern.I think this fits to your requirement.

see this link
http://dofactory.com/Patterns/PatternStrategy.aspx

Before coming to conclusion, I would suggest you to read some other post as well on other sites.Take careful decision.

250   96.9
Jun 23, 2010
As per the problem statement and the points:

a.) Check the version of the software emitting XML files and call the appropriate Parse routine, say Version1.Parse() or Version2.Parse(), that then calls the parse routine of different files, like ParseFileA() or ParseFileB().

b.) There is a possibility that of the 20 XML files, syntax for only 5 of them has changed (ver 1.1) and rest remain the same. In that case, I would like to call the different file parsing routines. I guess, I should be able to call the appropriate method based on the version read in the previous step through a conditional branching (just wanted to let know).

I believe the solution should address the futuer versions also, cause sometime we can't even predict the extension points of a solution, so its better to keep it open.

I believe using Abstract Factory will be the most appropriate solution. Regarding the decision of using interface and Abstract class I always use interface whenever I want to make a behavioral contract and when there is a need for code re usability as well as creating a behavioral contract better to use Abstract Classes.

50   50
Jun 24, 2010
Thank you @Bhupendra & @Amlan for your time and suggestions.

The strategy pattern suggestion was a good one that I hadn't looked into. I figured for my situation I'll need a combination of Abstract Factory (where whole semantics of the files change) and Strategy Pattern (when there are minor additions/subtractions to the file).

In this particular scenario I think Interface is preferred, as I want to just lay out the contracts.