S.O.L.I.D Object Oriented Principle

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

  1. public class BankAccount {  
  2.     public BankAccount() {}  
  3.     public string AccountNumber {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public decimal AccountBalance {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     public decimal CalculateInterest() {}  
  12. }  

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.

  1. class BankAccount {  
  2.     public string accountNumber {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public double accountBalance {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public double CalculateIntrest() {  
  11.         return something;  
  12.     }  
  13. }  

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.

Additionally, since the code base is the same, the testing of this change request will be extra additional efforts, since we need to test existing as well as new account type.

By following this principle, we would like to do below.

  1. public abstract class BankAccount {  
  2.     public abstract double CalculateIntrest();  
  3. }  
  4. class SavingAccount: BankAccount {  
  5.     public override double CalculateIntrest() {  
  6.         return something;  
  7.     }  
  8. }  
  9. class FixedDeposits: BankAccount {  
  10.     public override double CalculateIntrest() {  
  11.         return something;  
  12.     }  
  13. }  

So now, we have a class BankAccount which has a method CalculateIntrest() and all child classes are getting inherited from this call but all sub classes are free to have their own defined business rule. Even if they want to change the rule or need to update their interest rates based on any attribute they can do it, without impacting others classes or existing application. And also, if any new account type is introduced in future they can extend new class from BankAccount.

'L' -LSP (Liskov substitution principle)

As per the definition of LISKOV principle, it says the parent should easily replace the child object. Now let’s continue using the same example after changing the parent class from abstract class to normal class and create a new class – checkingAccount.

Checking account is not eligible for any interest. But as per our above implementation, if we create below class and implement CalculateIntrest() as below, it is violating the principle. As CalculateIntrest() method is having its own implementation.

  1. public class BankAccount {  
  2.     public virtual double CalculateIntrest() {  
  3.         return something;  
  4.     }  
  5. }  
  6. class CheckinDeposits: BankAccount {  
  7.     public string accountNumber {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     public override double CalculateIntrest() {  
  12.         throw new Exception("Not allowed");  
  13.     }  
  14. }  

The above example is violating the principle of LSP. The solution is,

  1. interface BankAccount {  
  2.     double DepositMoney();  
  3.     double WithDrawMoney();  
  4. }  
  5. interface IntrestCalculator {  
  6.     double CalculateIntrest();  
  7. }  
  8. class CheckingAccount: BankAccount {  
  9.     public string accountNumber {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public double DepositMoney() {  
  14.         return something;;  
  15.     }  
  16.     public double WithDrawMoney() {  
  17.         return something;  
  18.     }  
  19. }  
  20. class SavingAccount: BankAccount, IntrestCalculator {  
  21.     public string accountNumber {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     public double DepositMoney() {  
  26.         return something;  
  27.     }  
  28.     public double WithDrawMoney() {  
  29.         return something;  
  30.     }  
  31.     public double CalculateIntrest() {  
  32.         return something;  
  33.     }  
  34. }  

“I” - ISP (Interface Segregation principle)

Consumer of an interface should not be forced to use all of its method, if the client does not want to or does not need them. Which means , if you have a BIG FAT interface, try to see can that interface be broken into different interfaces? If you find it yes , then break it. Please note – breaking the interfaces does not mean that in any case you need to break it. If you think all the methods available in that interface are related and all client need it, then no need to over complicate it.

Example

  1. interface ILog {  
  2.     void log();  
  3. }  
  4. interface IDBLog {  
  5.     void closeConnection();  
  6. }  

In the above interfaces, if we put both methods together, then closeConnection() might only be useful if we are using DB logging. So better to break that into separate interface.

"D" - (Dependency Inversion Principle)

As per this principle, high level module should not be directly dependent on low level module. If we need to set the dependency of a low-level module inside a high level module, it should be via abstraction. I found a very nice explanation of this in the below blog -

https://www.codeproject.com/Articles/615139/An-Absolute-Beginners-Tutorial-on-Dependency-Inver

Again, as I mentioned in the beginning, S.O.L.I.D principles are the guidelines but not hard and fast rules. So while designing solutions or developing any new application or enhancing existing applications, follow these principles to make a maintainable solution.