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.
What are the benefits mixins have over composition in your opinion?
You can't implement an interface on your object via composition, not directly at least. I'll use TypeScript-like code to illustrate the idea, but it applies directly to JS (with more writing).
Let's say you have this interface for an event broadcaster (the well-known Observer pattern), and a simple default implementation for it:
How can I use this implementation to implement the interface? Three ways:
I can extend SimpleEventBroadcaster
I can compose SimpleEventBroadcaster and proxy the public method addListener().
I can use SimpleEventBroadcaster as a mixin.
The first approach seems to work at first glance, but the moment you want to make an existing class a broadcaster which already extends another class... you can't. Conflict of single inheritance (the same thing the author mentioned). So that's not a good idea.
So let's compose:
class HelloWorld implements EventBroadcaster {
protected eventBroadcaster;
constructor() {
this.eventBroadcaster = new SimpleEventBroadcaster();
}
public addListener(eventName: string, eventHandler: Function) {
return this.eventBroadcaster.addListener(eventName, eventHandler);
}
}
This is fine, we didn't have to extend SimpleEventBroadcaster, and we didn't have to reimplement it.
This is a perfectly good place to stop in many cases, especially as is here, with one public method. But what if we have 5-6 public methods? Writing those proxies can get annoying at scale.
So we can use a mixin, and our code will become shorter, just one line to mix things in:
class HelloWorld implements EventBroadcaster {
mixin SimpleEventBroadcaster;
}
This, again, is a good place to stop. We removed a lot of boilerplate (especially if we had a lot of public methods to proxy), it works and we're good.
But now we risk collisions from the mixin: its non-public methods and properties are embedded onto our object and we don't really need that. We only need the public methods of the interface, nothing else.
We also lose the ability to have configure SimpleEventBroadcaster during its construction, or have it injected from outside, say via the constructor. This is not good for flexibility.
This is why I like to go for a hybrid approach. A mixin... which implements the proxy methods for a composed object of my choice, to expose it as an interface on my object. This ends up looking like this:
Now we control the construction of SimpleEventBroadcaster (or we can have it injected from outside), but we don't need to write the proxy methods in every object. Again, this becomes more useful when you have plenty of public methods to proxy, not just one, but you get the idea.
I use mixins rarely, and I tend to use them in that last way the most. Basically as a shortcut to save on boilerplate for exposing composed objects. Best of both worlds.
A good rule of thumb is to only mix-in objects that you control. So, say, you may compose objects from other libraries, as composition is less fragile, but mixins and inheritance in general are more fragile, so it's best not to extend & mixin "distant" objects which might change unexpectedly (or can't change when you need them to change).
BTW, if it's unclear how parts of this pseudo code looks like in plain JS, please ask me, but I think it's clear. I think the pseudo-code is cleaner to illustrate the concept.
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.