Composition Over Inheritance

  Рет қаралды 17,743

Awesome

Awesome

Күн бұрын

The advantages of Composition over Inheritance
💬 Topics:
- What is Composition?
- What is Inheritance?
- Class-based vs Prototype-based Inheritance;
- Composition in Go;
- Inheritance in Kotlin;
- Classes vs Types vs Interfaces;
- Go Type Embeddings;
- Kotlin vs Go;
📖 Blog Article - www.awesome.cl...
🥇 Become a Member - / @awesome-coding
✉️ Join the Newsletter - newsletter.awe...

Пікірлер: 54
@larsartmann
@larsartmann 4 ай бұрын
You can use "by" in Kotlin e.g.: class CounterSet( private val innerSet: MutableSet = mutableSetOf() ) : MutableSet by innerSet { //only new and or overwrite func's }
@julian_handpan
@julian_handpan 4 ай бұрын
In OOP dependency injection + composition. 😌😘👌🏻
@carlosmspk
@carlosmspk 4 ай бұрын
You're throwing away 25% of OOP by doing that. Not saying it's a bad thing, but can it still be called OOP?
@plaintext7288
@plaintext7288 4 ай бұрын
​@@carlosmspkwhy not? A subset of c++ is still the same language :)
@carlosmspk
@carlosmspk 4 ай бұрын
@@plaintext7288 C basic is not C++ I know over the years C++ changed to a point where it no longer is a superset of C, but even when it was, they were different languages. Either way, you're talking about languages. I was talking about the core principles of OOP, if you throw one of them out the window, I'd argue it's no longer OOP
@julian_handpan
@julian_handpan 4 ай бұрын
@@carlosmspk that’s not true my friend. Because OOP means objects sending messages, so is all objects. Even if the object is a composed object or a dependency object, still object talking to objects.
@carlosmspk
@carlosmspk 4 ай бұрын
@@julian_handpan hah, you're coming from Smalltalk. Alright, makes sense, then
@atlantic_love
@atlantic_love Ай бұрын
Maybe learn a little more about inheritance before you start blabbering nonsense. The car, minivan and truck are subclasses of Vehicle. It's not the first two being a subclass of the last, or the first one being a subclass of the second.
@shikanokonokokoshitantan
@shikanokonokokoshitantan 4 ай бұрын
May not be the first but heyy 2nd!
@pokefreak2112
@pokefreak2112 4 ай бұрын
The cleanest implementation of composition I've come across is the one in Odin and Jai. They let you put a 'using' keyword in front of any field, variable, or namespace to make its members directly accessible in the current scope. So not only can you use it in structs/classes, you can also use it in fuctions to essentially destructure the object or in namespaces to compose a larger API from smaller API subsets
@ProfessorRS
@ProfessorRS 4 ай бұрын
If I understand correctly, what you're describing is very similar to the 'with' keyword in kotlin
@pokefreak2112
@pokefreak2112 4 ай бұрын
@@ProfessorRS Had to look it up, but looks like it yeah! Except apparently *with* is a function in kotlin and not a keyword. If you could do 'with var app = makeApp()' it would basically be equivalent for the function use case
@netssrmrz
@netssrmrz 4 ай бұрын
Nice video but I didn't enjoy the cognitive load of switching between languages (Java vs Go) and data models (shapes vs cars). Also, for me, not detailing composition cons and inheritance pros is a red flag. Personally, knowing when to use each method effectively makes me a better dev than fanboys trying to shoehorn composition into everything.
@awesome-coding
@awesome-coding 4 ай бұрын
Noted! Thank you for pointing out these issues. Clearly I'll have to do a better job next time.
@bonquaviusdingle5720
@bonquaviusdingle5720 25 күн бұрын
“Composition over inheritance” is what a lot of beginners tend to say. Which to use depends entirely on the use case and design pattern.
@dei8bit
@dei8bit 3 ай бұрын
composition makes you happy 🙂 the inheritance, only when your parents were millionaires and considerate
@awesome-coding
@awesome-coding 3 ай бұрын
I really need some millionaire parents 😅
@dei8bit
@dei8bit 3 ай бұрын
@@awesome-coding well, composing them will probably be possible in the future(?
@awesome-coding
@awesome-coding 3 ай бұрын
@@dei8bit haha!
@HermesSoilder
@HermesSoilder 2 ай бұрын
Great video! I’m teaching my self rust coming from typescript and this makes traits much more clear.
@awesome-coding
@awesome-coding 2 ай бұрын
Glad to hear!
@alinghinea98
@alinghinea98 4 ай бұрын
Great video 👏
@maxamed14
@maxamed14 2 ай бұрын
No wonder Go is trending so hard! Amazing video as always mate!
@awesome-coding
@awesome-coding 2 ай бұрын
Glad you liked it!
@fakedevdutt
@fakedevdutt 4 ай бұрын
damn the languages are hard
@awesome-coding
@awesome-coding 4 ай бұрын
Which one?
@VigneshwarDev
@VigneshwarDev 4 ай бұрын
@@awesome-coding which one is not ?😅 just kidding bro
@awesome-coding
@awesome-coding 4 ай бұрын
@@VigneshwarDev 😅
@gonzo191
@gonzo191 4 ай бұрын
Not sure why we switched over to Go to demo composition, when the same could've been done in Java (OOP languages) but that's beside the point. These arguments oftentimes glorify composition as if it doesn't have any downsides when compared. Inheritance (Pros: Reusability, Polymorphism, Code organization | Cons: Tight coupling, Fragility, Inflexibility), Composition (Pros: Flexibility, Loose coupling, Code reuse | Cons: Complexity, Overhead (Memory), Code organization). Inheritance should be used when there is a direct class relationship whilst composition should be used when there is a need for more dynamic behavior via loose class coupling.
@awesome-coding
@awesome-coding 4 ай бұрын
Hey! I'm not sure why composition would require more memory overhead? We are talking about the same data being stored in the memory, right? I switched to Go for two reasons: 1. To bring on the argument that some languages don't allow inheritance at all since the language designers felt like the cons outweigh the pros. 2. I really like the inner type promotion aspect of Go, and I believed it would be an interesting detail when it comes to different ways in which software could be written. But to your point, yes, you are right, composition can be achieved in any OOP languages as well.
@carlosmspk
@carlosmspk 4 ай бұрын
@@awesome-coding you also added the disclaimer that this is a segment called "The Snippet", but I'd argue it doesn't work very well. If you're trying to explain a concept and change languages midway two things happen: 1-Any viewer not familiar with one of the languages will have trouble understanding that part 2-Even viewers that are familiar with both languages, will have some mental burden to see the conceptual differences of what you're trying to show, because the actual differences from the language itself (syntax, strucutre, etc.) will be extremely noticeable. I understand the idea of having multiple languages to kind make the concept come accross in a more "pure", language-agnostic form, but I don't think it works very well. Just my 2 cents.
@awesome-coding
@awesome-coding 4 ай бұрын
@@carlosmspk Hey! I really appreciate your feedback, and I actually believe you are right on this one. The context switching between languages makes things harder to follow along. I'm still exploring and testing this "The Snippet" concept, so your feedback is very valuable. Thank you!
@kevinb1594
@kevinb1594 4 ай бұрын
@@awesome-coding As someone who didn't know the first language and is only vaguely familiar with GO, I wanted to tell you that you did just fine and it was understandable. I think the only sticking point is that the viewer needs to understand how GO doesn't use a keyword for explicitly stating the association of methods to structs - though you did state it if they don't 'get' it then the rest of the explanation could be lost.
@awesome-coding
@awesome-coding 4 ай бұрын
@@kevinb1594 Thank you for your input!
@krtirtho
@krtirtho 4 ай бұрын
Ok, got it. Now spit out the cons of Cumposition. The memory overhead, complexity etc...
@awesome-coding
@awesome-coding 4 ай бұрын
Hmm I'm not sure why people are mentioning memory overhead in this scenario. Are talking about the references to the type embeddings? I'm pretty sure that's highly optimised. When it comes to complexity, the same argument can be made about Inheritace. As I said, Inheritance is not bad, it just has very specific use-cases, and is not a good idea to always use it when code reusability is required.
@bear458ziif-s
@bear458ziif-s 4 ай бұрын
never used java before, but for some reason i always assumed that composition wasn't even a thing in java. obviously it's just a pattern but i've never seen it before in java and it has a reputation of being _the_ oop language. i associate inheritance with oop more than i do with composition.
@awesome-coding
@awesome-coding 4 ай бұрын
Yep exactly! This is the miss conception in OOP, because the OOP principles are usually enforced.
@kevinb1594
@kevinb1594 4 ай бұрын
Unrelated: OOP in JS is so disgusting.
@awesome-coding
@awesome-coding 4 ай бұрын
fact!
@Laflamablanca969
@Laflamablanca969 4 ай бұрын
Js in general is disgusting
@theklr
@theklr 4 ай бұрын
🔥🔥🔥
@antibioticaddict
@antibioticaddict 4 ай бұрын
Or just use interfaces
@awesome-coding
@awesome-coding 4 ай бұрын
Right... but languages don't allow state in interfaces most of the time so how would you reuse state?
@TechBuddy_
@TechBuddy_ 4 ай бұрын
let's goooooo
@awesome-coding
@awesome-coding 4 ай бұрын
✌️✌️✌️
@shikanokonokokoshitantan
@shikanokonokokoshitantan 4 ай бұрын
May not be the first but heyy 2nd!
@awesome-coding
@awesome-coding 4 ай бұрын
👍👏🏻
Software Development Life Cycle: Explained
12:31
AltexSoft
Рет қаралды 44 М.
Worst flight ever
00:55
Adam W
Рет қаралды 27 МЛН
АЗАРТНИК 4 |СЕЗОН 3 Серия
30:50
Inter Production
Рет қаралды 1,1 МЛН
Стойкость Фёдора поразила всех!
00:58
МИНУС БАЛЛ
Рет қаралды 3,6 МЛН
Only Use Inheritance If You Want Both of These
9:10
Christopher Okhravi
Рет қаралды 17 М.
Why Favor Object Composition Over Class Inheritance? A Deep Dive
19:00
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 82 М.
The Simplest Tech Stack
9:38
Awesome
Рет қаралды 118 М.
What is the Java Job delusion?
12:23
Stefan Mischook
Рет қаралды 123 М.
Mastering Inheritance By Struct Embedding In Golang
13:05
Anthony GG
Рет қаралды 30 М.
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 945 М.
Why Rust is NOT a Passing Fad...
8:54
Travis Media
Рет қаралды 37 М.
STOP Using Classes In JavaScript | Prime Reacts
14:02
ThePrimeTime
Рет қаралды 240 М.
Composition over Inheritance Explained by Games! #programming
8:10
Metaphorically Speaking
Рет қаралды 11 М.
Worst flight ever
00:55
Adam W
Рет қаралды 27 МЛН