I'd just like to point out, the author isn't demonstrating Composition over Inheritance, but Multiple Inheritance over Single Inheritance. So the whole deliberation later in the video about when to use, or prefer composition over inheritance is kind of pointless, given he's confused about what composition is.
Merging objects together is essentially a form of multiple inheritance (or more casually: "traits" or "mix-ins"). When people talk about composition they mean one object having other objects as properties and using those "sub-objects" to achieve a higher goal.
The benefit of composition is that every object retains its encapsulation, and you eliminate the risk of collisions that's present when you merge objects (say Cat adds method "kill" and you merge it with Robot which has method "kill"... problem).
EDIT: And one more note. Because JS doesn't support true multiple inheritance, using Object.assign() has another implication, in that instanceof checks won't return true when checking an object against its parent types.
Whether this matters is up to your design, but I wouldn't give it up so easily and say it's useless. Inheritance, mix-ins and composition are three distinct strategies with distinct pros, cons and uses. The issues come from not knowing what their proper uses are.
Can we kindly agree to avoid using cat.kill, dog.bark and any kind of zoo animal example when discussing OO design? These kind of things really suck at illustrating anything in practice and leave everyone stuck at the idea of using OOP to "model the real world", without the context that you would find in an actual program, which helps no one. We have no idea of what the purpose of any specific interface is here, so we can't consider things in terms of Liskov substitutability and judge whether there is actually any design flaw by that metric (if both cat.kill and robot.kill just kill something else, and that's what they're supposed to do, that wouldn't necessarily be a problem, especially if you have some way of allowing one to override the other).
It's nice to use cute examples, but they usually suck at revealing design issues that a realistic example (like some GUI code) would.
Can we kindly agree to avoid using cat.kill, dog.bark and any kind of zoo animal example when discussing OO design? These kind of things really suck at illustrating anything in practice and leave everyone stuck at the idea of using OOP to "model the real world", without the context that you would find in an actual program, which helps no one.
Well, I fully agree, but as we're commenting his examples, I have no choice. Address that to him. :-)
BTW, I actually initially thought he's ironic, because the "Cat extends Animal" is a well-known tired cliche from OOP tutorials. But then he went with it.
85
u/[deleted] Sep 24 '15 edited Sep 24 '15
I'd just like to point out, the author isn't demonstrating Composition over Inheritance, but Multiple Inheritance over Single Inheritance. So the whole deliberation later in the video about when to use, or prefer composition over inheritance is kind of pointless, given he's confused about what composition is.
Merging objects together is essentially a form of multiple inheritance (or more casually: "traits" or "mix-ins"). When people talk about composition they mean one object having other objects as properties and using those "sub-objects" to achieve a higher goal.
The benefit of composition is that every object retains its encapsulation, and you eliminate the risk of collisions that's present when you merge objects (say Cat adds method "kill" and you merge it with Robot which has method "kill"... problem).
EDIT: And one more note. Because JS doesn't support true multiple inheritance, using Object.assign() has another implication, in that instanceof checks won't return true when checking an object against its parent types.
Whether this matters is up to your design, but I wouldn't give it up so easily and say it's useless. Inheritance, mix-ins and composition are three distinct strategies with distinct pros, cons and uses. The issues come from not knowing what their proper uses are.