No video

Kotlin Code Reuse: Composing like you're Inheriting

  Рет қаралды 7,830

Dave Leeds

Dave Leeds

Күн бұрын

The classic Design Patterns book introduced the principle of favoring composition over inheritance. In this video, we'll explore what the authors meant by this principle, we'll consider the characteristics that are affected by it, and we'll look at an underrated Kotlin feature that can change our perspective on it!
✨ New to Kotlin? Start your journey here: typealias.com/...
... or pick up the new Leanpub Edition for offline access and more! book.typealias...
🚀 Get the inside scoop on everything I'm working on - join my new email newsletter!
Sign up here: newsletter.typ...
📘 Chapter 13 - Introduction to Class Delegation
typealias.com/...
🎥 Got a question for the livestream?
forms.office.c...
🎞️ Chapters in this video
00:00 Introduction
00:47 Inheritance
04:02 Composition
08:20 Trade-Off Considerations
11:00 Composing like you're Inheriting
12:14 Limitations
13:20 Wrap-up

Пікірлер: 43
@codingCouncil
@codingCouncil Ай бұрын
Dave I love your videos and out of the millions out there , your way of explaining things stands out . Please keep them coming
@typealias
@typealias Ай бұрын
That's very kind of you to say - thank you so much! I'll keep at it!
@serrrsch
@serrrsch Ай бұрын
I gotta say I'm kinda jealous of the newcomers who are getting into programming / computer science today. Only ten years ago this quality in a lesson was not available to me on YT or similar platforms ~FOR FREE~. Big up for the outstanding video!
@typealias
@typealias Ай бұрын
Yes, it's quite a different world, for sure! I'm honored (and encouraged!) that you found this lesson to be of that level of quality!
@QuantuMGriD
@QuantuMGriD Ай бұрын
At last! patterns starting to emerge in the channel. Thank you so much! 😊
@typealias
@typealias Ай бұрын
Hey, you're most welcome! I'm glad you mentioned it last time - there were enough likes on those comments that I couldn't ignore it! 😁
@QuantuMGriD
@QuantuMGriD Ай бұрын
😊❤
@robchr
@robchr Ай бұрын
Go lang is statically typed and it does allow for implicit interfaces. It''s because Kotlin is statically typed using a nominative type system. This is why it why you need to explicitly specify the relation.
@typealias
@typealias Ай бұрын
Thanks Robert - that's a great clarification... it's not just the static typing. TIL Go is structurally typed! Might have to play with that at some point 👍
@brunojcm
@brunojcm Ай бұрын
Go and Typescript are both structurally typed and Kotlin uses a nominal type system, but all of them are statically typed. This is something people rarely talk about, maybe a video about it would be nice!
@ErikBongers
@ErikBongers Ай бұрын
Pros and cons over dogmatics, thank you! The 'by' keyword in Kotlin is indeed one of their great syntax sugars.
@alanmeanam
@alanmeanam Ай бұрын
This is the best explanation to this principle I have ever seen, thanks!!!
@guyguy467
@guyguy467 Ай бұрын
Very nice explanation. Thank you
@BewareOfStinger
@BewareOfStinger Ай бұрын
Thank you, Dave! Superb video as always. Keep them coming! :)
@vyrus507
@vyrus507 Ай бұрын
Just bought the book, was gonna get it eventually but this one sold me, great vid!
@typealias
@typealias Ай бұрын
Hey, thank you so much! I hope you enjoy the book! 🙂
@osisuper98
@osisuper98 Ай бұрын
No one explains anything better than Dave, omg.
@harsh3305
@harsh3305 Ай бұрын
Crispy clean explanation
@pablovaldes6022
@pablovaldes6022 Ай бұрын
So for proxy classes or to implement the proxy object pattern I can't use the class delegation, one has to manually forward every function call to whatever is the current proxy implementation. 😢
@mohammad-rezaei2018
@mohammad-rezaei2018 25 күн бұрын
As always excellent
@typealias
@typealias 24 күн бұрын
Thanks so much, Mohammad!
@youssefhachicha-nj6wf
@youssefhachicha-nj6wf 8 күн бұрын
great video
@westforduk
@westforduk Ай бұрын
Great as usual. Thanks Dave :)
@typealias
@typealias Ай бұрын
Thanks so much!
@EugeneGalonsky
@EugeneGalonsky Ай бұрын
There's a mistake in Chapter 13 in the Waiter's UML box: Waiter+ + prepareEntree(name: Entree): Entree? Should be: + prepareEntree(name: String): Entree?
@typealias
@typealias Ай бұрын
Thanks, Eugene! I'll get that fixed up. 👍
@MoamenHraden
@MoamenHraden Ай бұрын
Thanks
@typealias
@typealias Ай бұрын
Hey Moamen! Man, thank you so much for the SuperThanks! I'm excited about growing the channel and the community - and your support is a big encouragement!
@ulicqueldromal
@ulicqueldromal Ай бұрын
About the ackwardness of IVehicle and Vehicle. It's pretty obvious here why this naming is suboptimal. All of the cars are Vehicles. Yet the thing called Vehicle is just one example of a vehicle. Why is that one called a Vehicle but not the others? The interface should be called Vehicle and this Base subclass should get a name fitting your domain. Since this is just an example you might end up with a name like BaseVehicle but in a well defined domain this would have a better name.
@mwatkins0590
@mwatkins0590 Күн бұрын
why not just call the interface Drivable, since thats the point of it?
@wagnerarcieri
@wagnerarcieri Ай бұрын
if Junker has 'makeEngineSound() = Unit', why it printed "Vroom! Vroom!" ? while 'accelerate() = Unit' returned speed as 0.0
@typealias
@typealias Ай бұрын
In the example at 9:05, it's important to note that raceCar2 isn't a Junker; it's a RaceCar that wraps a Junker (line 36). It delegates speed and accelerate() to the Junker (lines 27-28), but it provides its own implementation of makeEngineSound() (line 29). This is roughly the same idea as if RaceCar were to inherit from Junker and override only makeEngineSound().
@wagnerarcieri
@wagnerarcieri Ай бұрын
@@typealias Oh! I get it now! Thanks for your kindness to explain!
@typealias
@typealias Ай бұрын
🎉 That's great! Happy to do so!
@Kubkochan
@Kubkochan Ай бұрын
It would be much nicer to have engine in composition. This kind of composition looks too unnatural
@ArthurKhazbs
@ArthurKhazbs Ай бұрын
Yes! I wanted to write that comment too.
@typealias
@typealias Ай бұрын
Hey, thanks for commenting! Yes, it can look unnatural - mostly because it's easiest for us to map our notions of real-world object relationships onto software models - for example, RaceCar "is a" Vehicle, and Vehicle "has a(n)" engine. Many of us learned that kind of mapping early on, and plenty of successful software systems have been largely designed around it. It's helpful because one of the most important characteristics of code is for a human to readily understand it. That shouldn't be our only lens, though. There are additional characteristics (flexibility, performance, scalability, security, etc.) that we should consider, and to understand those, we have to ask what it is that we gain or lose by constructing the relationships one way compared to another (e.g., inheritance vs. composition, recursion vs. iteration, and so on). That's what I hoped to achieve in this video - to demonstrate that inheritance can also be expressed with object composition or class delegation, and to consider the trade-offs involved with each approach.
@aungkhanthtoo7678
@aungkhanthtoo7678 10 күн бұрын
Dave, may I know the name of font you used?
@typealias
@typealias 9 күн бұрын
Hello! Are you referring to the font on the thumbnail image? If so, it's called Luckiest Guy: fonts.google.com/specimen/Luckiest+Guy
@aungkhanthtoo7678
@aungkhanthtoo7678 9 күн бұрын
@@typealias Sorry, I meant font using in the IDE.
@typealias
@typealias 8 күн бұрын
Ah, yes - that's using JetBrains Mono: www.jetbrains.com/lp/mono/
@j2shoes288
@j2shoes288 Ай бұрын
we in 1990s?
Only Use Inheritance If You Want Both of These
9:10
Christopher Okhravi
Рет қаралды 15 М.
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 62 МЛН
ОБЯЗАТЕЛЬНО СОВЕРШАЙТЕ ДОБРО!❤❤❤
00:45
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 99 МЛН
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 43 МЛН
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,6 МЛН
HTMX Sucks
25:16
Theo - t3․gg
Рет қаралды 117 М.
Type-Safe Navigation In Jetpack Compose
4:20
Daniel Atitienei
Рет қаралды 2,4 М.
Intro to Collection Builders in Kotlin
11:52
Dave Leeds
Рет қаралды 4 М.
The World Depends on 60-Year-Old Code No One Knows Anymore
9:30
Coding with Dee
Рет қаралды 738 М.
Variance... without Generics!
9:01
Dave Leeds
Рет қаралды 6 М.
Why Favor Object Composition Over Class Inheritance? A Deep Dive
19:00
*Next-door 10x Software Engineer* [FULL]
4:50
Programmers are also human
Рет қаралды 501 М.
Compose UI for... a Light Switch | Jake Wharton
47:02
Kotlin by JetBrains
Рет қаралды 11 М.
Inline Functions: inline, crossinline, and noinline
11:59
Dave Leeds
Рет қаралды 4,5 М.
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 62 МЛН