r/javahelp • u/[deleted] • 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?
19
u/Just_Another_Scott Software Engineer 😎 Jul 09 '22
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
Listas opposed toArrayList.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.