r/javascript • • Sep 21 '15

Composition over Inheritance (9 min video)

https://medium.com/humans-create-software/composition-over-inheritance-cb6f88070205
17 Upvotes

7 comments sorted by

4

u/MoTTs_ Sep 25 '15

As I watched your video, I thought, "Wait... that's not composition. That's multiple inheritance." Then I looked at the comments of (a different) reddit thread, and the top voted comment said exactly the same thing.

EDIT: For example, in Python, which supports multiple inheritance, we can write:

class barker:
    def bark(self):
        print('Woof, I am ' + self.name)

class driver:
    def drive(self):
        self.position = self.position + self.speed

class killer:
    def kill(self):
        pass # ...

# all together now
class murderRobotDog(barker, driver, killer):
    def __init__(self, name):
        self.name = name
        self.speed = 100
        self.position = 0

bruno =  murderRobotDog('bruno')
bruno.bark() # "Woof, I am Bruno"

4

u/dwighthouse Sep 21 '15

Nailed it.

1

u/fay-jai Sep 22 '15

Love your video and article - really great explanation!