SOLID is a famous design pattern for writing classes in OOPS
S: Single Responsibility Principle
O: Open Closed Principle
L: Liskov's Substitution Principle
I: Interface Segregation Principle
D: Dependency Inversion Principle
Single Responsibility Principle (SRP)
- Class should have one and only one responsibility
- Write a Class to achieve one goal
- SRP helps to provide high maintainability and better visibility to control across application module
Open Closed Principle (OCP)
- Software components should be open for extension, but closed for modification.
- Our classes should not contain constraints that will require other developers to modify our classes in order to accomplish their job – only by extend our classes to accomplish their job.
- It helps to achieve software extensibility in a versatile, intuitive, and non-harmful way.
Liskov's Substitution Principle (LSP)
-Derived types must be completely substitutable for their base types.
- Objects of subclasses must behave in the same way as the objects of super classes.
- This principle useful for runtime-type identification followed by the cast.
Interface Segregation Principle (ISP)
-Clients should not be forced to implement unnecessary methods that they will not use.
-splits an interface into two or more interfaces until clients are not forced to implement methods that they will not use.
This principle stands for Clients should not be forced to implement unnecessary methods that they will not use. In other words, we should split an interface into two or more interfaces until clients are not forced to implement methods that they will not use. For example, consider the Connection interface, which has three methods: connect(), socket(), and http(). A client may want to implement this interface only for connections via HTTP. Therefore, they don't need the socket() method. Most of the time, the client will leave this method empty, and this is a bad design. In order to avoid such situations, simply split the Connection interface into two interfaces; SocketConnection with the socket() method, and HttpConnection with the http() method. Both interfaces will extend the Connection interface that remains with the common method, connect()
Dependency Inversion Principle (DIP).
-Depend on abstractions, not on concretions.
- sustains the use of abstract layers to bind concrete modules together instead of having concrete modules that depend on other concrete modules.
- sustains the decoupling of concrete modules.
This principle stands for Depend on abstractions, not on concretions. This means that we should rely on abstract layers to bind concrete modules together instead of having concrete modules that depend on other concrete modules. To accomplish this, all concrete modules should expose abstractions only. This way, the concrete modules allow extension of the functionality or plug-in in another concrete module while retaining the decoupling of concrete modules. Commonly, high coupling occurs between high-level concrete modules and low-level concrete modules.
Ex: A database JDBC URL, PostgreSQLJdbcUrl, can be a low-level module, while a class that connects to the database may represent a high-level module, such as ConnectToDatabase#connect().