Home > Uncategorized > Aggregation vs Composition

Aggregation vs Composition

One of colleagues asked me this question. What is the difference between aggregation and composition. Since it is a very common question I thought I could blog about it rather than explaining it to her alone. Before we delve into the differences let us discuss the similarities first.
 
Both Aggregation and Composition model a ‘has a’ / ‘contains’ / ‘whole part’ relationship. People usually use the ‘has a’ test to determine if a relationship can be modelled as an Aggregation / Composition. For ex: Order has a LineItem, LineItem has a Product pass the has a test. (I am just forming a sentence with the Parent object in the front, the has a phrase in the middle and the child object in the end). Manager has a person, Employee has a person etc do not make sense. It rather fits the ‘is a’ test and hence could be candidates for inheritance. Now that we have the background lets us see what is the difference between them.
 
Whether a relationship is an Aggregation or Composition is primarily decided by who can control the lifetime of the object. If the parent controls the lifetime (i.e. the object(s) is/are instantiated by the parent and goes out of scope when the parent goes out of scope) then it is Composition. If the parent does not control the lifetime of the child then it is Aggregation. This makes sense only if the child object can be used outside of the context of the parent. Here the child object does not go out of scope if the parent object goes out of scope. That is the object can be shared by another parent object. In case of Composition this can never happen, only one parent object can own the child object.
 
"I see composition and aggregation as opposite ends of a conceptual spectrum. At the composition end, constructors and instance methods create back-end objects and stick references to the back-end objects into its own instance variables. At the aggregation end, container objects are given references of objects to hold onto and then give back in some fashion. "
 
Examples time
Explaining Composition and Aggregation without examples is real hard. Let us look into an example. Consider an order processing system. Let us take just a few objects which will interest this discussion. An "Order" which represents an actual order by a "Customer". A "LineItem" which represents each of the LineItems in an Order. An Order has a LineItem(s). The same LineItem cannot be part of another order. In other words the LineItem object does not make any sense outside the context of an Order and the Order object solely owns the LineItem. This is a candidate for Composition. Sale, SaleLineItem is another example for Composition in the retail domain.
 
Consider a Course registration system in which we are modeling the Department and the Student. The Department object has Students. It is a has a relationship. But the same Student can be part of multiple department objects. In the order processing system example itself each LineItem has a Product, but the Product can be shared across different LineItem objects. This is an example for aggregation.
 
UML Notation:
Agregation: line connecting the parent and child with a hollow diamond attached to the parent side.
Composition: line the parent and child  with a shaded diamond attached to the parent side
 
Knowing the difference between these is of Paramount importance in a language environment like C++ where you manage the lifetime. In a garbage collected environment it hardly matters. But people carry legacy and thats why this question pops often.
 
Some terminolgy confusion:
Some folks refer to the general has a relationship as Aggregation.
has a relationship in which parent exclusively owns a child as Composite Aggregation (Composition in my article)
has a relationship in which parent shares the child with other parents as Shared Aggregation (Aggregation in my article)
 
 To Summarize:
Composition

Aggregation

Parent controls the lifetime of child

Parent does not control the lifetime of child

Parent creates the child

Parent is usually handed over the child

Parent owns the child exclusively

Parent shares the child with other parents
 
References
 
 
Advertisements
Categories: Uncategorized
  1. Prakash
    October 16, 2006 at 6:29 am

    Excellent Post. I would really appreciate if you can add implementation details (Code level)  also in your examples…
    Also.. the images are not very clear. Please find a better image hosting service… 🙂

  1. November 19, 2010 at 10:59 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: