Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I understand what composition is in OOP ,but I am not able to get a clear idea of what Aggregation is . Can someone explain ?

share|improve this question
add comment

7 Answers

up vote 77 down vote accepted

Simple rules:

  1. A "owns" B = Composition : B has no meaning or purpose in the system without A
  2. A "uses" B = Aggregation : B exists independently (conceptually) from A

Example 1:

A Company is an aggregation of People. A Company is a composition of Accounts. When a Company ceases to do business its Accounts cease to exist but its People continue to exist.

Example 2: (very simplified)

A Text Editor owns a Buffer (composition). A Text Editor uses a File (aggregation). When the Text Editor is closed, the Buffer is destroyed but the File itself is not destroyed.

share|improve this answer
7  
Last sentence should be: A Text Editor owns a Buffer(composition). A Text Editor uses a File (aggregation). When you apply this correction, I will change my stance on your post from negative (confusing) to positive. :) –  Ed James Mar 25 '11 at 8:37
2  
So is a car an aggregate or a composition of its parts? –  reinierpost Mar 31 '11 at 12:13
1  
And how is aggregation different from any other relationship between two sorts of entities? –  reinierpost Mar 31 '11 at 12:14
10  
@reinierpost In reality, a car is an aggregation of parts, and parts are simply an aggregation of molecules... However, in a model it all depends on your requirements. Is it important to treat the engine as a separate entity so that you can track its lifetime independent of the car? Can you reuse the exact same engine in another car? If so, then you probably want aggregation. Otherwise you want a composition because you don't care about engines that aren't part of cars, nor do you care about reusing engines. –  Curtis Batt Mar 31 '11 at 14:06
1  
what is missing is an implementation example for a complete understanding... –  Chesnokov Yuriy Oct 26 '13 at 10:04
show 2 more comments

From http://en.wikipedia.org/wiki/Object_composition

Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.

So - while you have an ownership relationship with composition the owned object is also destroyed when the owner is - an aggregation (and the objects contained) can exist independently.

--

Update: Apologies - this answer is far too simplistic in hindsight.

c.batt provides an excellent definition in his answer: Aggregation vs Composition

share|improve this answer
3  
In the example you quote the composition is a one-to-many and the aggregation also has a one-to-many relationship implied, though here it could also be a many-to-many relationship for the aggregation (we can suppose possible that a teacher can teach in multiple departments). Whereas a department cannot be part of multiple universities. Composition implies ownership whereas aggregation does not go beyond relationship. The quote is correct but the comment is not. –  Newtopian Mar 24 '11 at 5:20
1  
it has nothing to do with destruction! UML does not define garbage collection system. –  Display Name Mar 24 '11 at 10:01
1  
i think the wikipedia link is getting reflexive upvotes, but this is a terrible definition - as @bold pointed out these relationships have nothing to do with GC. This also falls apart when an object is the component of two other objects, such as the ball in a ball-joint joining two artificial limbs. The Component relationship is about functional dependence. –  Steven A. Lowe Mar 24 '11 at 14:13
1  
I agree that my answer is severely lacking - but so is the WikiPedia article... –  HorusKol Mar 24 '11 at 22:13
add comment

There is no single explanation. Different authors mean different things by aggregation. Most don't really mean anything specific by it.

share|improve this answer
2  
This is the correct answer. I've read it in two books, one of them being Martin Fowler's UML Distilled. –  davidhaskins Mar 25 '11 at 12:52
add comment
  • Composition is an Association

  • Aggregation is an Association

  • Composition is a strong Association (If the life of contained object totally depends on the container object, it is called strong association)

  • Aggregation is a weak Association (If the life of contained object doesn't depends on the container object, it is called weak association)

Example:

class Contained {
    public void disp() {
        System.out.println("disp() of Contained A");
    }
}

public class Container {
    private Contained c;

    //Composition
    Container() {
        c = new Contained(); 
    }

    //Association 
    public Contained getC() {
        return c;
    }

    public void setC(Contained c) {
        this.c = c;
    }     

    public static void main(String[] args) {
        Container container = new Container();
        Contained contained = new Contained();
        container.setC(contained);
    } 
}
share|improve this answer
    
What is the difference between aggregation and association that is neither composition nor aggregation? –  reinierpost Mar 24 at 14:54
add comment

aggregation is a simple collection, like a bag of marbles

composition implies internal/functional dependencies, like the hinges on a box

cars aggregate passengers; they get in and out without breaking the car's functionality

the tires are components; remove one and the car no longer functions correctly

[note: the spare tire is an aggregate!]

share|improve this answer
add comment

I always look at composition as 'needs a', i.e. a car needs an engine, and I look at aggregation as 'things related for a purpose'. So staying with the car analogy, my aggregation may be to represent a journey which may involve bringing a car and passengers together. The journey does not own the car or the passengers, I'm aggregating data that is related for a specific scenario. When the journey is completed the car and the passengers go on. When a car is ended, the car and it's engine are normally destroyed together.

share|improve this answer
add comment

Is easy to understand when you think in terms of C++ language:

/* Composition */
class B
{
public:
  int i;
};
class A
{
public:
  B x; /* "x" is a component of any instace of A */
};
void main()
{
  A *a = new A(); /* create one instance of A AND create the component "x" */
  a->x.i = 123;
  delete(a); /* destroy the object referenced by "a" AND the component "x" */
}

/* Aggregation */
class B
{
public:
  int i;
};
class A
{
public:
  B *x; /* "x" is a reference to one object of type B */
};
void main()
{
  A *a = new A();  /* create one instance of A */
  B *b = new B();  /* create one instance of B */
  a->x = b;        /* "x" references the instace of B */
  a->x->i = 123;
  delete(a); /* destroy object referenced by "a" BUT NOT destroy the object referenced by "b" */
}
share|improve this answer
1  
Don't just dump a piece of code; why not explain in prose what your code does? –  Martijn Pieters Feb 8 at 10:18
    
C++ is a formal language. The previous code explains in a formal fashion the concepts of Composition and Aggregation. I believe that previous code is enough simple. –  user119088 Feb 9 at 7:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.