1. Overview
S.O.L.I.D is the acronym for the first five foundamental principles of the OOD (Object Oriented Programming). For a developer that writes in OOD paradigm like Java, it is important understand very well these principles to write a better code, make it more readable, testable and mantainable.
Before start let's see some useful definitions:
- Coupling: It's the level which one module or a class is directly releated to another. When a class depends by another class it means you have to use both classes even if you want to use only one. It's possible reduce the coupling by using interfaces and abstract classes that define the points of communication between the various parts of the system.
- Cohesion: It measures the relationship of two or more parts of a system and how they work together to produce a most valuable product. Classes that do many things have too responsibility and a lower cohesion with the outside parts.
- Information Hiding: It is a way to hide information but not only: Information Hiding allows the developers to use a class or module knowing only its interface. A developer doesn't know and it must not know the implementative aspect of that class or module.
2. Single responsibility principle
The first principle says that an object should be focused on a single responsibility and it must have a single reason for change itself. Have small and coincise objects help us to avoid to have big classes with a low level of cohesion. Following this principle we'll have classes easy testable, readable, mantainable and reusable.
3. Open-Close principle
Open to change, but Closed to modify, every object should be able to add new features without change his internal behavior: It is always possible to add new methods to an object, but at the same time it must not be necessary to re-compile the entire application.
The goal of this principle is avoid introducion of bugs in the code already implemented and tested. We can apply Open-Close principle using abstractions, separating the object interface from the his implementation, the possible changes of the implementation don't impact the clients that use the inferface as it remains unchanged. The possible new features are translated in an extension of the interface and the relative implementation, which is transparent for the clients that uses the old methods.
4. Liskov substitution principle
That principle says that should be always possible to use any child classes instead of the parent class without changing the code. If object A is the parent of object B and function(A) works, then also function(B) works without that function knows the implementative details on class B. This principle grants that a derivate class can't affect the behavior of the parente class.
5. Interface segregation principle
The Interface segregation principle claims that a client should not depend by the methods that it not use, for this reason the interfaces should be a lot, small and specific. This allows the to the client to depend my a minimum set of methods, ie only those belonging to the interfaces which it actually uses.
6. Dependency Inversion principle
The last principle, but not for importance is the Dependency Inversion. That principle help to uncouple the code in such a way that the classes depend by abstractions instead of concrete implementations.
Dependency Inversion and IoC (Inversion of Control) are two concepts at the base of Spring Boot, infact Spring Boot moves the responsibility of the object lifecycle (as the creation, dependecy settings...) from the application (as developer) to the framework, read here for more about IoC. Thanks to IoC and Dependency Injection, in Spring Boot the consumption of classes happens through interfaces and abstract classes, is the Dependency Injection that provides the specific implementation of the class that uses it.
The advantages of the use of Dependency Inversion are a lower coupling and more flexibility as it is possible to change an implementation in a transparent way for the clients.
7. Conclusion
In this article, we have explained five fundamental principles of the Object Oriented Programming that will help you to have a better code.