Composition Is Better Than Inheritance in Python

preview_player
Показать описание

In this video, I explain the difference between composition vs inheritance, what the design principle "favor composition over inheritance" means in practical terms, and how you can use it to make better software design decisions.

🔖 Chapters:
0:00 Intro
1:11 Explanation of the example
3:57 Technique #1: inheritance
8:30 Problems with the inheritance technique
10:02 Technique #2: composition
18:48 Improvement: turn Commission into an abstract class
20:03 Limitations of this example
21:29 Final thoughts

#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

Counter-argument: I'd rather get an inheritance from a deceased relative than a composition, no matter how good at poetry they were.

AzureFlash
Автор

I love it that your videos are somewhat advanced, there are not a lot of these on Youtube. Keep up the great work!

ShlomoGottlieb
Автор

I really like how you make small incremental changes, and run/test the code after each iteration.

MichaelFJ
Автор

Been listening to the audio book of a Pragmatic Programmer and in the book they emphasize that "has a" is a lot better than "is a". Actually doing it in code is a different story. Seeing it here it makes a ton of sense! Thanks for the vid!

michaelyost
Автор

Man your videos are so high quality! Sadly I don't think you're gonna grow fast 'cause advanced topics have a smaller audience. I really hope you keep the videos coming anyway. Thanks for the content!

cristobaljvp
Автор

Thank you! Finally a more advance level of Python programming and specifically dealing with classes. A lot of good information, I'll need to watch this again! I just subscribed!

mauisam
Автор

OOP composition has not been covered enough, so thank you for creating this video!

reddit
Автор

the only video I really understood. It is important to know the real problem before thinking how to write the code.

nianeddavid
Автор

Arjan your channel is just great! Thanks for this material :)

nato.musica
Автор

Would love to see some creational patterns as the continuation of this video when that main() gets crowded because there are many parts to create before instantiating The Employee or when you need to decide which Employee to create during runtime. P.S. Please keep up this great work! In python you have either hello_world() tutorials or data science ML. Your videos are like a fresh air in Python world! ;]

freacas
Автор

This video is great. What is not mentioned in it is that it also conforms the single responsibility principle to separate different kind of responsibilities into different interfaces/classes to provide a flatter structure instead of having a fat class or a very deep inheritance relationship that doing everything, which also make it improves the cohesion of the class. I did find it is quite interesting that when people are talking about a good design pattern from one aspect, its goodness will also make it to conform the other coding principles automatically. Great job 👍

yishan
Автор

Very nice content ! One more thing you could add to this codebase is a NoCommission class that returns 0 when get_payment is called. With this class there is no need for "Optional Commission" in the Employee class and the concept of "there is no commission" has now a concrete representation.

laurentverge
Автор

I generally find it helpful to approach my Object model from an RDBMS mindset. The Employee class might be better treated as a Personnel record containing only that information needed to identify a person. That person's relationship to the company, pay structure, and other factors are defined by other related data structures.

WthyrBendragon
Автор

Man! you are great with your videos, most of the people really don't include the incremental changes while making the code and sometimes it adds a lot of complexity when you are studying way complex things but in your video things start to make much sense as we do see the changes happening and it helps in understanding the stuff very well. Really awesome videos!!

amitsinghsansoya
Автор

Fantastic content, clear explanations, and great editing. Subscribed!

Wahlnetwork
Автор

This channel has been a game changer for my coding skills.

jampk
Автор

3:05 That's a perfect example for mixins: shared behaviour without class hirachy.

Elite
Автор

I love your typing speed and sound of it. Amazing content. Subscribed!!

noblessetech
Автор

Great video.
For those who want a simple sentence to sum up composition -
Composition is dividing different class responsibilities to abstract interfaces stored inside the class
as opposed to defining these responsibilities through inheritance.

magenertech
Автор

Watching you is intellectually stimulating....what a relief to find such an amazing and ADVANCED content

maroben