Software DevelopmentLearn Object Oriented Programming in Python - Composition

Learn Object Oriented Programming in Python – Composition

The Concept
When you hear the word “composition”, what will be the first thing to come into your mind?

– Actually, complexity!!

Great, you are right!! The word itself means something is composed of smaller things, and that (besides to the pronunciation of the word) is what gives you the feeling you mentioned.

Now, let me ask you another question: given that you have a class that was written by somebody else, and you need to use the features in that class in a way, but you need to add your own data and methods that suits your needs. What would you do?!

– Are you kidding?! Of course I will use inheritance!! It is you who told us that in the last article!!

Yes, yes… you are absolutely right!! Now, (and please bear with me for another 2 minutes) what if you have two or three base (parent) classes in hand, and you need to use features in all, besides to extending them with your own data and methods?!

– Ah, I think I will create one child class that inherits both super-classes.

Yes, this is a possible solution!! But a simpler one would be the composition.

What is Composition?
The composition means having a container object that contains other objects.

What is the Difference between Inheritance and Composition?
In Inheritance, a class is inherited (extended) by a new sub-class that will add custom attributes and behavior to the inherited ones.

In Composition, a class is utilized by creating an instance of it, and including that instance inside another larger object.

To make it simpler and easier to remember, let’s say it in one sentence:

Inheritance extends (inherits) a class, while Composition uses an instance of the class.

C Programming_1

Example
To get the idea closer to your mind, let’s take an example. In this example, we have two classes: Math and Math2. Math has two member functions that perform addition and subtraction, while Math2 has two methods that perform multiplication and division. We need to utilize the existing classes and offer (besides to the four available operations) a new operation which is the power function.
So, let me start by introducing our friends Math and Math2:

1
Now, I will define a new class Math3 (so, please pay attention to the coming magic)
2

Well, have you got what I have done? I have defined a normal class, and defined the __init__ method (the constructor) to initialize the instances. Among what will be initialized in the __init__ function are two objects m1 and m2, one is an instance of the Math class, while the other is an instance of the Math2 class.
Next, I have defined the new method power. After that I defined what could be considered a delegation from the new class to the contained objects. I defined the member function add, that will call the m1 member function add. Similarly, the subtract and multiply methods have been declared.
Now, let’s see them all in action.

3
It works as expected… Great !!!
So, again what have we done?

We have two existing classes, each achieve part of the data processing we need. Instead of writing one new class that achieves the whole process from scratch, we need to make use of the existing ones. So, the solution is to create a new class that defines instances (objects) of the existing classes. Now, we have in hand all two objects, each of them has data and methods. The new class defines the new data and methods that it was basically created to achieve, and on need delegates some of the required operations to the member functions of the defined objects (the instances of the existing classes). Notice the usage of the word “delegate” which means to assign a job to someone who is known to do it well.
So, as the composition actually includes delegating some functions to objects, some references use the term delegation instead of composition.

Example
To strengthen our understanding of the concept, let’s take another example. In this example, assume we have in hand two classes CountUp and CountDown. As their names implies, one is used to count up, and the other to count down.

4

Now, let’s see the new class in action, and test its operations:
Now, we have two great classes, one counts up, and the other counts down. We need a new class that can count up and down, and on need it can reset the counter to 0. To achieve this using the composition (delegation), we are going to define a new class that will use instances of both the CountUp and CountDown classes. The instance of the CountUp class will be delegated the task of incrementing the counter when asked to do. This is because it is the one who knows how to do so by using its increment method. Similarly, the instance of the CountDown class will be delegated the task of decrementing the counter, also because it is the one who can do by using its decrement method. Besides to these delegated operations, the new class will add its own touch by defining a method that resets the counter to 0, and another to display the counter.
The new class NewClass should look like this:

5
Now, let’s see the new class in action, and test its operations:
6
It worked just as desired!! Perfect!!

* * * * * *

In this article, we have talked about the concept of Composition in OOP. Composition is a technique that can be a replace to the Multiple Inheritance that some designers and developers prefer to avoid.

The idea is instead of inheriting the existing classes; I use instances (objects) of them. While new data and methods are added to achieve the new requirements that the old classes don’t achieve, the old operations (that are supported by the old classes and are still needed) are delegated to the convenient objects which know how to perform.
I hope you find article useful.
See you in the next article.

5 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -