Composition over Inheritance

Fun Fun Function · 535K views · 2015 · 8:34

Open on YouTube

Software architecture & designWeb developmentProgramming language designexplainer

Why Stack Overflow recommends it

Stack Overflow users link it when someone is fighting a class hierarchy, wants multiple inheritance, or asks about Object.assign and composition patterns, because it explains the idea quickly with a memorable example.

Recommended by 11 people across 10 different months, 2013 to 2021.

What it is

An 8-minute episode of the Fun Fun Function show in which MPJ explains why designing types by what they can do (composition) beats designing them by what they are (inheritance). He uses JavaScript examples to show the limits of class hierarchies and how to compose behaviours instead.

What people said

Strong rec smolinari · 2016-12 · Stack Overflow · on “Is a class that manages multiple classes a "god object"?”

Thanks @WReach. I looked into this more. I found some pretty cool videos on this. The best one is with the analogy, "When you want a banana, why should you get the gorilla with it?" [this video] My curiosity stems from this "framework". It is basically becomes a very large object, depending on the application, from what I understand of it. [LINK]

Recommends bholben · 2015-11 · Stack Overflow · on “Kyle Simpson's OLOO Pattern vs Prototype Design Pattern”

This sounds like "favor composition over inheritance" - a great strategy. In ES6, you can use `Object.assign()` - [LINK] If writing in ES5, you can use Underscore's `_.extend()` or Lodash's `_.assign()`. Here's an excellent video to explain... [this video] If you have any colliding properties, the last one wins - so order matters.

Recommends Oleksandr Papchenko · 2015-12 · Stack Overflow · on “Multiple inheritance in Javascript with prototype chain”

Why not to do something like this: ViewBase ViewBase -EventValidator, which implements Validate --ViewTabs ---EventViewTabs --ViewSimple ---EventViewSimple. Also consider to use composition over inheritance see this video [this video]

Recommends chriszumberge · 2016-02 · Stack Overflow · on “How to Maximize Code Reuse in this Interface vs Inheritance C# Example”

Inspired by a great video [this video] on the topic "Favor object composition over inheritance" which used JavaScript examples; I wanted to try it out in C# to test my understanding of the concept, but it didn't go as well as I'd hoped. /// PREMISE // Animal base class, Animal can eat public class Animal { public void Eat() { } } // Dog inherits from Animal and can eat and bark public class Dog : Animal { public void Bark() { Console.WriteLine("Bark"); } } // Cat inherits from Animal and can eat and meow public class Cat : Animal { public void Meow() { Console.WriteLine("Meow"); } } // Robot base class, Robot can drive public class Robot { public void Drive() { } } The problem is that I want to a

Recommends Ian · 2016-10 · Stack Overflow · on “Redux store namespaced actions?”

This is a perfect time to argue for composition over inheritance. If this concept is totally new to you, go watch this video [this video] . It sounds to me like your common view component should be a child (perhaps the only child) of your other components that would "extend" it. Then you can pass in on props the specific action creators that the common view component should use. /* Common View Component */ class CommonSplitView extends React.Component { ... render() { return ( <button onClick={this.props.onCloseDetailView}>Close</button> <button onClick={this.props.onOpenDetailView}>Open</button> ); } } CommonSplitView.propTypes = { onCloseDetailView: React.PropTypes.func, onOpenDetailView: React

Recommends epayne · 2017-02 · Stack Overflow · on “The correct structure for linking objects”

Would you suggest going forward I should just use composition instead? I watched this video which helped clear quite a bit [this video]

Recommends Gerben Rampaart · 2017-03 · Stack Overflow · on “What's the equivalent of Javascript's Object.assign() in C#”

Vivek's answer is the correct one. Because Object.Assign is part of the larger Inheritance vs Composition discussion. This is not so simple in javascript, where you need Object.Assign, but in C# you can compose a class in your own class by just instantiating it and making it a property. You really do it all the time. Javascript with it's prototypal inheritance, you're really just linking objects. So inheritance (classical) does not exists there. Check this video, it's very enlightening: Inheritance vs Composition [this video]

Recommends Vala · 2017-03 · Stack Overflow · on “Is it possible to call constructor function method without creating object in JS?”

It appears that you are coming to JavaScript hoping that it's Java for the browser. It isn't. You should spend some time trying to grok the prototype inheritance model, which is pretty significantly different from classical inheritance. He's pretty judgmental in many ways and don't take his word as gospel, but I think you may pick up a lot from these videos: [LINK] [this video]

Recommends Ahmad · 2019-09 · Stack Overflow · on “Component has Render Method, but does Not Extend React.Component”

I wouldn't have expected this to work to begin with. Let's examine how this extends-classes library works. The classes function returns a dummy class that within its constructor goes through all classes listed as arguments to the classes function and copies over all functions and properties to the aforementioned dummy class. It then calls the constructor of all classes. tl;dr: it mashes everything together so the class you're actually extending from is not an actual instance of React.Component . Which is most likely why you're getting this error. Don't fight the language. The language doesn't support multi inheritance. May I suggest you use composition over inheritance [this video] instead?

Recommends Fuhrmanator · 2020-01 · Stack Overflow · on “Head First Design Patterns question on Duck Simulator walk through in chapter 1”

And in the programming to an interface solution the UML illustration appears to require same amount of duplicate code that Joe was berated for. It doesn't seem to solve that problem. That's right. Inheritance (by subclassing or using interfaces) is pretty inflexible to changes, which is what the managers are trying to explain. Well I've either overthought this too much or I just don't understand the assertions of code duplicatation and "these behaviors are no longer hidden away in our Duck classes" They're pulled out of the implementations of Duck classes, and put into a new hierarchy of Behavior classes. Then the Duck classes can (as needed) use the implementations of those behaviors (think