Slide 1

Slide 1 text

COMPOSITION OVER INHERITANCE “LA TASSA SULL ’EREDITARIETÀ”* * il titolo è colpa di @liuggio

Slide 2

Slide 2 text

WHO AM I? ▸ Francesco Mosca ▸ twitter.com/moscaf ▸ Technical Lead @ Doing A DEV IS NO ONE

Slide 3

Slide 3 text

DEFINITIONS TWO COMMON PATHS TO CODE REUSE ▸ Inheritance: a class may inherit […] the fields and methods of its superclass. Inheritance is transitive […]. Subclasses may override some methods and/or fields to alter the default behavior. ▸ Composition: when a Field’s type is a class, the field will hold a reference to another object, thus creating an association relationship between them. Steven Lowe - Composition vs. Inheritance: How to Choose?

Slide 4

Slide 4 text

TOLD YOU SO! A (REALLY NOT SO) LONG TIME AGO

Slide 5

Slide 5 text

REMEMBER THIS! “REUSING THE IMPLEMENTATION” “[…] THE NEW PROGRAMMER CAN GET THE IDEA THAT INHERITANCE SHOULD BE USED EVERYWHERE. THIS CAN RESULT IN AWKWARD AND OVERLY COMPLICATED DESIGNS. INSTEAD, YOU SHOULD FIRST LOOK TO COMPOSITION WHEN CREATING NEW CLASSES, SINCE IT IS SIMPLER AND MORE FLEXIBLE.” (Bruce Eckel - Thinking In Java, 1998) “FAVORING OBJECT COMPOSITION OVER CLASS INHERITANCE HELPS YOU KEEP EACH CLASS ENCAPSULATED AND FOCUSED ON ONE TASK. YOUR CLASSES AND CLASS HIERARCHIES WILL REMAIN SMALL AND WILL BE LESS LIKELY TO GROW INTO UNMANAGEABLE MONSTERS. “ (Gamma et al - Design Patterns, 1994)

Slide 6

Slide 6 text

YEAH, SURE… Bruce Eckel - Thinking In Java “YOU CREATE A BASE TYPE TO REPRESENT THE CORE OF YOUR IDEAS ABOUT SOME OBJECTS IN YOUR SYSTEM. FROM THE BASE TYPE, YOU DERIVE OTHER TYPES TO EXPRESS THE DIFFERENT WAYS THAT THIS CORE CAN BE REALIZED.“ BUT THEN… (continues for 8 pages)

Slide 7

Slide 7 text

A STOLEN EXAMPLE SO, WHAT’S THE (FIRST) PROBLEM? Mattias Petter Johansson - Composition over Inheritance bark() poop() Dog meow() poop() Cat

Slide 8

Slide 8 text

A STOLEN EXAMPLE SO, WHAT’S THE (FIRST) PROBLEM? Mattias Petter Johansson - Composition over Inheritance bark() Dog meow() Cat poop() Animal

Slide 9

Slide 9 text

A STOLEN EXAMPLE SO, WHAT’S THE (FIRST) PROBLEM? Mattias Petter Johansson - Composition over Inheritance clean() CleanerRobot kill() MurderRobot drive() Robot

Slide 10

Slide 10 text

A STOLEN EXAMPLE SO, WHAT’S THE (FIRST) PROBLEM? Mattias Petter Johansson - Composition over Inheritance “Our customers demand a MurderRobotDog. It needs to be able to .kill(), .drive(), .bark(), but it cannot poop().

Slide 11

Slide 11 text

A STOLEN EXAMPLE SO, WHAT’S THE WRONG SOLUTION? Mattias Petter Johansson - Composition over Inheritance Dog meow() Cat poop() Animal clean() CleanerRobot kill() MurderRobot drive() Robot bark() GameObject MurderRobot Dog

Slide 12

Slide 12 text

A STOLEN EXAMPLE SO, WHAT’S ANOTHER WRONG SOLUTION? Mattias Petter Johansson - Composition over Inheritance bark() Dog meow() Cat poop() Animal clean() CleanerRobot kill() MurderRobot drive() Robot bark() MurderRobot Dog

Slide 13

Slide 13 text

