DEV Community

Discussion on: An IoC pattern to avoid repetitive if statements

Collapse
 
tomazfernandes profile image
Tomaz Lemos

Hi Trifon!

Are you suggesting to put the condition directly in the handleData method? Then how would the factory know what instance to return? I do like the Tell-don’t-ask principle, but didn’t get how you’d apply it here.

We might put a if (!shouldHandle(url)) throw ... in the beginning of the handleData method to offer some protection against fools.

As I said I’ve borrowed this pattern from Spring, in an earlier version I had put a piece of their code, perhaps I’ll put it again to illustrate how they use it.

Thanks a lot for your feedback!