Is a square a rectangle? Is a rectangular square? If you had all of these questions
in your mind today they will be answered with the help of the Liskov Substitution Principle.
A principle from the SOLID Principles. I'm Alex from rebeloper.com and if you do want to learn
all about this go ahead and hit that SUBSCRIBE button now let's open up Xcode and let's dive in.
Liskov Substitution Principle. This is what we are going to tackle today. But before we dive
into that let me remind you about rebeloper.com/mentoring. A 60 minute Zoom call with me where
you can ask me questions whether it's bugs in your code or you would like to learn SwiftUI or Swift
go ahead and check out rebeloper.com/mentoring. and because it's the New Year we are going to
have a game on our channel. So this is what you have to do to win one mentoring with me. You have
to first of all SUBSCRIBE to the channel if you haven't done so. That's a shame on you. So SUBSCRIBE,
LIKE this video and SHARE it and then final is to COMMENT down below in the comment section. So four
things SUBSCRIBE, LIKE, SHARE and COMMENT down below and in the next video I will pick a random winner
from all of you that did all of these four things. So go ahead and do that if you do want to win
one mentoring session with me and make sure to check out the next video so where I pick out
the lucky winner from you guys, okay. So Liskov Substitution Principle. Now what is this Liskov
Substitution Principle and that is one of the principles from the SOLID Principles and we
have covered the first two Single Responsibility and Open Close Principles. Go ahead and check out
the link in the description for the whole playlist where you can take a look at them. Now we are going
to tackle the Liskov Substitution Principle which is part of the SOLID Principles of course.
Now why do you want to adopt the SOLID Principles you might ask. Well because it
solves bad architecture like Fragility, Immobility and Rigidity. Now the principles establish
practices that lend to developing the software with considerations for maintaining and extending
as the project grows. So you are not writing this code print according to these code principles
just because, you are writing for your future self. Now the first one was the Single Responsibility
Principle. We talked about that, go ahead and check out the link in the description for
the playlist. Then Open Close Principle came and finally we are at Liskov Substitution Principle.
Now it states: Objects should be replaced with instances of their subclasses without altering
the behavior. Now this is the short version here's a much longer version. If S is a subtype of T
then objects of type T may be replaced with objects of type S without altering any of the
desirable properties of the program. Now again this is encrypted for now but we are going to dive into
an example. So let's just do that let's open up our Xcode project and if you haven't already
done so there's a link in the description for the resources where you can download the resources for
these series and basically you have all of these folders with a before and after file, okay.
So let's start off with the before file here and let's just uh build up
kind of an example which doesn't adhere to the Liskov Substitution Principle and
we are going to start off with building out a rectangle and calculating its area. So I'm going
to create a file private class here and this will have a width and a height of course.
So let's just have it right over here. Let me just have a lot more space so we can type out
all of our code efficiently. So width, that would be a Float and let's just set it to zero by
default so we don't have to bother with the initializers and let's have a height also of Float
and that's equals also zero. Now when we set the width and the height we are going to
have some functions for that. So func and self dot width equals our input width and
we are going to do the same for the height. So let me just copy that out and paste it in.
Change it to height and again set height equals height, okay. So that is how we are setting our
width and height and finally let's have a function and that will return a Float, okay. And let's just
return here with and multiplied by height, okay. So this is reasonable, okay. This is good code by the
way everything here is good code, it works but if you don't adhere to these principles you will have
a lot of troubles on the long run, okay. So that is why actually you want to adopt all of these.
Now let's say okay we have our rectangles so let's create a square here. So again a file private class
and then we want to override because this is a square it has equal size so we want to override
our width and height function. So maybe set width, there we go it's just auto completing it and you
might go like setting the width to the actual width and also setting the height also to that
input width, okay. And let's do the same for the set height function. Let's override it and self dot
height equals height and then self dot with equals width, okay. This is fine, this look
okay we don't really see the problem right over here but let's just have a file private
using a file private function using all of this. So let's just have a file private
func and let's call it set size and let's just print out the area. So set size and print area
and we want to add in here a rectangle. So of rectangle of type rectangle, okay.
And what we are going to do here is just go rectangle dot set and let's just set the
height for example. Let's set this to 5 and then we just go rectangle dot set the width of maybe
4 and then we just print out our rectangles calculate area, okay. So that's great.
This looks okay but what if we had another function that uses also a rectangle and
also a square. So a rectangle and also a square. So let's have that file private func example function
that I'm talking about and this is where it all starts to get kind of not good. So let's create
our rectangle and that equals a rectangle, simple rectangle and then set size and print area of
our rectangle, okay. This works fine and actually this will print, let's just go over here and
prints 20. Okay! So it will just print out 20. But what happens if we just have let square
equals our square and then of course we can also set size and print of area
because square is actually a rectangle that's how we define it and let's just have a square
here and actually this is where you might want to touch the essence of
the Liskov Substitution Principle because this will actually print out 16.
So why is that? That is because we have first set the height to 5 and then we set the width to 4
and that's not the way it should be. Why doesn't it print out maybe 20? Why is it exactly 16?
So this is where it all gets hazy. So if we do have and by the way we can have this function, so if
we can have this function this breaks the Liskov Substitution Principle. So we
shouldn't be allowed to even create this kind of function. So what to do instead? Well let's just go
into our after file right over here and let's rephrase our rectangle and square
approach. Now instead of creating a rectangle we would create a protocol. So this is where
protocols come in and go ahead and check out the link in the description where I detail
everything about protocols that you need to know in that video, okay. So let's create a protocol
that will encompass the square and the rectangle . So let's name this protocol a polygon and
if you take a look at that video you will see that protocols are kind of like
these contracts. So this polygon contract will require everybody
who adheres to this contract to have a function called calculate area, okay. Now if and this will
just return a Float. Go ahead and check out the video on protocols if you don't
really understand what I'm doing here with the protocol. And that's it we created our protocol,
now it's time to create our classes. So first of all I want to create a rectangle. So file private class
rectangle, okay and this adheres to the polygon protocol. What this means is that we must add in
here the calculate function. So we must adhere to it and for that I will have to have
width and height. So var width and that is of type Float equals zero. Let's have a default var height
again of type Float equals zero and the area will just return, calculate area will just
return with multiplied by height, okay. And we can also have here a function called set width and
this will just go self and width equals width and now let's just do the same for our height.
Okay! I just have height right over here.
There we go. Okay! So yeah let's just hit Command + B so that error goes away and we are done
with our rectangle polygon. Now let's create our square polygon. So this will be different. So let's
create a file private square of type polygon. Where it is polygon, there we go. Because we adhere to
the polygon protocol we need to add of course our, oh sorry not that one but file private, oh
I need to add in here class first and then we need to adhere to the calculate area. So what I will do
let's just add in that function for now and let's just have our width and height added but actually
we are not going to do name it as width and height, we are actually going to go with a side because
squares only have sides. So let's have that as a Float and let's set this to zero and then
we want to have a function and that will go self dot side equals side. Now you kind
of get to see the difference here. Here we are going to return pow and side squared, okay. So now
you see the differences between those two. If we want to print out the area now we are going to
create that fireprivate function that prints out our area. So let's just name this func print area.
Oka! Here we want to actually file private yeah we need to add space here add a polygon and
we want to go print our polygons calculate area, okay. And now let's create our example
function. So again file private func. Now it's time to create our rectangle and our square.
And this is where it all shines that you cannot actually make the confusion between those two.
So let rectangle equals our rectangle, okay. Next we just go rectangle dot set width
of five and then rectangle dot set, sorry not width but set height. Set the
height, there we go. 4 and then we just go print area of our rectangle and this will actually
prints 20, which is how it's supposed to be, which we are expecting. Now let's create our square.
Square equals square and now we can just simply add a square set and all we have
here and this is the important part all we have here and only one thing that we have here a side.
And let's just set this to four and then just print area of square and this prints out
16, what it should do. So yes! That is the Liskov Substitution Principle. One of the five principles
in SOLID. Now if you do like these types of videos go ahead and check out rebeloper.com/mentoring
where you can meet me on a one-on-one Zoom call and don't forget about january's game. Go ahead and:
SUBSCRIBE to the channel if you haven't already done so. LIKE this video, SHARE it with somebody you
want to share it to and then COMMENT down below. It's very important so I can pick one of you guys
to be a winner of a one hour mentoring session. Looking forward to your comments down below
about the Liskov Substitution Principle. Now if you do like these types of videos make sure to
smash that LIKE button and while you are at it go ahead and hit that notification bell to get
notified of new videos. New videos like the next one in our series and that is about the
Interface Segregation Principle and if you do like this go ahead and check out also the
playlist, looking forward to your comments and as usual I will see you in the next one.