SOLID Principles

Isuri Punchihewa
4 min readApr 13, 2021

What are SOLID principles??

SOLID principles are the design principles that are first conceptualized by Robert C. M and which let us to manage most of the software design problems. It encourage us to create software more maintainable, understandable and more flexible.

Why using SOLID Principles?

  1. Reduce the dependencies and makes it easier to change the code without affecting the other code blocks whenever there is a block in the code.
  2. Makes design easier and more understandable.
  3. The systems become more maintainable, testable, scalable and reusable.
  4. Avoids the bad design of the software.

Five concepts together make up the SOLID principle and they are,

  1. Single Responsibility Principle
  2. Open/Closed Principle
  3. Liskov Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion Principle

Let’s talk about these five concepts separately.

1. Single Responsibility Principle

“A class should have one, and only one reason to change.”

Single Responsibility principle states that a class should have only one responsibility, which means that a class should have only one reason to change.

Implementing many functions inside a single class could mashup the code and performing any modifications to the code later on may affect the whole code. But by using the Single Responsibility principle we can overcome these problems.

Why using Single Principles ?

  1. Testing : When a class have only one responsibility, it has fewer test cases.
  2. Lower Coupling : Having less functionalities in a single class will have few dependencies.
  3. Organization : Well organized classes makes it easier to search than monolithic ones.

2. Open / Closed Principle

“Software entities such as classes, modules should be open for extensions but closed for modifications.”

Any new functionalities should be implemented by adding classes, attributes and methods instead of changing the current or existing ones. Once a class is developed it shouldn’t be modified except to correct bugs.

By following this principle we can stop modifying the existing code and causing potential new bugs.

Why using Open/Closed Principle?

  1. Reduce introducing new bugs to the existing code.
  2. Prevent from changing the source code time to time after it is being written, tested and debugged.
  3. Increase flexibility.

3. Liskov Substitution Principle

“Objects in a program should be replaceable with instance of their subtypes without altering the correctness of that program.”

Liskov Substitution Principle is introduces by Barbara Liskov. It applies to inheritance in a way that the derived classes must be completely substitutable for their base classes. In other words, if class X is a subtype of class Y, we should be able to replace Y with X without interrupting the behavior of the program.

If this principle is violated , it will work fine during development but will bow up in production.

Why using Liskov Substitution Principle?

  1. Makes the code cleaner.
  2. Reduce the complexity.
  3. Easier to debug.

4. Interface Segregation Principle

“Many client-specific interfaces are better than one general-purpose interface.”

We should not enforce clients to implement interfaces that they don’t use. Instead of creating a one big interface, we can break down it to smaller interfaces. By doing this, a class only need to be concerned about the methods that are of interest to them.

This principle is first used and formulated by Robert C. Martin.

Why using Interface Segregation Principle?

  1. Smaller interfaces are easier to implement.
  2. Reduce coupling.
  3. Increase Flexibility.
  4. Increase reusability.
  5. Minimize dependencies on unused members.

5. Dependency Inversion Principle

“High-Level modules should not depend on Low-Level modules. Both should depend on abstractions.”

“Abstraction should not depend on details. Details should depend on abstraction.”

Dependency Inversion Principle states that we must use abstraction (abstract classes and interfaces) instead of concrete implementations. This principle couples the software.

Why using Dependency Inversion Principle?

  1. Design easier to change.
  2. Low coupling.

That’s simply about the SOLID principles. I hope you all enjoy reading this blog and hope you find this useful. Thank You for reading and Have a nice day!

--

--