Тёмный

OOP Principles: Composition vs Inheritance 

Dave Crabbe
Подписаться 13 тыс.
Просмотров 52 тыс.
50% 1

This video attempts to introduce the viewer to the two major Object Oriented Design paradigms. Composition and Inheritance. In this video, I use Python for my programming language. Much of the information here came from FunFunFunction's video on this topic at:
• Composition over Inher...

Опубликовано:

 

26 апр 2018

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 100   
@new-anointingaremu3597
@new-anointingaremu3597 Год назад
Have actually watched about 10 videos for composition vs inheritance this is still the best
@davecrabbe4579
@davecrabbe4579 Год назад
thx
@error467able
@error467able 2 года назад
I am having difficulties understanding this topic, but this explained far way better than the other. Great job.
@davecrabbe4579
@davecrabbe4579 2 года назад
This was a topic that gave most of my students difficulty.. so you are not alone.
@user-qy1ny9bu1h
@user-qy1ny9bu1h 3 года назад
Finaly some good explanation what is composition! Other videos were about rooms, walls, furniture and windows...
@DerEchteAndrej
@DerEchteAndrej 5 лет назад
Thank you Dave for this video. It's the best one on this topic I've seen so far, and it helped me to understand this eventually!
@southern_smacker
@southern_smacker 4 года назад
I was watching different videos on this subject since last 8 hours but could not understand inheritance and composition. The approach and language used in this video is pretty simple and readily understandable.
@tkvenki
@tkvenki 5 лет назад
One of the most Simple and Best explanations on this topic!!
@therobby3
@therobby3 5 лет назад
Thanks for the video, very clear and the point. I've been using inheritance way too much over the years and this explains exactly what I've needed.
@jonathan10543
@jonathan10543 5 лет назад
Wow I hope you make more Python and Object Oriented Design videos! This is top-notch!
@shinkicked3686
@shinkicked3686 5 лет назад
Why not just separate behaviors in different classes? like barker, mover, eater cleaner and you compose every object base on behavior like.. dog will be compose of eater and barker objects, CleanRobot will be composed of cleaner object and so on.. the end game with the SuperRobot will be an object composed of barker, cleaner, mover and gameplayer. I think that would be a better, more expressive and clean design and is even more reusable because what if later on the dog needs to move too.. it would be ugly to compose a dog with a robot in order to make it move, instead of you compose dog with mover object that makes more sense!.. But nice video!
@milad5554
@milad5554 4 года назад
nice 👍
@Catalin52
@Catalin52 2 года назад
This is the right approach 👍
@davecrabbe4579
@davecrabbe4579 Год назад
yes.. I would tend to agree with this.. If starting from scratch this is a better way to proceed. Good thinking.
@ipodtouch470
@ipodtouch470 11 месяцев назад
Why make stuff like barker, mover, and water classes though? Why not just make them interfaces and implement them? This would also give you the benefit of polymorphism which is imo one of the main advantages of inheritance.
@angelsdemons8520
@angelsdemons8520 3 года назад
finally got the video that can explain the difference between inheritance and composition clearly , great job Mr. Dave
@surfer4987
@surfer4987 5 лет назад
Provided a quick reference with some simple and easy to understand examples. Thanks a ton, helped me with my homework
@gintautasmartinaitis4789
@gintautasmartinaitis4789 5 лет назад
You are amazing! The best video on this subject I have seen! Thank you!
@rajivtipp
@rajivtipp 5 лет назад
Very good explanation, tried to read from many sites but this 15mn video is all you need.
@houseandhobby6971
@houseandhobby6971 Год назад
Short sweet simple concise thank you. And for such simplicity one gets rewarded with alternative ideas that forget that a simple somewhat contrived example-set is needed for the original goal. I suppose as an aside for a coder to guarantee no code duplication that the objects are done away with and that all unique referenced code exists in a static function library. And then any and every object grabs an a la cart copy of what it wants to implement.
@bro_chenzox
@bro_chenzox 4 года назад
Thanks, you are the best explainer. I am not a native speaker, but there is something in your videos that is guaranteed to provide 100% absorption of information. Exactly those with a webcam.
@bro_chenzox
@bro_chenzox 4 года назад
PS. I start to understand why I will never meet you in Google, there would be a high risk of overflow of good developers, then minimizing their salaries and totally development crisis ). Thank again, Dave!
@arthurbrc_
@arthurbrc_ Год назад
"We need a robot that can clean, move, bark + play games..." 😂😂 That's what a boss would say
@Aldabil
@Aldabil 3 года назад
The real lesson I learned from this video is, people who create "this type of composition as explained in the video" clearly never heard about something called "abstraction".
@Peter-bg1ku
@Peter-bg1ku 5 лет назад
Good video. Had to watch it at x1.5, though :)
@aMulliganStew
@aMulliganStew 4 года назад
2x but that didn't bother me. I listen far more quickly than I speak.
@balamurugankalyanasundaram426
@balamurugankalyanasundaram426 4 года назад
One of the clear explanations on composition..
@mattizie91
@mattizie91 5 лет назад
I've struggled for years to understand OOP, this one video has really tied it together in a FAST and EFFECTIVE way that uses examples that demonstrate the concept beautifully. Very under-rated video.
@davecrabbe4579
@davecrabbe4579 5 лет назад
thank you for the comments
@simonsantama
@simonsantama 3 года назад
I liked this a lot. Super clear, well explained.
@aMulliganStew
@aMulliganStew 4 года назад
Thank you. I get it now. What was missing in my understanding was having functions implemented as a call-thrus to functions of member objects. I'm not sure if this'll help me on my current project, but it does give me something else to consider. Again, thanks.
@voltaralcala9021
@voltaralcala9021 4 года назад
I will be back here later, thank you for great explanation once again
@muddassirghoorun4322
@muddassirghoorun4322 3 года назад
Thanks for this awesome video. Good stuff!
@maksat607
@maksat607 5 лет назад
You are the best teacher!
@FXK23
@FXK23 5 лет назад
I'm not into python but coupling a bot to a dog (dependency) in order to let the bot 'bark' doesn't seem the right thing to do. Abstracting out Skills classes/interfaces as 'in between models' would make more sense and decouples bots from dogs. Both animals and bots can then have some collection of skills to be invoked.
@yonishachar1887
@yonishachar1887 3 года назад
can you give an example?
@MichaelLamTheDeveloper
@MichaelLamTheDeveloper 3 года назад
Completely agreed, I knew what the video was trying to get at but the way the classes were used was weird. Using the example in the video, the SuperBot HAS A Dog, Robot and CleanRobot
@yonishachar1887
@yonishachar1887 3 года назад
@@MichaelLamTheDeveloper Hey, can you please provide an example for what you'd consider a good refactor of the video?
@MichaelLamTheDeveloper
@MichaelLamTheDeveloper 3 года назад
@@yonishachar1887 If SuperBot wants to bark, it shouldn't need to purchase a dog. As a SuperBot, it has a voice, I would create an interface called IVoice with a method called "Speak" inside. I would then create a concrete class that inherits from IVoice called "Bark". Within the Bark class I would implement the method "Speak" and return "bark bark!". So a SuperBot has a voice (IVoice), and when it "Speak" it'll say "bark bark!". I want to emphasize that SuperBot has a reference to "IVoice" and NOT the "Bark" class. This allows for polymorphism, we can change the Voice without changing the SuperBot class. Take it with a grain of salt, I'm learning as just as you are. I'm sure there are better approaches to this and hopefully, other members of the community can chime in. This is what I think would be better and clearer
@prototype8861
@prototype8861 3 года назад
@@yonishachar1887 interfaces. ICanBark, ICanMove etc.
@sergiomoreno9223
@sergiomoreno9223 4 года назад
Thanks a lot, the best video of this topic.
@csquared9
@csquared9 2 года назад
Excellently described!!!
@TuanTran-jn5gs
@TuanTran-jn5gs 4 года назад
Thank you so much Dave!!!
@vineets176
@vineets176 3 года назад
Made the concept clear. Keep it up.
@LuisAguilar-hp3lq
@LuisAguilar-hp3lq 2 года назад
Totally clear!! Thanks
@nikokodzhabashev807
@nikokodzhabashev807 6 лет назад
Thank you! Amazing content
@CaptainBravo87
@CaptainBravo87 Год назад
Amazing and simple!
@z4vdi20
@z4vdi20 6 лет назад
Really helped me. Thanks a lot!
@milad5554
@milad5554 4 года назад
Well and simple described , Thank you
@mattt2684
@mattt2684 4 года назад
Amazing explanation!
@rahulagarwal7182
@rahulagarwal7182 3 года назад
Great VIdeo..How easily you explained it is awesome..Great work and Thanks for video
@sarahpes32
@sarahpes32 3 года назад
Thank you Soooo much!! Best explanation!!! 💙👍
@aqilisayev5529
@aqilisayev5529 3 года назад
Thanks for explanation!
@ahmetkarakartal9563
@ahmetkarakartal9563 2 года назад
thank you so much, this explanation is very clear
@Mrmtl100
@Mrmtl100 3 года назад
great explanation!
@prototype8861
@prototype8861 3 года назад
I want my 15 minutes back. He just literally stuffed an entire dog inside the robot so the bot can bark. Evil.
@seano.2267
@seano.2267 4 года назад
wow, i am the 10000th viewer. Great video, really helps, thank you.
@sadam_khan
@sadam_khan Год назад
Brilliant.......
@hassansakr4993
@hassansakr4993 5 лет назад
great video thanks a lot
@machinimaaquinix3178
@machinimaaquinix3178 Год назад
Sept 2022 - This was a fantastic example of composition. You can't believe the abstract and academic word salads that are in various texts and some other YT vids. This explained the scenario so clearly, I finally got this, and on a day I got half as much sleep as normal lol. Regarding some other comments about a better way to do this by creating new specialist classes based on the actual functions, eg, "Bark", "Move" and "Clean" class ... - they are valid points but I think Dave was explaining this from the perspective of reusing existing classes - which is often a requirement, especially if you don't own or cannot modify the code. If one could re-model the domain, I would definitely use specialist classes and then compose a new class with them. Last comment regarding the three return functions, they are not really needed as you can access them after instantiation like so "megatron.o1.move()" - Idk if this is good python form or not - just saying they can be accessed like this.
@PTM1008
@PTM1008 Год назад
megatron.o1.move() as you suggest doesn't sound right to me. What if o1 (dog) has a method called poop()? Being a machine, megratron won't be able to poop()... Hence, it is important for megatron to only expose the required functionality. It may not be a hard and fast rule. There might be scenarios where your suggestion will make more sense.
@moazelsawaf2000
@moazelsawaf2000 4 года назад
Thank you so much sir
@cibelless510
@cibelless510 3 года назад
very gooooooddddddd you are very clear
@edinetgrunhed6000
@edinetgrunhed6000 3 месяца назад
this is it folks!!
@PETERTRITSCH
@PETERTRITSCH 2 года назад
great !!!
@akio3524
@akio3524 3 года назад
thx so much!!!!!!!!!!!
@user-hi9dd4dl9q
@user-hi9dd4dl9q 4 года назад
Thanks
@banamali143
@banamali143 Год назад
Very nice👍
@Kawlinz
@Kawlinz Месяц назад
At around 12:00, in your class of Superbot, you directly reference three of their classes in the creation of the superbot... So I'm just learning and this is the part that I'm having trouble with. I thought a class wasn't supposed to reference other classes, but that makes it super difficult to do really simple things (in my example, I've made a nuclear power plant object, that when it is created it takes a certain amount of time to "power up" and then it creates a signal, which is a reference to another class, which is a no no). Is the proper way to use some sort of interface? Any advice is appreciated.
@rhkina
@rhkina 3 года назад
Hi, Dave! Thank you for your video! It helped me a lot to study about inheritance and composition, but I understand the approach should be different. What about creating a class of actions and use them inside the objects as Matt suggests?
@davecrabbe4579
@davecrabbe4579 3 года назад
I don't see a reply here from a Matt.. so not sure of the details
@d0opify
@d0opify 3 года назад
how u handle states, owned by super but reflected in sub? ex varialbe for what to woof!
@eidiazcas
@eidiazcas 4 года назад
I could have created a class called eater which contains that method eat, meower only with meow(), and barker with bark() only and make cat and robot cat inherit from those classes. How is composition different from multiple inheritance then?
@davecrabbe4579
@davecrabbe4579 4 года назад
True, but that is not how classes get created. Classes, in OOP, generally all have lots of methods. The idea is to reuse methods of classes that you’ve already created without loading the entire class and inheriting all those classes you don’t need (and which could cause bugs).
@josephlyons3393
@josephlyons3393 3 года назад
Nice video. Just as a heads up, I don't think the return statements in the SuperBot class methods were needed.
@machinimaaquinix3178
@machinimaaquinix3178 Год назад
Yes this puzzled me as well. Fwiw if anyones reading this past Sept 2022, the current version of python I tried this on (3.10.6) does not require all the function calls in Superbot and the methods can be accessed with this code: megatron = Superbot(): megatron.o1.move() megatron.o2.bark() megatron.o3.clean() megatron.play_games()
@paragkadam2169
@paragkadam2169 3 года назад
Great video.. I noticed you did not show how you handled the duplication that was introduced by the eat() method after you converted your solution to composition, can you please tell me how to managed the duplicate eat() method in the dog and cat classes?
@davecrabbe4579
@davecrabbe4579 3 года назад
I didn't show the eat() method for the dog/cat in the 2nd example, but one way to handle it is to create a new class called Animal with the eat() method. Here in animal you might put any method that could be used by multiple classes that you might want to create later. So you've only create the eat() method in one place. To add the eat() method to Superbot() you would add the lines: self.o4=Animal() and the method: def eat(self): return self.o4.eat()
@ajaybhandari5180
@ajaybhandari5180 6 месяцев назад
I dont get it , isnt that violating the SOLID principle of DIP? why don't use abstractions for Dog, Cat, CleanRobot class and use these in a constructor of main class? correct me if I am wrong please.
@milad5554
@milad5554 4 года назад
As i Understood, One of the benefits of using composition over inheritance is , from examples of this video , in inheritance when boss says "hey i need a SuperBot with Bark ability" , we can import whole Dog class into new SuperBot Class and its seems works ,but we are importing other abilities of Dog that we don't need them . so it may cause speed and memory problems . But in composition we just import the ability that we need. Is it true ?
@barsidemen5208
@barsidemen5208 4 года назад
But here when we initialize the Dog object (self.01 = Dog()), aren't we importing all its method as well?
@milad5554
@milad5554 4 года назад
@@barsidemen5208 I think no , because when we use composition we define objects by their abilities and we use abilities instead of whole object
@prashantbajpai2142
@prashantbajpai2142 4 года назад
Yes...you are right @Milad Jafari, the possible solution to send Bark method from Dog class into SuperBot class as a method parameter. (Method as a function pointer with the help of delegates). I hope it makes sense.
@milad5554
@milad5554 4 года назад
Prashant Bajpai 👍🙏
@freemanfreed1581
@freemanfreed1581 2 года назад
Subscribed !!! I need delegation in python
@Lavasea
@Lavasea 4 года назад
I think a better implementation will have the cleaner robot class named cleaner, because all it does is clean, and it represents something that can clean. Then if you want to implement the original cleaner robot that can both clean and move, you can make a new class called CleanerRobot and have both a cleaner and a robot class in it.
@davecrabbe4579
@davecrabbe4579 Год назад
yes.. this comment has come up before and it is a cleaner (excuse the pun) and more self-documenting method for the design.. the details are the same....
@houseofcodingwithnayan5937
@houseofcodingwithnayan5937 2 года назад
awesome explanation
@mr.anderson5393
@mr.anderson5393 4 года назад
Sir why did you use o1, o2, o3 with superRobot? Very good video sir.
@pianopat6924
@pianopat6924 3 года назад
I think he used them to assign Dog, Robot and CleanRobot class to have access to their methods. superRobot uses these methods in defining its own move, bark, and clean methods. In short, so that he can make superRobot with these behaviors without redefining it's own move, bark, and clean methods. Even shorter - re-using code definitions in other objects without inheritance.
@JustAnotherAlchemist
@JustAnotherAlchemist 4 года назад
Are you monologing some well known source? I ask, because your video is almost verbatim identical to Fun Fun Function's video on the same subject from three years prior.
@davecrabbe4579
@davecrabbe4579 4 года назад
I did research from a bunch of different sites as there was not much information available on illustrating the concept of Composition.. Just watched the site you mentioned and no doubt I used much of that material for my in-class lecture.. I didn't realize I had used so much of it and I'll credit it in my description.
@rob876
@rob876 3 года назад
Wouldn't you say that the recommendation to prefer composition over inheritance is the consequence of only having single inheritance and not multiple inheritance in some defective OO languages like Java and c#? With only single inheritance, it's better not to use the feature at all in that language - instead, roll your own multiple inheritance using composition.
@davecrabbe4579
@davecrabbe4579 3 года назад
When you only use inheritance, you can often get a pile of things inherited that you didn’t plan. This can happen in any proper OOPL, such as Python. Using Composition only gets those methods that you need and keeps everything cleaner. Certainly Java had OOP tacked on and is more complex for many reasons.. such as the difference between primitives and object data types.. But composition is not something that is relative to the language. Even fundamentally object oriented languages will benefit from composition design where appropriate.
@rob876
@rob876 3 года назад
@@davecrabbe4579 Thank You for your reply. I agree. I would only inherit from a class I was very sure of - such as one I'd recently written. If not, I'd wrap it and make sure not to reach into its member variables. On the other hand composition gives you so much more flexibility.
@unabonger777
@unabonger777 5 лет назад
a pair abouts?
@davecrabbe4579
@davecrabbe4579 4 года назад
a pair of Bots?
Далее
OOP Principles: Overloading
11:29
Просмотров 991
The Flaws of Inheritance
10:01
Просмотров 911 тыс.
Understanding Java Composition Part 1
17:17
Просмотров 30 тыс.
Composition over Inheritance
8:34
Просмотров 509 тыс.
Fundamental Concepts of Object Oriented Programming
9:16
Simulating the Evolution of Rock, Paper, Scissors
15:00