LSP-Liskov Substitution Principle Part 1

C# Design Patterns and SOLID Principles SOLID Principles With Code Sample
10 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

OK, so far, so good. And up to this point out of five, we are done with two principles. So I hope single responsibility principle, which is the first one, which is going to tell you that your class should be focused on the same single purpose. And rule number two, which is open closed principle, which is telling you that your course should be open for extension, but it should be closed for modification. And I hope you guys are able to understand both of these principles. Now, it's time to move on to Principle number three.

But, before we move on to Principle number three, I want to discuss one of the small requirement which we are assuming that we got in this particular project. Now, as we know, we are creating a software which is going to take care of the invoice mechanism of my client. And in this case, maybe we need to do some calculation as well as maybe we need to take a print of invoice. Now, suppose I got a new requirement from my same client. And they are telling me that sometimes after generating an invoice some of the clients, they want to just modify the invoice. So it means we already have an invoice which is already generated.

And then maybe based on some requests, we need to modify that. If I want to do that thing, I need to add a new class, which can help me in modification of an existing invoice. So I'm going to right click on this particular project, and I'm going to work on this new requirement. So I'm going to add a new class, and I'm giving the name of this class invoice modifier. The moment we add this modifier class, the first thing which I'm always going to do is I'm going to make sure that this class is going to be public. And now inside this class, I have to write some code, which technically should connect with my invoice class so that I can modify that.

Inside this class. I have a code like this and now if you check this code Have a constructor of invoice modifier, which is having one parameter of type invoice and the value of the parameter we are going to assign to a private variable which is also type invoice, the name of the variable is underscore invoice. Now this is the same thing which you always do with the constructors and the parameters of that. Ultimately, the logic of this class is telling me that we have two methods set subtotal and set tax rate. These two methods are going to allow me to pass my parameter in the form of decimal data type, which is subtotal and tax rate. And based on that we are able to modify our invoice subtotal and tax rate.

So in short, the new values of supporter and tax rate will be assigned to the property which is suffering tax rate associated with that invoice. The moment we are done with this two method calls, we'll have a new values assigning to that and then we are going to call one method which is generate invoice which is the purpose of this particular class and this will generate invoice based on that instance underscore invoice. Again, we are following rule number one, which is single responsibility. And the responsibility of this class is just to get a newly modified invoice generated based on the changes which we are doing. And this invoice modifier class is associated with invoice because ultimately we can modify only those invoices which are associated with the invoice cloth. Once you're done with this, this invoice modifier is done.

Okay, now assume that from a government, we got a new kind of text. Like right now whatever tax calculation we are doing this is something which is based on the current tax rate. Now assume that from a government, we got a new text, which is a complex text. I don't know about others, but in India, we have a lot of Texas which we have to pay. So that's why I know that any particular point of time, maybe government is going to launch a new text and we have to modify our software for that. Now assume that I got a new kind of tech from government.

And now the scenario is that my clients sometimes bank wants to generate invoice based on the normal tax rate, which we have already calculated and given in this, and sometimes they have a requirement to generate an invoice with the complex texts. Now when I want to do this thing, maybe I need a new kind of class which can help me to generate a complex invoice. To do this, I'm going to add one more new C sharp class in this project. So I'm going to right click on this project and I'm going to add a new C sharp class. I'm giving a name of the C sharp class complex invoice and then I'm going to click on Add. Now this complex invoice class is also going to be public.

And as per the understanding, I hope it is fine that this is going to have everything seemed like invoice class. My inverse class is having subtotal tax rate and other things inside that my complex invoice is also going to have similar kind of thing. But the only thing is tax rate is not going to be this one is going to be something new, which I got from a government. So I'm just doing one thing first, I'm going to put the parent class of this Which is my invoice class. So it's something like this that we are trying to use inheritance in this goal. And invoice is going to be a parent class and complex invoice is going to be a child class.

Now, I hope you're able to understand that as per the rule of inheritance, child can access everything of parent. And that's why whatever we have an invoice, which is these two properties, and these two public methods are going to be accessible into the complex invoice. Using this rule, I'm just going to add two new things inside complex invoice. First I want a property which is going to be public decimal, second tax rate. And the second tax rate is actually going to be a new kind of tax rate which I got from government, which is also having gettin set. Now after this property, this is that property which is only the complex invoice and if I have a second tax rate, obviously they're going to have a new kind of total which is based on this tax rate.

So that's why the method which we have inside this which is calculate total and calculate tax, Now it's the time to use one of the methods inside of a complex invoice child. So I'm specifying public new, the symbol and then after defining calculate Tex now this time, I'm just going to make sure that the name of the method is going to match with my calculate tax of my invoice class. Now, the moment we do this thing, what we are trying to do is we are trying to redefine a new implementation of this method. So the parent class is also having a method with the name calculate tax and the child class is also having a method with the name, calculate tax, we are just going to redefine so we are going to add a new implementation of this thing in the child class. My new implementation will go like this.

We are going to return subtotal into tax rate divided by hundred that's what the normal and the previous code but with this code I'm going to add that not only this tax rate, we are also going to add subtotal, which is going to be multiply by second tax rate. And then the second tax rate is also going to divide by 100. And this is only going to be a new kind of a calculated tax. So, this time we are not only using a tax rate, but we are using tax rate as a second tax rate both as per the rules of the government. Now, if you are able to understand this new requirement and the logic which I have changed inside this, our core goes fine and we are still using all the principles of principle one and two. But now in this case, there is one small problem because of this inheritance.

Now if you check our invoice modifier class, which we have created just before two minutes, then you can check one thing that my invoice modifier class is only connected with invoice and that's why it actually do not know that we have a class like a complex invoice. Also. If any modifications required using invoice modifier is going to only modify invoices which are associated with the actual invoice class. Because it's really did not have any idea that we have a class which is complex invoice, which is also having a method calculate tax, which is having a new implementation of a parent class. In this kind of scenario, this is that place where liskov substitution principle is going to come into the picture. Now, let me tell you one thing, let me introduce one thing first, the lady who's there on screen, her name is Barbara Lesko.

And by profession, she's an American computer scientist. And let me tell you one thing, she's the one who has invented this principle, which is known as liskov substitution principle. And that's why the name of the principle is on her name liskov substitution principle. Now, she's is saying in her principle, that the parent cannot replace the child object seamlessly. Now obviously, when she's saying this thing, she's talking about inheritance of C sharp, we know that we have a special In class and child class, and most of the time, a child is going to have everything of parent. And that's why it can replace the parent seamlessly always.

But in this case, there are certain situations in which a parent cannot replace the child object seamlessly means always, it's not always true. And that's why we have to understand that a parent cannot replace the child object seamlessly. And the same situation happened with us also in this code. If you check this code, my parent class which is invoice and my child class, which is complex invoice. Most of the time, if I have any logic which are associated with the parent is going to work fine because most of the time child is going to use the parent methods. It's not going to modify with the new logic.

But there are certain situations like this in which we need a new kind of implementation of the calculate takes and that's why this is the place where a parent cannot replace the child. If I want to do some calculation related to complex invoice I need to associate with the child Class cannot only associate with a parent class and solve this problem. This is what this goes up to the principal is trying to tell you in this particular principle. Now, I hope that you're able to understand the principle and what they are trying to say in this. If you have a question, how we can fix this, how we can follow rule number three, and how we can make sure that our code is also going to follow third principle, which is this core substitution principle. Then we have a part two of this video, which you're going to see next.

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.