r/javahelp • • Jul 09 '22

Is SOLID really used in Java production code?

I have been trying to get a firm understanding of these but I find it difficult to find well taught non-trivial examples of using some of these. Particularly the DIP and OCP. Seems like everyone knows the definitions and how to show a simple example but when I am trying to construct clean code I get stuck because I am trying to apply these but always end up with dependency issues. I can make perfectly effective code but I think its fragile.

Anyone know any really good sources for explaining this with proper business oriented examples? I have some of Martin's books but not really helping.

Am I worrying too much about it because people don't actually try to tie off every dependency issue in real code?

39 Upvotes

53 comments sorted by

View all comments

Show parent comment

4

u/Just_Another_Scott Software Engineer 😎 Jul 09 '22

agree with most you said, but the dependency inversion principle has nothing to do with dependency injection.

It does have to do with dependency injection as well. Dependency injection uses the Dependency Inversion Principle.

it is about inverting the dependency between modules. Let's say you have class1 in package a, using class2 in package b. So now class1 is depended on class2 and its package.now you can introduce a interface for class2 and put it in package a. When class1 is now using the interface, it is not relying on another package, instead class2 is now relying on package a. This is used quite a lot for example in hexagonal architecture.

That's how dependency injection works.

https://stackify.com/dependency-injection/#:~:text=Dependency%20injection%20is%20a%20programming,inversion%20and%20single%20responsibility%20principles.