Composition to the rescue of the inheritance

You can believe me on word, it always ends by getting

The concept of inheritance is a concept that one must master when one uses a language objectIf you want to organize your code around objects, there is a strong possibility that you will be faced with the need to use inheritance. Although the legacy brings a lot of undeniable advantages, this is not the answer to all the problems. If you fall into the indiscriminate use of inheritance, there is a high chance that you mordiez fingers a few months later when you need to evolve the functional scope of your application. Of course it is tempting to say, “If I inherits from Foo, then I will have all of its features without any effort.”. But at the same time, you will bind very strongly to both of your classes and if they come to diverge in the future you will not be able to that you just say “Ok, they share the same when a few features ”. You'll then start to realize that it is now difficult to test each of these classes, and that it is far from obvious to the split. The problems begin. The concern for developers not experienced is that one does not realize this fact that in the middle of a project or when the client wants to make a change to the operation of an existing element. It is necessary to be able to detect the different types of relationships that can exist between classes. A class can be linked to one another by three types of relations: The composition will always be more flexible than a mixins and will not be directly related to the class that the hosts in contrast to the inheritance.

Today, we will talk about software architecture

Therefore, it was now the vehicles have a speed, the car can roll and the helicopters can fly.

Now let us say that one wishes to create a class for the planes technically can roll and fly. Our plane is halfway between the car and the helicopter. Of course, we could use mixins, but this is neither more nor less than a form of multiple inheritance. It would be much better than our current solution and would solve our problem. The other solution would be to use the composition, which allows you to isolate behaviors in specialized classes. We will then use instances of these classes in other classes. It therefore allows to have classes clean, concise, without methods that are superfluous, and very easily testable. Use the composition, it is to have access to the full power of a dedicated class to manipulate an object. So we can put it in place: I grant you that this solution is more verbose than mixins but it is also much more flexible and powerful, and will in practice probably easiest to test.

For the demonstration, I initialized the objects Wheels and Wings on the fly, but in practice it tends to do this work of initialization in the initialize method which would allow to have persistent objects and to avoid the problems of competition.

In practice, no reason to follow a precise method, why use the composition, mixins, or inheritance in an exclusive manner when you can mix the three.

He must know how to adapt and use the solution that will be more flexible.

We will therefore use the inheritance when it is needed, remember, “a developer is a person”.

We will proceed to the mixins when one is in the situation, “a developer is acting as an employee”. It will probably be to the composition if this relationship turns out to be something of a complex, an object requiring a dedicated class. To summarize the content of this article, think of your classes so that they are the most modular as possible, don't stick yourself in a box of which you may not eventually go out, remember to clearly delineate the responsibilities of each. If your tests are difficult to implement, it is often the sign of an architectural issue that should you put a flea in the ear. Sisi, it is possible. Do not stay alone.