SOLID Principles With .NET - Liskov Substitution Principle
Liskov Substitution Principle
is a fundamental SOLID principle that states " You should be able to use any derived class instead of a parent class and have it to behave in the same manner without modification". It ensures that a derived class does not affect the behaviour of the base class .
Before going into implementation of LSP , please do go through the previous article on OCP . We are continuing with the same example .
This article is divided into three parts :
- Banking Service without LSP
- Banking Service Example with LSP
- Conclusion
And now business want to provide the Additional Compound Interest to Salary Accounts , so in our base abstract class we are now adding another method called CalculateCompundInterest . And our abstract class looks like this
and now the derived classes looks something like below and everything looks fine till there .
Now check the below code which violate the LSP principle .Now I guess you got the problem .
Yeah ,Saving account will get not implemented exception and that is violating LSP.
Then what is the solution for this ? Break the whole thing into two different interfaces which can maintain this principle .
Followed to this article , Interface Segregation Principle (ISP) is explained on top of this .
For source code for the above , it is present on Git Hub .
Comments
Post a Comment