Loading
Current section: 4. Composition vs Inheritance 4 exercises
lesson

Intro to Composition vs Inheritance

Loading lesson

Transcript

00:00 Okay. Now we're gonna talk about composition versus inheritance. I'm a fan of composition. We're just gonna come out with that right now. You can have inheritance where you're extending, extend, extend, and now you're lost.

00:10 Where is that method coming from? And TypeScript helps with it a bit because you can just, like, option click on it, and you can find exactly where it is. But even still, I think composition's better. But, there are some instances where it just makes sense to use inheritance. So sure.

00:25 Inheritance is a is a relationship, is a. So we've got this animal, we have a dog, a dog is a animal. So we're using extends. Composition on the other hand is a has a relationship. So we have this engine, a car has an engine, and, and so it doesn't inherit those things, but it can use the methods from, its parent.

00:47 You you look at this and you're like, Kent, that's a little bit more actual, like, work that I gotta do. Whereas this, the dog can make a sound because the animal has it. Well, the dog would have to override it anyway because dogs make unique sounds. But, anyway, point being, yes, it is gonna be a maybe a little bit more work in some cases, but to me, it's a lot more clear, seeing where things are coming from, and I like clarity over whatever else the, complexity, I guess. There are actually three types of composition.

01:18 So, you can have inheritance, so the isa with extension. That's actually, inheritance not composition, we're talking about versus. But, three things that we're gonna talk about. We have object composition, so that has a relationship, and then you have function composition. This is my favorite.

01:36 This is transform. So you combine multiple functions into pipelines and then you take objects through those, those different functions to perform whatever transformations you wanna perform on them. So, here is our object oriented composition and that's what we're gonna focus on in this exercise because this workshop is about object oriented stuff. But in functional composition, you can have, actually and this is kind of interesting, you can have a function that returns a function and that function does something. And so you can create, send email logger and when you pass the logger to this send email function, you get back an email that Or a function that you can then pass an email to.

02:19 So that's one mechanism for functional composition. I actually don't do that specifically a lot. Pretty much most of the stuff that I'm doing is here's some objects and here's some functions that trans that operate with those objects and that's pretty much it. It ends up being pretty simple. So, yeah, we we've got inheritance, like, when you have something that very clearly is a thing, then, that could be helpful.

02:43 This manager is an employee. I'm not so sure. Like, it kinda it really depends. You gotta often you will try to do do it both ways and choose the one that you hate the least. And composition, car has an engine, it's not an engine.

02:55 Computer has a CPU, it's not a CPU. User has a profile, you know, etcetera etcetera etcetera. Alright. I think that is enough for you to get going on this exercise. So good luck.

03:06 We'll see you in the exercise.