I’m writing an Windows Forms application that reads a file, processes it and then commits the changes.
Depending on some circumstances, when i click on “Process button” from the form i want it to interact with a specific object, for example depending on the extension of the file, the file gets processed by a specific class that handles that type of file.
In terms of application architecture, what’s the best way to do this?
So for example a .txt file is processed by a TextParser, a .csv file is processed by a CSVParser, a .doc file is processed by a MSWordParser, everything else is processed by a GenericParser, etc.?
Well, the first thing would be to create an interface, i.e. IParser so that every class has a .Parse(file) method.
Next is the task of matching up the file extension strings with classes. The first thing that comes to mind for me is a Dictionary. The key would be the file extension as a string and the value would be a parser for that type of file extension. Before doing any processing you would populate the dictionary with all of the file extensions you wish to support.
Some assumptions for this to work well:
-You don’t have a very large number of supported file extensions.
-One instance of the parser can be used repeatedly
-new-ing a parser isn’t particularly expensive; there is minimal initialization beyond what the language needs to do to new an object.