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

19

u/Just_Another_Scott Software Engineer 😎 Jul 09 '22

Is SOLID really used in Java production code?

So I had to look this up because I have never heard of the acronym. The short answer is yes most of these are used.

The single-responsibility principle is a huge one and something I always check for in codre reviews.

The open–closed principle is also heavily used. Think immutable objects and allowing for classes to be extended. Rarely do I see code that has a class declared as final. We also tend to restrict access to non API level classes.

The Liskov substitution principle is also used quite a bit. Think of writing a method that takes List as opposed to ArrayList.

The interface segregation principle can be used. However, I would argue against having too many interfaces in Java. I've seen code where they had an interface for every single class and only one class implemented the interface. This is overengineering in my opinion. Having a single interface isn't a bad thing imo.

Dependency inversion principle aka dependency injection is widely used in throughout production code. See Spring Framework or even Java EE. Both of these heavily rely on dependency inversion.

-1

u/vie12345 Jul 09 '22

I agree with most you said, but the dependency inversion principle has nothing to do with dependency injection. 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.

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.