Designing object-oriented software is hard, and designing reusable object-oriented software is even harder.
This is the opening line of the classic manual on software design Design Patterns: Elements of Reusable Object-Oriented Software, published in 1994.
More than twenty years later, this statement still rings true. The first chapter of the book introduces a few high-level principles to accomplish this goal. One of them stands out:
Prefer composition over inheritance
What advantages does composition have over inheritance?
Coupling and Cohesion
Ideally, the objects in our system have high cohesion (i.e. a single responsibility) and low coupling with other objects.
Composition-based solutions break up a problem into distinct responsibilities and encapsulate the implementation of each into separate objects. In an inheritance-based solution, a given object often has multiple responsibilities it has inherited from its various ancestors.
Since complex functionality is assembled by combining a series of objects that can only communicate via their public interface, they tend to rely more strongly on polymorphism and duck typing than the equivalent inheritance-based implementations. This lines up with another principle described in the chapter:
Code to an interface, not an implementation
This lowers coupling and allows you to handle special cases by passing in an object with a different implementation but the same interface.
In a composition-based system, the various components get combined together at run-time to create the desired behavior. This allows flexibility to build behavior dynamically without having to resort to clever metaprogramming tricks.
Composition has a few downsides too. It tends to add more indirection to a system. It can also be more work to assemble all the components together into the desired behavior. Anything that can be implemented via inheritance can alternatively be implemented using composition. So when should you use inheritance?
Use inheritance when the cost of indirection and construction of a composition-based approach is higher than the benefits of encapsulated responsibilities and flexibility.
Use inheritance when your object already has a single responsibility and you really do just want specializations.
Use inheritance when you don’t have a choice such as when building
In general, composition is a more robust and flexible approach to architect reusable object-oriented software. Composition should be your go-to solution most of the time. Only reach for inheritance when it makes sense.
- The first chapter of Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, and Vlissides has a great discussion on the concepts behind what makes software components easy to change and reuse.
- The first chapter of Design Patterns in Ruby by Russ Olson has a similarly good analysis, specifically for Ruby.
In both cases, it’s worth reading that first chapter even if you don’t read the whole book.
This article is part 4 of 4 in a series on building reusable object-oriented software.