I am not sure where this pattern comes from but it is pervasive. The benefits have always seemed dubious to me. If you are writing a framework, sure go nuts, but if you are writing an application that wants to test and verify everything works you don't want implementations swapping in and out.
As far as I can tell, this comes from the "reusability is everything" hype.
Every component has to be written in a way that abstracts away any coupling to anything, in case you might want to reuse that class. And in the end you write 300 lines of code to maybe save 5 lines at some point. To me this looks like the typical take one principle and overshoot it's application to the max.
The first is the mantra "code to an interface" that gets repeated blindly while misunderstanding what the "interface" in question is. People see the word "interface" in that statement and automatically assume it means the same as "interface" in Java. It does not.
The second is that old versions of DI frameworks were able to work their DI magic against interfaces but not implementations. Those days are over, now that Spring and CDI can deal with implementations without extra work.
5
u/deadron Apr 21 '21
I am not sure where this pattern comes from but it is pervasive. The benefits have always seemed dubious to me. If you are writing a framework, sure go nuts, but if you are writing an application that wants to test and verify everything works you don't want implementations swapping in and out.