I have recently been digging into composition as related to object oriented programming.<p>Can someone summarize why this is better than say inheritance?<p>I can possibly understand the value of composition where you need a large team to be able to constantly add and remove functionality to a code base with this without having to mess with APIs override methods, etc...<p>But is there a fundamental benefit of composition over inheritance in ever circumstance? (I've read a number of articles on this recently and I am not understanding the benefits vs the complications it adds)
A monk asked Java master Kaimu:
What is the “single-responsibility principle”?<p>Said Kaimu:
That a class shall do exactly one thing,
and do it completely.<p>The monk asked:
How shall I decide this “one thing”?<p>Said Kaimu:
The Fisherman does not build ships,
or we would call him a Shipwright.<p>The monk asked:
Is there no room in your philosophy for interfaces?
What if my class can serve as a Fisherman,
and a Shipwright and Sailor besides?<p>Said Kaimu:
What would you name such a three-headed monster?<p>The monk replied:
Shísho, after my uncle. He lives by the sea and does all these things.<p>Said Kaimu:
I would give your class Shísho three instance variables:
a Fisherman, a Shipwright, and a Sailor.
Then Shísho may implement those interfaces by delegation.<p>The monk replied:
I speak of inheritance, yet you answer in composition.
All of my uncle is a Fisherman, not just his left foot.
What use are objects, if we do not faithfully model the world?<p>Said Kaimu:
If I paint a fine shark upon this page, will you say, “Fine shark!”
or will you complain that it is flat and does not eat you?<p>The monk asked:
But how are we to know when the flat shark is shark enough?
Or when our uncle should fish with his left foot?<p>Said Kaimu:
Learning how is our “single reponsibility”.<p>- “Flat Shark”<p><a href="http://thecodelesscode.com/case/175" rel="nofollow">http://thecodelesscode.com/case/175</a>
If your system doesn't have anything to gracefully handle inter-component communication without coding custom adapters and introducing new dependencies, all your "reusable" components will become a legacy mess in 2-5 years.
Configuration === composition, IMO.<p>Composition is just bringing together a bunch of objects to form something that does more.<p>Configuration is just bringing together a bunch of primitives, perhaps structured, to form something that does more.