[Tutor] Aggregation vs Composition

boB Stepp robertvstepp at gmail.com
Sun Dec 10 19:40:05 EST 2017


I own this book, too.  I'll insert the portions of the text that I
believe the OP is referring to.

On Sun, Dec 10, 2017 at 3:01 AM, Alan Gauld via Tutor <tutor at python.org> wrote:
> On 10/12/17 05:07, jia yue Kee wrote:
>
>> in Dusty Philips's Python 3: Object-Oriented
>> Programming book.
>
> Caveat: I've not read this book so can only
> guess at what the author might be meaning.
>
>> Based on my reading, what I gathered was that Composition implies a
>> relationship where the child cannot exist independent of the parent while
>> Aggregation, on the other hand, implies a relationship where the child can
>> exist independently of the parent.

The author defines composition on page 17 as:  "Composition is the act
of collecting together several objects to compose a new one.
Composition is usually a good choice when one object is part of
another object."

> Correct. But the key word here is "implies".
> In some languages the differences can be directly
> implemented in the language but in Python the
> relationships are always just implied. The differences
> do not really exist. (Unless you go to inordinate
> lengths to hide and link the data, which is rarely,
> if ever, justified.)
>
>> However, in one of the paragraph of the book, *Dusty mentioned that
>> composition is aggregation*

On page 18 the author goes on to use a chess set as an example aiming
to use object-oriented design for a computer chess game.  I _think_
the OP may be referring to this paragraph:

"The chess set, then, is composed of a board and thirty-two pieces.
The board is further comprised of sixty-four positions.  You could
argue that pieces are not part of the chess set because you could
replace the pieces in a chess set with a different set of pieces.
While this is unlikely or impossible in a computerized version of
chess, it introduces us to *aggregation*.  Aggregation is almost
exactly like composition.  The difference is that aggregate objects
can exist independently.  It would be impossible for a position to be
associated with a different chess board, so we say the board is
composed of positions.  But the pieces, which might exist
independently of the chess set, are said to be in an aggregate
relationship with that set."

He continues in the next paragraph:

"Another way to differentiate between aggregation and composition is
to think about the lifespan of the object.  If the composite (outside)
object controls when the related (inside) objects are created and
destroyed, composition is most suitable.  If the related object is
created independently of the composite object, or can outlast that
object, an aggregate relationship makes more sense.  Also keep in mind
that composition is aggregation; aggregation is simply a more general
form of composition.  Any composite relationship is also an aggregate
relationship, but not vice versa."

I think it is these last two sentences that are confusing the OP.

Hope this provides more context to help the OP.

boB


More information about the Tutor mailing list