What are 5 SOLID Principles?

Hello Friends,

Today, we will see what are SOLID principles in Software life cycle. This is one of the must interview question for a software programmer.

In this article, we will go through over the definition for each of the principle and short description only which is needed for a quick refresh while attending the interview.

We will go through and discuss each of the principle in detail along with examples in the up-coming articles. Basically each letter denotes a specific functionality in SDLC when developing the software.

  1. S: Single Responsibility Principle (SRP) : 
      • Your function or method should not be overloaded with multiple tasks. It should be designed to handle single functionality in a given work flow.

For Example: Saving the customer information in the database, to ensure data integrity and avoid junk data, we check for duplicate customers, valid email address before saving.

            1. Check for duplicates
            2. Validate email address
            3. Calling the data access layer method to save to database 
      • SaveCustomer() – If you perform all the above 3 actions in one method, then you are violating first principle.
      • ProcessCustomer() – Design your method / function to call the below 3 private methods / functions from ProcessCustomer() to perform various tasks by different methods.
                • ValidateCustomer()
                • ValidateEmailAddress()
                • SaveCustomer()
  1. O: Open closed Principle (OSP) : 
      • A software module/class is open for extension and closed for modification. “Open for extension” means we need to design our module/class in such a way that the new functionality can be added only when new requirements are generated. “Closed for modification” means we have already developed a class and it has gone through unit testing and deployed to production as well. We should then not modify that function / method until we find bugs. As it says, a class should be open for extensions, we can use inheritance to do this.
  2. L: Liskov substitution Principle (LSP) :
      • You should be able to use any derived class instead of a parent class and have it behave in the same manner without modification
  3. I: Interface Segregation Principle (ISP) :
      • You should design your interfaces in to smaller units, clients should not be forced to implement interfaces they don’t use. Instead of one large interface group your business functionality in to smaller units, small interfaces are preferred based on groups of methods, each one serving one sub module.
  4. D: Dependency Inversion Principle (DIP) :
      • You should design your high-level modules/classes in such a way that it should not depend upon low-level modules/classes. Both should depend upon abstractions. Secondly, abstractions should not depend upon details. Details should depend upon abstractions.

All the above points are summarized in to a single picture as below. Please free to download the below image and keep in handy when you are attending interviews. 🙂 Happy Learning.

solid principles
SOLID Principles

Based on my knowledge and experience in the software field, I have written the above blog article. Click the below link for more information and details.

Internet – Truth of information and credits.

Please leave any feedback in the comments area. Happy Learning 🙂

Comments are not allowed for the posts older than 30 days.

Please Click Here to contact me if you have any questions.