Composition Vs Inheritance - Why You Should Stop Using Inheritance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 dotnet 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 web site on so you know that this is going to 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 dollars 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 to web dev simplified my name is 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 they have different type of monsters so we have a standard monster which can attack and walk so it can 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 read implement walk and attack when we make a flying monster or a swimming monster we can 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 fine the bear can walk in 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 be able to fly and swim with this monster so you say ok I'm going to do that we'll 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 monster and then you'll realize that you need to duplicate your flying logic and you're 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 returned and then we'll keep it in our flying monster and then inside of our flying swimming monster we can just extend swimming monster and then fly is already going to be taken care of up here we can just reimplemented 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 our monsters can do so what we can 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 function out so we're gonna 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 it returned we want to return an object and this object is going to have this swim property which is a function and this function is just going to do whatever swim did before so she's gonna say console dot log this dot names swam and since there is no this inside this function we did actually pass in the name and we're gonna pass it using object destructuring if you're not familiar with that i have an entire video covering it linked 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 main 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 the swim function on it and we can even see that here by just saying object is going to be equal to that and we can call object swim as you can see just like this and let's just call this test now if we save you can see we get test swim so our swim functionality is working right here and what we can then do is go even a step further we'll create another function we're just going to call this swimming monster Creator it's going to take in the name of the monster we going to create and we're going to create that monster variable by 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 the spread operator which again that video I mentioned earlier on D 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 and we're gonna pass in our monster because it has that name property and now the swimming monster Creator we can just call this and we can pass through the name and 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 our 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 fly in 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 call the flier whoops flier just like that and we just need to pass in the monster and flier is gonna look just like swimming except for a flyer here is going to be slightly differently because it's going to have a five function and it's just going to say flu 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 can just come all the way back down create a new function and we're just gonna make this one whoops attacker and Walker and what this is going to do is it's 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 and now if we save it you can see that 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 swim monster we could create a flying monster that just has a fire 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 composable 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 takun walk or they can fly or any combination of those and then what we do is we 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 can 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 know you can think of a ton of different combinations and the ability to just create a new function throw in 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 remove 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 thank you very much for watching and have a good day
Info
Channel: Web Dev Simplified
Views: 130,860
Rating: undefined out of 5
Keywords: webdevsimplified, js, javascript, js tutorial, javascript tutorial, inheritance vs composition, composition programming, inheritance programming, object oriented progamming, javascript composition, javascript classes, javascript object oriented programming, oop, oop composition, composition and inheritance, composition vs inheritance programming, composition javascript explained, javascript composition tutorial, js composition, js composition explained, js composition tutorial, wds
Id: nnwD5Lwwqdo
Channel Id: undefined
Length: 10min 16sec (616 seconds)
Published: Sat Jan 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.