Introduction
To design and develop high-quality software, well–established design principles need to be followed. Those five principles make the acronym S-O-L-I-D. That’s what this article is all about.
I will explain SOLID principles in this article. Let’s start with what the SOLID principle is.
What is SOLID?
SOLID stands for
- S- Single Responsible Principle (SRP).
- O- Open Closed Principle (OSP).
- L- Liskov Substitute Principle (LSP).
- I- Interface Segregation Principle (ISP).
- D- Dependency Inversion Principle (DIP).
Michael Feathers introduced the SOLID acronym in the year 2000. SOLID is a design principle that plays a very important role during Object-Oriented design. Software development becomes easy, reusable, flexible, and maintainable using these design principles.
Robert C Martin has promoted SOLID Principles and now it’s very famous. It has changed the development approach and dominated in software development industries.
Benefits of SOLID
- It makes software design more understandable, flexible, and maintainable.
- Best suitable principle can be used as per project requirement.
- Loosely coupled.
- Parallel Development.
- Testability.
- Code becomes smaller and cleaner
- Maintainability – Large Systems or Growing systems become complicated and difficult to maintain. This Principle helps us to create a maintainable system. A maintainable system is very important in industries.
Single Responsible Principle (SRP)
Robert C Martin's original definition is, "A class should have only one reason to change".
As the SRP name says, each and every module/class should have single responsibility in software, and that responsibility is encapsulated by the class.
If a single class has multiple responsibilities it may cause problems. To avoid these problems we should separate responsibilities in multiple classes or modules.
Let’s start with a simple example. Suppose we have a company that creates new members and logs error messages.

What is the code doing? This code has 2 responsibilities
- Add newly added member in the database
- Logging activities that log errors in a text file.
What will we be doing if we want to log errors in the event viewer or in the database? We have to change the logging code in all the classes including member class. This means member functionality is not changed but still, we have to touch member class because of logging. This violates first principle (Single class or module should have a single responsibility).
Now we will separate these two responsibilities in 2 separate classes,
In the above code, two separate classes (Members and Logger) have their own responsibilities, which solved earlier problems in the code and fulfill the single responsibility Principle.
Open Close Principle (OCP)
Bertrand Meyer defines OCP in 1988, “Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”
Let’s now discuss on what are the problems this principle solved,
Suppose we want to introduce member type and based on the type we want to give/calculate the discount.

Let’s think, what is the problem with the above code? If we want to add another member type, we have to change the working member class code and here we use the Open close principle.
OCP says a class should be,
- Open for extension – means we need to create a base class and that class will be available for extension. This class should have common functionality.
- Close for Modification – Instead of changing the base class, we will extend the base class and add/modify type-specific coding in the derived class.
Assume that we have two types, if MeberType =1 (IMP Member) and MemberType =2 (VIMP Member) and a base class Members. Let's try to undestand using the below diagram.

As base class (Members) is open for extension, derived classes inherited it and extend their functionality as per requirement. In future if we want to add another type, we have to add another derived class.
Let's see the below code,

In the above code, 1 is Base Class and 2&3 are Derived classes. Derived class extended base class and modified discount as per type.
So in simple words, the Member base class is closed for modification but open for extensions.
Liskov Substitution Principle (LSK)
Barbara Liskov introduced this principle in 1987 in the conference (Data abstraction and hierarchy) hence it is called the Liskov Substitution Principle (LSK). This principle is just an extension of the Open Close principle.














David CosterPosted Dec 21, 2022, 11:32 PM
I am thinking that the second code example creates a Logger object called "obj". For readability I'd recommend the object be called "logger" or something. When I read the code I saw "obj" and I have to then search to see what it was.