SOLID Principles in Java
Solid is a popular design principle in Object-oriented based coding in the software development world. Following the solid principle in our programming makes our code very clean and understandable, and flexible for changes and modification.
SOLID is an acronym for five principles. In this article, let's understand one by one.
- Single responsibility principle(SRP) ->
- Every class must perform a single functionality so that, every class should have one and only one responsible. Each attribute and method of a class should work toward one responsibility.
- in order to implement the SRP, We can create many classes for each functionality instead of putting all the functionality in a single class.
2. Open-closed principle(OCP) ->
- Open for extension closed for modification.
- a class must be able to add new requirements and should not be able to modify the original code rather than bugs.
- To achieve the OCP principle in our code, we can use the abstract classes or interface in possible places instead of using concrete classes.
3. Liskov substitution principle (LSP)->
- superclass can be able to replace by the subclass without any barriers.
- The designed superclass and subclass should have a pure IS-A relationship, otherwise, your design would violate the LSP.
4. Interface segregation principle(ISP) ->
- A class should not be forced to implement methods that are not relevant to the class.
- In this case, we can design multiple interfaces rather than create a single interface and define all the methods and variables in it.
5. Dependency intervention principle(DIP) ->
- High-level modules should not depend on low-level modules, both should depend on abstraction.
- We should design our code at aggregation access(HAS-A), the class should depend on an abstract class or interface instead of the concrete class.