字幕表 動画を再生する 英語字幕をプリント Hello, everyone. In today's video, we're gonna be covering composition versus inheritance and why going with composition is almost always better than inheritance. And knowing the difference between the two is one of the easiest ways to write better and more maintainable code. So let's get started now before we get started. I want to talk about today's video sponsor, which is atlantic dot net hosting. And this is an incredible hosting company, which is giving you an entire year of hosting completely for free. If you sign up with the link down the description below and this isn't some cheapo server, this is actually an incredibly powerful server, which is more powerful than the server. I even host my own website on, so you know that this is gonna be powerful enough to handle any of your needs. On top of that, they have incredible reliability and redundancy on their servers. So your Web page is always going to be up and always available, which is a great thing to know. On top of that, you're gonna get an additional $50 of free credit if you sign up using the code Kyle. So make sure you check that out using the link down in the description below. And with that out of the way, let's jump into the video You came here for Welcome back toe Web have simplified my name's Kyle, and my job is to simplify the Web for you so you can get started on your dream project sooner. So if that sounds interesting, make sure you subscribe to the channel for more videos, just like this one. Now to get started. I've already written the shell of the code for what we're going to be doing, but essentially, this code is going to be code inside of some video game. So you can imagine that this video game has monster characters that are trying to attack the main character and you have different type of monsters. So we have a standard monster which can attack and walk so it could walk towards the character and it can attack. We also have a flying monster which can fly as well as attack, and it can walk because it extends from the monster based class. And then we have a swimming monster which can swim, and it can also attack and walk, just like the normal monster because again it extends monster. And this is the basics of inheritance. We have this base class which has all of the functionality that our Children share. So that way we don't have to re implement, walk and attack when we make a flying monster or a swimming monster weaken. Just inherit that by saying extends monster. And then down here we have a few examples. We have a bear and eagle and a shark, and each one is of their specific type. So now if I save this, you can see that everything's printing out. Find the bear can walk an attack. The eagle can walk, attack and fly. And then the shark can finally walk, attack and swim. And this is great. And when you're getting started, this may be what you write for your game and your game code is working fine. Everything is great until one day someone says, What if we want to add a new type of monster that can both swim and fly? We want to build a fly and swim with this monster. So you say, OK, I'm going to do that will create a class. We're gonna call it flying swimming monster. And of course, we're gonna come in here and we're going to extend a monster. And then you'll realize that you need to duplicate your flying logic and your swimming logic into this new class. So instead you'll say, OK, I'll extend the Flying Monster. But now you need to duplicate the swimming logic. So you kind of get stuck in this pickle of there's no way to do this without duplicating logic or what you need to do is you need to say, Well, I'm going to move this five function here up into my base monster class and I'm just gonna have it do absolutely nothing. We're just gonna have it return, and then we'll keep it in our flying monster and then inside of our fine swimming monster, we can just extend swimming monster, and then fly is already gonna be taken care of up here. We could just re implement it down here, but as you can see, there's really no good way to go about creating this flying swimming monster. So this is where the idea of composition comes in place with inheritance. You're describing exactly what objects are and how they're related to each other, but with composition, you're describing what an object can do. So in our example, we have attack, walk, fly and swim as the actions that are monsters can do. So what we could do, instead of having all of these different classes, is if I just come down here, we can create a function. And we're going to say that we want to implement the swim functionality. So we're going to say this is going to be a swimmer and inside of this swimmer, what we're going to do is we just want to return an object inside of this function that has our swim functionality. So we're gonna say return, we want to return an object, and the object is going to have a swing property, which is a function, and this function is just going to do whatever swim did before. So she's gonna say council dot log this dot name swam. And since there is no this inside this function, we didn't actually pass in the name, and we're gonna pass it using object de structuring. If you're not familiar with that, I have an entire video covering it looked in the cards and description for you to check out. But essentially we're gonna pass an object, and one of the properties of that object is going to be named. So now we have that name property inside of this swimmer function. And now we can just call swimmer like this if we just call Swimmer and we pass it in an object that has name, for example, like this. Now what we've done is we're getting a new object that has this swim function on it and we can't even see that. Hear what you're saying? Object is going to be equal to that. And we can call object dot Swim. As you can see, just like this and what's just call this test now? If we save, you can see we get test swim. So our swim functionality is working right here on what we can then do is go even a step further. We'll create another function. We're gonna call this swimming monster creator. It's going to take in the name of the monster we're going to create, and we're going to create that monster variable. We're just saying we have an object with the name property just like this, and then we're going to return our monster object. So we're gonna use this spread operator, which again, that video I mentioned earlier on de structuring covers spread. But essentially, what it does is it takes all the properties inside Monster and adds it to the new object. So we now have a name, and we can come in here and do the same thing. But for our swimmer, we're gonna pass in our monster because it has that name property. And now this swimming monster creator. We can just call this and we can pass through the name. In our case, we're just gonna pass it here, monster. And now it says monster swim. So we just created a function which does the same thing are swimming monster class did. Except for now, we can actually just add this swimmer function into any of the classes that we want so we can create that flying swimming monster super easily. So if I just copy this, come down here and I say flying, swimming monster and we just need to create a new function called a fire hose flyer just like that. And we just need to pass in the monster and fire is gonna look just like swimmer except for a fire here is going to be slightly differently because it's going to have a fly function and it's just going to say flew in here. Now if we create a flying swimming monster just like this, we can say object dot fly. And if we run that you can see that monster can now fly and it can swim. Lastly, since we want to make sure we take into account our functionality for attacking and walking up here, we could just come all the way back down, create a new function, and we're gonna make this one lives Attacker and Walker and what this is going to Dio is going to have an attack function, which is going to say it attacked. And it's also going to return an object that has a walk function, which is just going to say, walked inside of it. And now we can use this attacker and Walker inside of our other monster classes. Just so the functionality is exactly the same as it was before, and now all of our different monsters can actually attack and walk. We can see that by just saying dot Attack and we can say object dot Walk Now, if we save it, you could see it That way our monster can attack, it can walk, it can swim and it can fly. And the great thing is, we can create any combinations of these that we want. For example, here we have our swimming monster. We could create a flying monster that just has a flyer here instead of a swimmer. We could also create a monster that can't fly or swim by just removing this completely. And you see, you have tons of different compose herbal objects you can create. And that's the idea of composition. We have all these functions here which define exactly what all of our objects can do. They can swim, they can attack and walk, or they can fly, or any combination of those. And then what we do is re create these functions which just return new objects that have all of the functionality we want inside of them. So, for example, here we have an attacking, walking swimming monster. This one couldn't fly, attack, swim and walk, and so on. And you can imagine in a real life game. You may have hundreds of different types of monsters that you want to create flying, poisonous walking. You You can think of a ton of different combinations and the ability to just create a new function. Throwing the things you want and just have a new type of monster available is really powerful. You can also see something that's really powerful about this is there's absolutely no classes. If we removed the code up here, we started with and save. You see, all we have is a bunch of different functions. There's no inheritance. There's no object oriented programming to worry about. No classes. It's just super straightforward and to the point and incredibly flexible. That's the main thing with composition is it allows you to be really, really flexible with your code and not be stuck in the way of doing exactly the same thing and having to follow that strict inheritance tree. And that's all there is to composition versus inheritance. If you enjoyed this video, make sure to check out my other clean code related videos linked over here and subscribe to the channel for more videos just like this one.