I want to write a class for the following scenario: I have hundreds of files with different extensions(ex: pdf,txt,csv,xml etc) I want to read the file and parse it based on its extension(type) & put it in a DB.
But a general If-Else / switch approach will be not the optimum solution.
Can any one help me if there is a pattern for this sort of thing.. * I have tried putting the extension & action in a file as pairs and then accessing them
I dont think you are getting the problem I'm facing.
I have 500 (for example) files all with different extensions. Now to associate with an action corresponding to the File Parsing if-else or switch is not a good solutions as i will have to generate/write as many if blocks. So is the case of a filter I believe?
I have tried putting the extension & action in a file as pairs and then accessing them
Bang on. This is a good approach, provided you read the file once at the application start and put it in memory after that. I also hope that you have an interface that defines the contract for processing a kind of extension. If you are doing the above, you are on track and in some way following the Factory method design pattern