Just because you are using object oriented programming, it does not mean that you are doing object oriented programming. In order to do that, we need to follow certain principles while designing our application.
S.O.L.I.D principles are those guidelines, which help us to create a clean, maintainable, independently testable solution. Please note that these are not hard and fast rules which you need to follow in any case. But these are the standard principles which allow our objects easy to change and easy to test.
So, let’s dig into it in detail.
'S' (Single responsibility principle)
As the name suggests, a class should be solely responsible for one thing and only one thing. If we need to change the class, there should be one and only one reason to change. So, when you design a class, think about what that class should be doing. If you find that the class is taking care of multiple things, then plan to break that class into different classes.
SRP allows you to have modules loosely coupled. (Coupling means, what are the dependencies between two or more components (objects) in your application. If your application has high coupling, it means that the independent components are highly dependent on each other. If we change one ,there is a very high possibility that we may break another.)
Ex- 1
- public class BankAccount {
- public BankAccount() {}
- public string AccountNumber {
- get;
- set;
- }
- public decimal AccountBalance {
- get;
- set;
- }
- public decimal CalculateInterest() {}
- }
In the above given example, class BankAccount is holding Accountdetails but it is also doing interest calculation. so, this class has two different responsibilities which is against the SRP's principle.
In order to follow SRP, we need to break this class into two different classes. 1. Only to hold BankAccount details 2. Only to calculate intrest.
'O' - Open/Closed principle(OCP)
According to this principle, classes should be open for extension but closed for modification. Let me explain what it means and how to implement this.
- class BankAccount {
- public string accountNumber {
- get;
- set;
- }
- public double accountBalance {
- get;
- set;
- }
- public double CalculateIntrest() {
- return something;
- }
- }
The above code looks clean and there is no problem in that. Now, consider a scenario where we have been asked to calculate the interest based on certain criteria e.g accounttype , account balance etc.
Now, one option we have is, we will keep changing the logic whenever the bank adds any new type of account or makes any changes in the existing one. The problem in this solution is that even if we need to add a new account type, we will be adding risk to the existing account type as well, because they are using a common code.
By following this principle, we would like to do below.
- public abstract class BankAccount {
- public abstract double CalculateIntrest();
- }
- class SavingAccount: BankAccount {
- public override double CalculateIntrest() {
- return something;
- }
- }
- class FixedDeposits: BankAccount {
- public override double CalculateIntrest() {
- return something;
- }
- }

Join the conversation! Your thoughts help the community grow.