I understand what composition is in OOP, but I am not able to get a clear idea of what Aggregation is. Can someone explain?
9 Answers
Simple rules:
- A "owns" B = Composition : B has no meaning or purpose in the system without A
- 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.
-
18
-
2And how is aggregation different from any other relationship between two sorts of entities? Mar 31, 2011 at 12:14
-
89@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. Mar 31, 2011 at 14:06
-
4what is missing is an implementation example for a complete understanding... Oct 26, 2013 at 10:04
-
2What about employee when a company ceases to do business? Employee and people are different entities right? So can I say company is composition of employees?– arjunJun 16, 2015 at 2:13
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.
@Curtis Batt provides an excellent definition in his answer: Aggregation vs Composition
-
3In 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. Mar 24, 2011 at 5:20
-
1it has nothing to do with destruction! UML does not define garbage collection system. Mar 24, 2011 at 10:01
-
2i 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. Mar 24, 2011 at 14:13
-
1I agree that my answer is severely lacking - but so is the WikiPedia article...– HorusKolMar 24, 2011 at 22:13
-
The difference between composition and aggregation is clear. The problem with aggregation is that it's unclear how it is different from ordinary association. Aug 16, 2016 at 12:20
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);
}
}
-
6What is the difference between aggregation and association that is neither composition nor aggregation? Mar 24, 2014 at 14:54
Composition(mixture) is a way to combine simple objects or data types into more complex ones. Compositions are a critical building block of many basic data structures
Aggregation(collection) 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
╔═══════════╦═════════════════════════╦═══════════════════════╗
║ ║ Aggregation ║ Composition ║
╠═══════════╬═════════════════════════╬═══════════════════════╣
║ Life time ║ Have their own lifetime ║ Owner's life time ║
║ Relation ║ Has ║ part-of ║
║ Example ║ Car has driver ║ Engine is part of Car ║
╚═══════════╩═════════════════════════╩═══════════════════════╝
Both denotes relationship between object and only differ in their strength.
UML notations for different kind of dependency between two classes
Composition : Since Engine is part-of Car, relationship between them is Composition. Here is how they are implemented between Java classes.
public class Car {
//final will make sure engine is initialized
private final Engine engine;
public Car(){
engine = new Engine();
}
}
class Engine {
private String type;
}
Aggregation : Since Organization has Person as employees, relationship between them is Aggregation. Here is how they look like in terms of Java classes
public class Organization {
private List employees;
}
public class Person {
private String name;
}
-
this doesn't seem to offer anything substantial over points made and explained in prior 12 answers– gnatJul 1, 2015 at 7:05
-
-
Here the List of Employees is the part of Organization Object. How can this be aggregation ? Feb 12, 2016 at 9:59
-
-
I love this answer. It finally explained to me properly what the difference between aggregation and composition is. Apr 1, 2018 at 8:33
There is no single explanation. Different authors mean different things by aggregation. Most don't really mean anything specific by it.
-
5This is the correct answer. I've read it in two books, one of them being Martin Fowler's UML Distilled. Mar 25, 2011 at 12:52
-
5
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!]
-
This is nonstandard. The more standard distinction is that in composition, the members are always members of exactly one container. Tires can exist apart from cars, but if you only want to model tires on cars, the relationship becomes a composition. You will then still be able to change tires, but only as an atomic operation. Feb 1 at 13:22
-
@reinierpost nonstandard, according to whom? Perhaps I misunderstood what you wrote but it sounds like by this definition, two spare tires in the trunk would be composition, as they are both members of one container. Feb 3 at 8:19
-
You're right, 'standard' isn't really an appropriate term to use here. But in my limited experience, composition is defined by a restriction in cardinality on the relation and the rest is handwaving. Feb 3 at 10:57
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.
How about this simple example:
An array of objects is a composition. An array of pointers to objects is an aggregation.
If I delete the first one, its contents vanish with it. The second one, on the other hand can vanish without affecting its members existence unless there is a specific method that deletes each object as its pointer is deleted.
-
3this doesn't seem to add anything substantial over points made and explained in prior 11 answers– gnatSep 5, 2014 at 16:23
-
Respectfully disagree, @gnat. This is a helpful example of how the two could be implemented. People learn better with examples. (I came here to check my understanding that a pointer member was likely to be an aggregation, and an object member a composition. This is the only answer to directly address that.) Apr 9, 2019 at 17:40
Semantically, all sets are made of subsets, right? Therefore:
The aggregation is when those subsets exists independently of the father set. As a monitor can be unplugged from the computer to be connected to another.
The composition is when those subsets depends of the existence of the father set. As a leaf is a part of a tree or liver is a part of a body.
These concepts talks about the kind of dependency between two objects or classes, conceptually. Directly in a program, in an aggregation, when the father object disposes, the aggregate objects should be disposed too. In the same scenario for a composition, composite son objects will persist then the father object dispenses.