SOLID Principles for Object Oriented Design

SOLID principles will help us designing our project as below by reducing the tightly coupled code.

  • highly reliable
  • easy to extend and maintain
  • code reusability
  • flexibility
  • stable

Tightly coupled code means the classes are highly dependent on one another. So we can reduce this tight coupling among the classes or components using SOLID Principles and build loosely coupled classes.

SOLID stands for five different Object Oriented Design principles as follows

S – Single Responsibility principle 

O – Open Closed principle 

L – Liskov substitution principle 

I – Interface segregation principle 

D – Dependency Inversion principle 

Single Responsibility Principle

SRP states that a class should have only one responsibility or one job or one purpose. So it is saying, a class should have only one job to do not more than one.

Ex: A class should have employee related functionality and should not have email sending and logging on every employee management operation. So we have to write only employee management related functionality in one class and email sending in another class and logging in another class.

Open/Closed Principle

PCP states that a class should be open for extension and closed for modification. So once the development of the feature or job is done then the class is closed for modifications and open for extension through inheritance and polymorphism.

Ex: A class is providing Login function to validate the username and password for now. So we will write authentication logic to validate the username and password. Later if we need to add captcha validation also, we should not implementing that in the same class file. We will be inheriting it and adding our captcha validation also to it.

Liskov Substitution Principle

Interface Segregation Principle

Dependency Inversion Principle

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s