in oop php programming solid ~ read.

Composition over Inheritance

In this post, we are going to talk about Composition over Inheritance.

What is Composition over Inheritance?

In order to understand the topic we need to understand Composition and Inheritance separately. For programming, it all boils down to trying our best to avoid duplication, trying our best to make sure our code is DRY enough, not Wet.

Inheritance

In Object oriented programming, we use classes to inherit commonly used state and behaviors from other classes in order to achieve the reusability. And Inheritance defines the way we extend our class in order to keep the majority of our code dry and clean.

Composition

Composition refers to combining simple types to make more complex ones. You can imagine that we use a little component here and there in order to achieve a rather complicated goal at the end.

Why should I choose Composition over inheritance?

In order to explain the difference we need to get some code first.

We normally will use our real life object to create our classes. For example, if we need to create Cat and Dog class we might need to create a parent class to hold the similarity between Cat and Dog in to a Animal class.

class Animal {...}

class Dog extends Animal {...}  
class Cat extends Animal {...}  

As you can imagine as more and more really life objects got introduced into here. The distinction will soon become rather ambiguous. Let's say right now we need to create a Bird class and make it even more distract like the following.

class Animal {...}

class Bird extends Animal {...}  
class Mammals extends Animal {...}

class Dog extends Mammals {...}  
class Cat extends Mammals {...}  

Next let's image that we have to include a Penguin class. Should we put it under Bird or Mammals? It is rather ambiguous at this point. Cause not everything in our real life is either 1 or 0, is not either white or black.

Why Composition?

class Animal {...}

class Bird extends Animal {  
    abstract protected function fly();
    ...
}
class Mammals extends Animal {...}

class Dog extends Mammals {...}  
class Cat extends Mammals {...}  

Let's say there is an abstract method in Bird called fly() that will enable the classes inherited from it to fly. And you make Penguin extend from Bird. But as we all know Penguins can't fly, maybe it used to be able to but not anymore. In that case there is not too much change you can do about it.

Let's think about it using Composition that doesn't inherit anything at all. Let's have a look at the following example.

class flyable {  
    abstract protected function fly();
}

class Penguin extends fly{  
    protected function fly() {...}
}

If we want to make it fly we can just make it extend flyable class and that it is. Not too much stuff to worry about.

End

This concept has been widely used in Functional programming and has been proved to be rather efficient and fast. Hope this will give you some great ideas between Composition and Inheritance. As always if you have any opinions or ideas please leave a comment below.

comments powered by Disqus