SRP-Single Responsibility Principle

C# Design Patterns and SOLID Principles SOLID Principles With Code Sample
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

Okay, so now I guess you're ready to learn solid principles. And for that, without wasting time in theory at all, I would like to cover theory with code samples. So I'm just opening up with this video. And inside that we are going to create a new application. So for certifying that, I'm going to create a console app in dotnet framework. And let's say that we are going to give a meaningful name like the name of this application will be solid underscore P. Now this P stands for solid principles.

And the moment I click on OK, I hope you guys know that console application is the simplest application of dotnet framework. And ultimately, this is nothing but one simple EFC with only one class inside that with the name program.cs or something like that. So this is going to be a very simple console application right now. But step by step in this video series, I'm going to show you How we can write a code inside this by following all the principles available in this. So now we have a solid p which is having a program.cs inside that, which makes sense. Okay, it's just an empty class with a main method inside that for me, the first thing which I'm going to do is I'm going to right click on my solid p which is a console application, and I'm going to add a new C sharp class.

Now the name of the C sharp class I'm giving right now is invoice.cs. I'm just taking a scenario that assume that we are developing a software for an organization and this software is going to take care of invoice generation invoice printing, and all the calculations which are required for the invoice and all the declarations which are required for the invoice. Now inside this class invoice, the first thing which I'm going to do is I'm going to make sure that my graphics is whatever is going to be public, so that I can access this invoice class, anywhere. In this project and then inside this class we just have a simple logic like this. So you can check that I have an invoice class, which is having first two things like subtotal and tax rate. And if you know basics of object oriented programming concepts, I hope you know that these things are known as properties or even the better bodies automatic properties, because if you check subtotal and tax rate both are having a getter and setter inside this without any values.

So this is something which is known as automatic properties. And both the properties in my case are of type decimal. After these two properties, I have total three methods. So you can check the first method is calculate tax, which is returning decimal. And in that I'm just simply having a logic to do the calculation like subtotal multiply by tax rate divided by hundred. So that's what is going to give me a tax calculations.

And then we have another method which is calculate total, which is doing subtotal whatever Getting from that plus the method which is calculate tax we are calling here. So it's something like this that calculate tax is on the top. And we are calling it inside this calculate total. Finally, we have a third method, which is sprint, and the sprint is taking one parameter, which is of type invoice. So this is my class itself. And the sprint is not returning anything, it's just returning void.

And we do not have any logic right now inside the sprint, but I'm assuming that I'm going to have some logic of printing inside that which is right now not there in this code. Now, the question is, this is a very simple class and I hope you are able to understand the logic and the code which I have written inside that. So in this simple console application, I have a very simple class. And as we know, the first rule of solid principle is single responsibility principle. And to understand this principle, first of all you need to understand or you need to be smart to understand how many responses Release are there in a particular class. Like, for example, if I ask you that this class, which I have created right now is having a name invoice.

And this invoice class is having two properties and three methods inside that. If I asked you this question, how many responsibilities I have in this class? Or how many responsibilities this class is going to follow? What will be your answer? This class is having one single responsibility, two or three. This is something which I want an answer from your side.

Because this is that one of the thing where most people make mistake sometimes, if I ask people how many sponsors out there in this class, they answer that we have three responsibilities. They answer three because most of the time they focus on three different methods, and they count the number of public methods in the class. Each public method is going to count like a one responsibility and that's why we have three responsibilities. Sometime even a few guys who are not clear with this concept, they count the properties also as a responsibility, which is not exactly true. And that's why the right answer of this particular class is we have total two responsibilities. So the answer is, it's this invoice class is having two responsibilities.

One is this class is responsible for printing an invoice. And another one is this class responsible for calculate total. Now, I'm sure you want to ask me one question that why calculate tax is not a part of the responsibility. The reason is calculate tax is the method which is actually used inside calculate total. So this is not a separate responsibility. This is something which is a part of this.

And that's why we created that method. And we can call that thing here. Now, same way, right now, as I'm saying that this class is having two responsibilities. This is also possible that somewhere calculate total I'm going to call inside my print method. At that time, you The invoice class is only responsible for printing and all the other things are used inside that. So I just want to clarify one thing that when you try to understand this kind of class and the responsibilities do not focus only on number of public methods, please focus on the functionalities which are there in that particular class.

Or I can say the purpose which that class is solving. And that's where our single responsibility principle is going to come into the picture. Now, I asked you one question that how many responses are there in that particular class? But actually more than that question, there is one more question which is very, very, very important. The question which I want to ask you now is, what do you think, which is a better option? First, a class with only one responsibility, or a class with many responsibilities.

Now, before you answer this question, I just want to compare the scenario with one of the real life scenario like if you see online My screen, I have two different nights available. On the screen, the left side one is a Swiss knife, I'm sure you have seen a Swiss knife which has many functionalities inside that I can use the Swiss nine for different different purpose. And the right side we have a normal life which we use in our kitchen every day. And then based on that, we are going to use that knife only for one purpose which is cutting, cutting, cutting something. Now, if I ask you out of this two things, you just visualize that the right side one is a class with only one functionality, only one responsibility, which is a normal life. And the left side one is a class with many responsibilities and many functionalities inside that.

If I ask you, which is going to be more useful. I know there are a few guys who choose. So it's nice because it's having many purposes, but in real life as well as in the application also. The right answer is a thing which is only solving one focus Purpose is the one which is more useful. And if you don't agree with me just check one thing. If I asked you how many times in a month you're going to use a Swiss knife.

And if I asked you this question, I'm sure the answer is not so many times, most of us, we do not have a Swiss knife with us. And even if we have, we do not use it regularly, it's very rare to use a Swiss knife regularly. Even though it has many purposes, we never use that thing every day. While on the other hand, we have a normal life, which we use mostly everyday in our kitchen. Even though this is solving only one purpose, that is the one which is most useful. And very often we use that thing.

The same case is there in the application, also a class which is only solving one purpose, maybe going to be much more flexible and reusable inside the application in spite of the one class which is having many functionalities inside that. So the answer of this question is which is a better option. Obviously one class with only one responsibility inside that. And this is where I want to change my code a little bit now, to solve a purpose of a principle number one, let me go back to my Visual Studio. And inside this within studio as I know that I have an invoice class with FME two responsibilities. One responsibility is calculate total in which we are using this property and method.

And another responsibility is printing in which we do not have any logic right now. But now if I want to follow rule number one, which is single responsibility principle, I'm going to right click on my project, and I'm going to add a new C sharp class once again. And then maybe I'm giving a name of this class, invoice printer. The moment I given the name of this class invoice printer, this is a new class which is invoice printer, and I'm making sure that this class is also going to be public so that I can use it Anywhere. And then from this invoice class, I'm just going to do Ctrl x. For the sprint logic, I don't want that this logic is going to go inside that.

And inside of that, I'm going to put this sprint logic inside this. Now if you focus, I have two different C sharp classes in my project invoice.cs and invoice printer.cs invoice.cs is the class which is only dealing with the invoice calculation and the total, while the invoice printer is only focusing on the printer. And this is where we are satisfying rule number one, which is single responsibility principle. So now we have two different classes which only concerned about their own area of expertise, or I can say a domain logic. This one is also going to help us in the test driven development. I hope you know that nowadays most of the applications write unit test cases for the logic.

And if you have classes with the single responsibility that's also going to help you in TDD approach. And that's what the single responsivity principle is trying to tell you that you should be concerned about a single focus purpose for each class. And your class is going to only focus on one particular purpose. And that's why we are going to have only one responsibility. I hope you're able to understand Principle number one, and the do's and don'ts associated with this. It's time to move on to Principle number two now.

Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.