S.O.L.I.D The first 5 principles of Object Oriented Design.

Jayamini samaratunga
3 min readFeb 21, 2021

SOLID principles were defined in the early 2000s by Robert C. Martin (also known as Uncle Bob). These five design principles are more like guidelines to be followed while designing software. Adopting these practices can also contribute to avoiding code smells, code refactoring and Agile or Adaptive software development.

S.O.L.I.D stands for

  • Single Responsibility Principle
  • Open / Close Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle

Let’s find out how they will help us develop quality software.

Single Responsibility Principle

“A class should have one and only one reason to change, meaning that a class should have only one job”

This reduces coupling and make the code easier to understand. Otherwise, if our class has more than one responsibility we will have a high coupling causing our code to be fragile with any changes.

Violation of Single Responsibility Principle:

‣ we have a Customer class with more than one responsibility

As implemented above Customer class owns a set of attributes. Also holds responsibility to redeem reward points and calculating membership fee.

when a class has multiple responsibilities it is more difficult to understand, extend and modify.

Solution:

Creating different classes for each responsibility.

‣ Customer class:

‣RedeemRewardPoints class:

‣CalcmembershipFee class:

With this solution we have three classes where each class has a single responsibility.

Open / Closed Principle

“Objects or entities should be open for extension but closed for modification “

This means that the class should be extendable without modifying the class itself.

We can use the abstraction and polymorphism to help us apply this principle. When there is a high responsibility to change always depend on abstraction

Benefits:

  • Code maintainable and reusable.
  • Code more robust.

Liskov Substitution Principle

“Every subclass or derived class should be substitutable for their base or parent class”

This principle was defined by Babara Liskov and says that objects must be replaceable by instances of their subtypes without altering the correctness of that program. The violation of this principle is considered a violation the open-closed principle too by Robert C. Martin. This extends the behavior of the open-closed principle by focusing on super and subclasses.

Benefits:

  • Code more reusable.
  • Class hierarchies easy to understand.

Interface Segregation Principle

“A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.”

In a situation where you have shape interface with print() and draw() method when the client only needs the shape to be printed, these interfaces are called fat interfaces. By following this principle you can avoid bloated interfaces. This interfaces will be defined by clients as the client knows about what is related to him. If you write methods that are not needed by the client, after implementing them client will have to interact with them. So this principle explains that client should not be forced to use unwanted interfaces.

Benefits:

  • Decoupled system.
  • Code being easy to refactor.

Dependency Inversion Principle

“Entities must depend on abstraction not on concretions. it states that the higher level module must not depend on the low level module, but they should depend on abstraction.”

Dependency Inversion principle means that one class should not directly depend on another class, but on an abstraction(interface)of that class.

when applying this principle we will be reducing dependency on specific implementations and it will make our code more reusable.

Ex: The three tire architecture.

Conclusion

In this article I have further explained the five principles of SOLID which was the first concept we learnt during our first lecture as software engineering specialization students. We have studied this under Application frameworks module presented by lecturer Mr. Kushira Godellawatte along with lecturer Mr. Thusithanjana.

If you’ve got any other examples of SOLID principles I would like to hear about them.

Thank you.

See you soon…

--

--