Press "Enter" to skip to content

Composition over Inheritance


today we are going to talk about

composition over inheritance inheritance

is when you design your types around

what they are while composition is when

you design your types around what they

do we’re going to learn about

limitations of comparatives and how to

solve them using composition this is a

weekly show when we try to become more

confident and excited about programming

by learning intriguing things that we

didn’t know before a tip is that you

will get more out of this video if you

have watched the previous episode about

factory functions you can do so by

clicking there let’s talk about

inheritance a bit let’s say that we are

designing a game and I need a dope what

after a while we realize that our

software like everything needs cats so

we create a cat class and because nature

calls we add poop to the cat and dog

class that’s duplication we can’t have

that so we lift poop into a shared

animal class and well now we we have a

lot of animals pooping everywhere so we

need a cleaning robot that can drive and

clean we also need a murder robot that

can drive and kill all the cats and dogs

that are pooping all over your nice

Scandinavian white floors

since drive is now duplicated between

cleaning robot and murder robot we

create a robot parent class to put it in

a couple of months of development go buy

your dog your murder robot and friends

have grown into a mature stable system

you’re feeling good about it at this

point in the project without fail the

project manager will say this our

customers demand a murder

robot dog the murder robot dog can kill

drive and bark but it doesn’t have a

digestive system so it cannot whoo and

now we are screwed we simply can’t fit

the murderer bot dog 9

sley into this inheritance hierarchy I

mean I guess that we could create a new

parent object where you put all the

functionality that is share so maybe a

game object with a port method like this

but this means that your objects will

have a ton of functionality that they

don’t use you end up with a sort of

gorilla banana problem where you request

a banana but you get a gorilla holding a

banana and the entire jungle with it or

maybe instead of a parent object we can

just duplicate the functionality like

this we give bark to murder Robo dog in

addition to the dog that is not as bad

but our mothers still wouldn’t approve

thus composition to the rescue again

inheritance is when you design your

types around what they are while

composition is when you design your

types around what they do using

composition we would do this instead a

dog is a poop herb that is also a marker

a cat is a pooper that is also a me our

a cleaning robot is a driver and a

cleaner and a murder robot dog is simply

a driver and a killer and Barker now I

am going to show you how to do this in

practice using javascript we’re going to

do this using normal functions and

normal object literals behold these are

the definitions of Barker and driver

I will let you contemplate these two

functions in solitude for a minute

the astute of you might have noted that

these functions are like the factories

from the previous video but instead of

creating their own state internally they

accept their state as a function

parameter this is so that they can share

the same state have a look at what the

actual murder robot dog Factory looks

like first it creates a state object and

assign some default values to speed and

position it also assigns the name from

the function argument it then uses

something that you might not have seen

before

object assign what object design does is

that it takes an object in this case a

just a new and the object and assigns

the properties from the other objects

into it so in this case it creates a

marker a driver a killer and then merges

them all into the new object and returns

that object we have our murder robot dog

DC a spy object dot assign is a handy

function built into Xmas script six but

all libraries underscore lodash has its

it is also super simple it does almost

nothing so if you’re feeling frisky you

can just write one yourself it’s like

seven lines of code in summary we have

looked at an example of an inheritance

tree the rokda and then we looked at how

to restructure it using composition and

finally we looked at how to actually

compose objects using javascript the

question that is probably on your mind

right now is when do you use inheritance

and when do I use composition well the

vast majority of developers would say

that you should favor composition over

inheritance me personally go one step

further I don’t see the point of using

inheritance at all I don’t think it’s a

good part

and I don’t think it should be used a

lot of people are going to tell you that

if something is an is a relationship

then it should use inheritance for

example me Matias is a map so I can

inherit from the map class if the

relationship is more of a has a nature

such as a car which has an engine then

you should use composition but I think

that reasoning is topsy-turvy while it

is true that I am a man I also have arms

and I also have cooking skills while it

is correct that a car has an engine it

also is a vehicle you can interpret

pretty much any concept both ways when

it comes down to it I think that the

problem with inheritance is that it

encourages you to go predicting the

future inheritance encourages you to

build this taxonomy of objects very

early on project and you are most likely

going to make big design mistakes while

doing that because humans cannot predict

the future even though it feels like we

can and once you build yourself into

that inheritance taxonomy it’s really

hard to get out of it I think it’s just

better to use composition from the start

it’s more flexible it’s more powerful

and it’s really easy to do so why not

and that is it that’s my thoughts on

composition but what are yours comment

down below or maybe just hit like if

you’re not be focus if type in this show

we try to become more confident and

excited about programming by learning

new intriguing things that we didn’t

know before there won’t be an episode

next Monday because I will be at Jay s

Kampf in Berlin if you’re also going to

be there make sure you tweet me at MP

Jamie so that we can say hi in real life

if you’re not going to J’s comm make

sure that you subscribe

that you don’t miss out on the next

steps until next next Monday stay curious

Please follow and like us: