SOLID Principles With .NET - Single Responsibility Principle
Single Responsibility Principle
The acronym SRP states that "A class should take one responsibility and there should be one reason to change that class". In this article I want to share my knowledge on how we can make sure to follow this in our career as a developer .
Every software module should have only one reason to change .The single responsibility principle gives you a good way of identifying classes at the design phase of an application.A good separation of responsibilities is done only when we have the full picture of how the application should work .
Now let's go into our example . And to go through SOLID principles , I have taken a real case scenario of a banking system as an example for clear understanding and to give everyone a wider view about how one should act while he starts to code .
And we have three different sections
- Banking Service without SRP
- Banking Service with SRP
- Conclusion
In the above code we see that
Account class is not only calculating the interest but also has method to generate report which is completely violating the single responsibly principle .But after few days , if the business asks to replace the crystal reports with excel generation , then this class need to be changed and which is not a good approach.
But according to SRP , one class should take one responsibility. So now the responsibility to generate report is moved into a new class and so that account class do have a single responsibility to taken care .
Comments
Post a Comment