A STOLEN EXAMPLE SOOO, WHAT’S THE “RIGHT” SOLUTION? Mattias Petter Johansson - Composition over Inheritance poop() Pooper bark() Barker Dog drive() Driver kill() Murderer MurderRobot MurderRobot Dog Cat meow() Meower clean() Cleaner CleanerRobot

Slide 14

Slide 14 text

A STOLEN EXAMPLE SO, WHERE’S THE CODE?

Slide 15

Slide 15 text

ANOTHER STOLEN EXAMPLE BUT, WHAT’S ANOTHER PROBLEM? Chris Hanson, ⚠ www.hautelooktech.com play() { “thru speakers!” } stop() MusicPlayer changeSide() RecordPlayer muteTrack() EightTrackPlayer

Slide 16

Slide 16 text

ANOTHER STOLEN EXAMPLE BUT, WHAT’S ANOTHER PROBLEM? Chris Hanson, ⚠ www.hautelooktech.com play() { “thru headphones!” } eject() Walkman play() { “thru speakers!” } stop() MusicPlayer

Slide 17

Slide 17 text

ANOTHER STOLEN EXAMPLE BUT, WHAT’S ANOTHER PROBLEM? Chris Hanson, ⚠ www.hautelooktech.com play() { “thru headphones!” } upload() MP3Player play() { “thru headphones!” } eject() Walkman X

Slide 18

Slide 18 text

ANOTHER STOLEN EXAMPLE OK THEN, WHAT’S THE “RIGHT” SOLUTION, AGAIN? Chris Hanson, ⚠ www.hautelooktech.com play() { “thru headphones!” } HeadphonesOutput play() { “thru speakers!” } SpeakersOutput upload() MP3Player changeSide() RecordPlayer muteTrack() EightTrackPlayer eject() Walkman play() { “thru wathever!” } BluetoothOutput

Slide 19

Slide 19 text

COMPOSE OR INHERIT? IS-A VS. HAS-A * no animals were harmed during the production of these slides (yes, the cat was already dead)

Slide 20

Slide 20 text

MORE… LISKOV SUBSTITUTION PRINCIPLE “SUBTYPES MUST BE SUBSTITUTABLE FOR THEIR BASE TYPES.” Robert C. Martin - Clean Code

Slide 21

Slide 21 text

LISKOV SUBSTITUTION PRINCIPLE area() width height Rectangle width height Square class  Rectangle  {
    function  area()  {  return  $this-­‐>width  *  $this-­‐>height  }
 
    //  …
 } class  Square  extends  Rectangle  {
 
    function  setWidth($width)  {  
        $this-­‐>width  =  $width;
        $this-­‐>height  =  $width;
    }
 
    //  …
 } function  testArea(Rectangle  $r)  {  
    $r-­‐>width  =  5;
    $r-­‐>height  =  4;
 
    assertEquals(20,  $r-­‐>area());  //  EPIC  FAIL
 }

Slide 22

Slide 22 text

TRADEOFFS, TRADEOFFS EVERYWHERE… PROS & CONS! ▸ Inheritance specifies the Domain in terms of semantics (focus on “what it is”) ▸ Composition allows the Domain to be flexible in terms of behaviors (focus on “what it does”) ▸ Inheritance is good for fixed, well-specified hierarchies (UI), but forces design early on ▸ Composition helps with “the same, but different” (business reqs.) and allows for supple design ▸ Inheritance is easy to (involuntarily) abuse, mixing different concerns and coupling unrelated behaviors, becoming harder to reason about (and test) ▸ Composition is more explicit and, focusing on small and single-responsibility objects, remains easier to reason about (and test) (So yeah, not many tradeoffs: composition is almost always preferable)

Slide 23

Slide 23 text

“EVERYTHING IN MODERATION, INCLUDING MODERATION.” Oscar Wilde THANKS!

Slide 24

Slide 24 text

QUESTIONS? QUESTIONS? ▸ Questions?

Slide 25

Slide 25 text

QUESTIONS! BUT… BUT… WHAT ABOUT TRAITS?

Slide 26

Slide 26 text

QUESTIONS! BUT… BUT… WHAT ABOUT MULTIPLE INHERITANCE?

Slide 27

Slide 27 text

QUESTIONS! BUT… BUT… WHAT ABOUT “FINAL”?