I have been a huge fan of composition for years, although composition and inheritance aren’t mutually exclusive. Your original example works fine until you need to make a MurderRobot that can Bark, so all you have to do is abstract Bark into a barker, and then create a MurderRobotDog class which also composes in the methods of being a barker. Works exactly as you describe, but doesn’t require getting rid of the initial inheritance pattern which still works so well.
At least in the Java world, creating delegates which do the jobs specified by an interface is just a dandy way to go about this.
I think that's they way you go about it in nearly any type safe language. If you have a bunch of classes that implement certain methods and want to interact with them as a group you need a type that encompasses that shared functionality. If you have two classes that require the same implementation of method then make a delegate that they both call. If your class starts to become bloated then decompose it into several delegates. Composition and inheritance aren't at odds. They are two complimentary ways to structure your code.
4
u/juckele Sep 24 '15
Best comment on the article itself
At least in the Java world, creating delegates which do the jobs specified by an interface is just a dandy way to go about this.