You are previewing Practical Object-Oriented Design in Ruby: An Agile Primer.

Practical Object-Oriented Design in Ruby: An Agile Primer

Cover of Practical Object-Oriented Design in Ruby: An Agile Primer by Sandi Metz Published by Addison-Wesley Professional
O'Reilly logo

Chapter 8. Combining Objects with Composition

Composition is the act of combining distinct parts into a complex whole such that the whole becomes more than the sum of its parts. Music, for example, is composed.

You may not think of your software as music but the analogy is apt. The musical score of Beethoven’s Fifth Symphony is a long list of distinct and independent notes. You need hear them only once to understand that while it contains the notes, it is not the notes. It is something more.

You can create software this same way, by using object-oriented composition to combine simple, independent objects into larger, more complex wholes. In composition, the larger object is connected to its parts via a has-a relationship. A bicycle has parts. ...

The best content for your career. Discover unlimited learning on demand for around $1/day.