0
Answered

Remove limitation of abstract factory Design pattern

Indiver Singhal 7 months ago • updated by Alexander Shvets 7 months ago 1

Dear Colleagues,

I have a question. Suppose I am going to create File handler that supports multiple file handling mechanism. So we would code in below way but still if i have to introduce new file handling mechanism in future I have to change my factory class and there would be more if and else statement. so i have 1000 types of different file handler. we have to use if and else thousands thousands times or with the help of switch case. It is breaking single responsibility principle. So how would you achieve this. One idea that came up in my mind. I would fetch types of file and its handler class name from database table and would create object with reflection. But what if i don't have to use database.




Answer

Answer
Answered

Hi Indiver!


Nice question! If I were you, I'd implement something similar to your "file-type -> class-name" association idea, but instead of keeping it in database, I'd load it from a config file and probably cached it somewhere during the app initialization.

Answer
Answered

Hi Indiver!


Nice question! If I were you, I'd implement something similar to your "file-type -> class-name" association idea, but instead of keeping it in database, I'd load it from a config file and probably cached it somewhere during the app initialization.