Composition Over Inheritance

Composition is a method of writing OOP code. It attempts to solve the same Software DesignSoftware Design
This page acts as a map of content for Software Design topics.



Status: #🗺️
problem as Inheritance, but instead of relying on inheriting features from the parent class, composition focuses on embedding objects inside of objects to allow the sharing of functionality.

While designing with Inheritance in mind, we are constantly trying to answer the question what objects are?. Composition on the other hand focuses on a different question - What objects do?.

It's important to note that Composition over inheritance is a principle that says that Composition should be favored over Inheritance, not that it should replace inheritance alltogether. See Should I be inheriting from this type?Should I be inheriting from this type?
[[Composition Over Inheritance]] as [[OOP]] principle doesn't have much value if we don't invest time to think about the cases when [[Inheritance]] should be favored over Composition.

If a class w...
.

Let's look at a simple problem and how we'd first solve it with Inheritance. We want to represent some animals and robots in our application.

 classDiagram  
 
 
 class Animal {
 +mate()
 }
 
 Animal <|-- Dog  
 class Dog{  
 +run()
 +bark()
 }
 

 class Robot{
 +drive()
 }
 
 Robot <|-- Roomba
 class Roomba{
 +clean()
 }

Dog is an animal that can bark, and Roomba is a robot that can clean - simple! Now imagine a Roomba which is also designed to keep your home both clean and safe, by being able to bark while cleaning. I'm sure we'd find a solution to this problem with inheritance, but composition makes this much more simpler.

 classDiagram  
 
 
 class CanMate {
 +mate()
 }
 
 class CanRun {
 +run()
 }
 
 class CanDrive {
 +drive()
 }
 
 class CanBark {
 +bark()
 }
 
 class CanClean {
 +clean()
 }
 
 
 CanRun *-- Dog
 CanBark *-- Dog
 CanMate *-- Dog
 class Dog{  

 }
 


 CanDrive *-- Roomba
 CanClean *-- Roomba
 class Roomba{

 }
 
 CanDrive *-- SecureRoomba
 CanClean *-- SecureRoomba
 CanBark *-- SecureRoomba
 class SecureRoomba{

 }

We define reusable behaviours separate from their owning objects, which allows us to easily create a Roomba that CanBark, or even a Dog that CanClean!

Sometimes, it's really useful to just be able to drop the labels - what you are doesn't have to define what you can do! jk lol


Status: #🌲

References: