Aggregation — Has-A with Independent Lifecycle
Introduction
A university department has professors. The Computer Science department might have Smith, Jones and Lee. If the university closes the department and moves its work into Electrical & Computer Engineering, the professors do not disappear: they carry on, now in the new department.
That kind of has-a relationship is called aggregation: one object, the whole, holds others, its parts, and the parts can exist without it. The department holds its professors, but it does not make them and it does not end them. Association — Objects That Know Each Other showed objects that keep references to each other; aggregation is an association between a whole and its parts. Composition — Has-A with Dependent Lifecycle comes after it, where the parts go when the whole goes.
Aggregation and Association
In code, aggregation looks like any association: a field that holds references. Department keeps a list of professors, and that list is all there is to it. The difference is in what the relationship means. A plain association says two objects know each other, and neither is in charge: a professor teaches a course, and neither one contains the other. Aggregation says one object is the whole and the others are its parts: the department is the whole, and the professors are its members.
UML marks the whole's end of the line with a hollow diamond, and gives that mark no fixed meaning of its own: "Precise semantics of shared aggregation varies by application area and modeler." Martin Fowler, in a note from 2003, says the hollow diamond "has no semantics beyond that of a regular association", and adds: "It is, as Jim Rumbaugh puts it, a modeling placebo." So the course gives aggregation one plain meaning: a whole whose parts can live on without it, parts that are made outside it and handed in.
Some everyday cases:
- A team has players. If the team breaks up, the players still exist and can join another team.
- A shopping cart has products. If you leave the cart, the products go back to the shelves.
- A department has professors. If the department is dissolved, every professor is still there.
One question tells it apart from composition: when the whole goes away, do the parts survive? If they do, it is aggregation.
UML Notation
Here is the department and its professors as a class diagram, with the objects of the run beside it. As in Association — Objects That Know Each Other, the boxes use the Java names:

UML draws the hollow diamond at the whole's end of the line, here at Department. The open arrowhead at Professor says which way the link can be followed: the department keeps a list of its professors, and a professor keeps no list of departments. Both ends say *. A department may hold no professors at all, as Computer Science does after it is dissolved, and a professor may be in several departments at once, or in none.
The second line is the plain association from Association — Objects That Know Each Other: a professor teaches courses, with no whole and no parts, and 0..1 because a course may have no professor yet.
On the right are the objects of the run below. After the joining calls, Smith is in two departments at once. After Computer Science is dissolved, it holds nobody, Mathematics still holds Smith, and all three professors still exist.
Watch a Department Dissolve
The interactive runs the listing's nine calls on its departments and professors, one call at a time. Watch what the dissolve does to Computer Science: its lines go, the professors stay, and the count of professors that exist never moves.

Code Example
Here is the department and its professors in code. The professors are made first, outside any department, and then handed in: Department never creates a Professor. It keeps references in its professors list, and dissolve empties that list and hands the professors back. Smith joins two departments at once, which aggregation allows:
class Professor:
def __init__(self, name: str, specialization: str) -> None:
self._name = name
self._specialization = specialization
@property
def name(self) -> str:
return self._name
def describe(self) -> str:
return f"Prof. {self._name} ({self._specialization})"
class Department:
def __init__(self, name: str) -> None:
self._name = name
# aggregation: the department holds its professors; it never creates them
self._professors: list[Professor] = []
def add_professor(self, professor: Professor) -> None:
if professor in self._professors:
return
self._professors.append(professor)
print(f" {professor.name} joined {self._name}")
# Leaving ends this one link; the professor stays in any other department
def remove_professor(self, professor: Professor) -> None:
if professor in self._professors:
self._professors.remove(professor)
print(f" {professor.name} left {self._name}")
def get_headcount(self) -> int:
return len(self._professors)
# Dissolving ends the department's links; the professors go on, free to join others
def dissolve(self) -> list[Professor]:
freed = list(self._professors)
self._professors.clear()
print(f" {self._name} dissolved: {len(freed)} professors are free")
return freed
def show(self) -> None:
names = ", ".join(p.name for p in self._professors) or "none"
print(f" {self._name} ({self.get_headcount()}): {names}")
# The professors are made first, outside any department
smith = Professor("Smith", "Algorithms")
jones = Professor("Jones", "Machine Learning")
lee = Professor("Lee", "Databases")
print("Joining:")
cs = Department("Computer Science")
maths = Department("Mathematics")
cs.add_professor(smith)
cs.add_professor(jones)
cs.add_professor(lee)
maths.add_professor(smith) # one professor in two departments at once
cs.show()
maths.show()
print("Dissolving:")
freed = cs.dissolve()
cs.show()
maths.show()
print(" Still here: " + ", ".join(p.describe() for p in freed))
print("Joining again:")
ece = Department("Electrical & Computer Engineering")
for prof in freed:
ece.add_professor(prof)
maths.remove_professor(smith)
maths.show()
ece.show()import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
class Professor {
private final String name;
private final String specialization;
public Professor(String name, String specialization) {
this.name = name;
this.specialization = specialization;
}
public String getName() {
return name;
}
public String describe() {
return "Prof. " + name + " (" + specialization + ")";
}
}
class Department {
private final String name;
// aggregation: the department holds its professors; it never creates them
private final List<Professor> professors = new ArrayList<>();
public Department(String name) {
this.name = name;
}
public void addProfessor(Professor professor) {
if (professors.contains(professor)) {
return;
}
professors.add(professor);
System.out.println(" " + professor.getName() + " joined " + name);
}
// Leaving ends this one link; the professor stays in any other department
public void removeProfessor(Professor professor) {
if (professors.remove(professor)) {
System.out.println(" " + professor.getName() + " left " + name);
}
}
public int getHeadcount() {
return professors.size();
}
// Dissolving ends the department's links; the professors go on, free to join others
public List<Professor> dissolve() {
List<Professor> freed = new ArrayList<>(professors);
professors.clear();
System.out.println(" " + name + " dissolved: " + freed.size()
+ " professors are free");
return freed;
}
public void show() {
String names = professors.stream().map(Professor::getName)
.collect(Collectors.joining(", "));
System.out.println(" " + name + " (" + getHeadcount() + "): "
+ (names.isEmpty() ? "none" : names));
}
}
public class UniversityDemo {
public static void main(String[] args) {
// The professors are made first, outside any department
Professor smith = new Professor("Smith", "Algorithms");
Professor jones = new Professor("Jones", "Machine Learning");
Professor lee = new Professor("Lee", "Databases");
System.out.println("Joining:");
Department cs = new Department("Computer Science");
Department maths = new Department("Mathematics");
cs.addProfessor(smith);
cs.addProfessor(jones);
cs.addProfessor(lee);
maths.addProfessor(smith); // one professor in two departments at once
cs.show();
maths.show();
System.out.println("Dissolving:");
List<Professor> freed = cs.dissolve();
cs.show();
maths.show();
System.out.println(" Still here: " + freed.stream().map(Professor::describe)
.collect(Collectors.joining(", ")));
System.out.println("Joining again:");
Department ece = new Department("Electrical & Computer Engineering");
for (Professor prof : freed) {
ece.addProfessor(prof);
}
maths.removeProfessor(smith);
maths.show();
ece.show();
}
}#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
class Professor {
public:
Professor(std::string name, std::string specialization)
: name_(std::move(name)), specialization_(std::move(specialization)) {}
const std::string& getName() const { return name_; }
std::string describe() const {
return "Prof. " + name_ + " (" + specialization_ + ")";
}
private:
std::string name_;
std::string specialization_;
};
// The department keeps plain pointers: it holds its professors and owns none of them
class Department {
public:
explicit Department(std::string name) : name_(std::move(name)) {}
void addProfessor(Professor& professor) {
if (std::find(professors_.begin(), professors_.end(), &professor)
!= professors_.end()) {
return;
}
professors_.push_back(&professor);
std::printf(" %s joined %s\n", professor.getName().c_str(), name_.c_str());
}
// Leaving ends this one link; the professor stays in any other department
void removeProfessor(Professor& professor) {
auto it = std::find(professors_.begin(), professors_.end(), &professor);
if (it != professors_.end()) {
professors_.erase(it);
std::printf(" %s left %s\n", professor.getName().c_str(), name_.c_str());
}
}
int getHeadcount() const { return static_cast<int>(professors_.size()); }
// Dissolving ends the department's links; the professors go on, free to join others
std::vector<Professor*> dissolve() {
std::vector<Professor*> freed = professors_;
professors_.clear();
std::printf(" %s dissolved: %d professors are free\n", name_.c_str(),
static_cast<int>(freed.size()));
return freed;
}
void show() const {
std::string names;
for (const Professor* p : professors_) {
names += (names.empty() ? "" : ", ") + p->getName();
}
std::printf(" %s (%d): %s\n", name_.c_str(), getHeadcount(),
names.empty() ? "none" : names.c_str());
}
private:
std::string name_;
std::vector<Professor*> professors_; // aggregation: held, never created here
};
int main() {
// The professors are made first, outside any department
Professor smith("Smith", "Algorithms");
Professor jones("Jones", "Machine Learning");
Professor lee("Lee", "Databases");
std::printf("Joining:\n");
Department cs("Computer Science");
Department maths("Mathematics");
cs.addProfessor(smith);
cs.addProfessor(jones);
cs.addProfessor(lee);
maths.addProfessor(smith); // one professor in two departments at once
cs.show();
maths.show();
std::printf("Dissolving:\n");
std::vector<Professor*> freed = cs.dissolve();
cs.show();
maths.show();
std::string still;
for (const Professor* p : freed) {
still += (still.empty() ? "" : ", ") + p->describe();
}
std::printf(" Still here: %s\n", still.c_str());
std::printf("Joining again:\n");
Department ece("Electrical & Computer Engineering");
for (Professor* prof : freed) {
ece.addProfessor(*prof);
}
maths.removeProfessor(smith);
maths.show();
ece.show();
return 0;
}class Professor {
constructor(
private readonly name: string,
private readonly specialization: string,
) {}
getName(): string {
return this.name;
}
describe(): string {
return `Prof. ${this.name} (${this.specialization})`;
}
}
class Department {
// aggregation: the department holds its professors; it never creates them
private readonly professors: Professor[] = [];
constructor(private readonly name: string) {}
addProfessor(professor: Professor): void {
if (this.professors.includes(professor)) {
return;
}
this.professors.push(professor);
console.log(` ${professor.getName()} joined ${this.name}`);
}
// Leaving ends this one link; the professor stays in any other department
removeProfessor(professor: Professor): void {
const i = this.professors.indexOf(professor);
if (i >= 0) {
this.professors.splice(i, 1);
console.log(` ${professor.getName()} left ${this.name}`);
}
}
getHeadcount(): number {
return this.professors.length;
}
// Dissolving ends the department's links; the professors go on, free to join others
dissolve(): Professor[] {
const freed = [...this.professors];
this.professors.length = 0;
console.log(` ${this.name} dissolved: ${freed.length} professors are free`);
return freed;
}
show(): void {
const names = this.professors.map((p) => p.getName()).join(", ") || "none";
console.log(` ${this.name} (${this.getHeadcount()}): ${names}`);
}
}
// The professors are made first, outside any department
const smith = new Professor("Smith", "Algorithms");
const jones = new Professor("Jones", "Machine Learning");
const lee = new Professor("Lee", "Databases");
console.log("Joining:");
const cs = new Department("Computer Science");
const maths = new Department("Mathematics");
cs.addProfessor(smith);
cs.addProfessor(jones);
cs.addProfessor(lee);
maths.addProfessor(smith); // one professor in two departments at once
cs.show();
maths.show();
console.log("Dissolving:");
const freed = cs.dissolve();
cs.show();
maths.show();
console.log(` Still here: ${freed.map((p) => p.describe()).join(", ")}`);
console.log("Joining again:");
const ece = new Department("Electrical & Computer Engineering");
for (const prof of freed) {
ece.addProfessor(prof);
}
maths.removeProfessor(smith);
maths.show();
ece.show();using System;
using System.Collections.Generic;
using System.Linq;
public class Professor
{
private readonly string specialization;
public Professor(string name, string specialization)
{
Name = name;
this.specialization = specialization;
}
public string Name { get; }
public string Describe() => $"Prof. {Name} ({specialization})";
}
public class Department
{
private readonly string name;
// aggregation: the department holds its professors; it never creates them
private readonly List<Professor> professors = new();
public Department(string name)
{
this.name = name;
}
public void AddProfessor(Professor professor)
{
if (professors.Contains(professor))
{
return;
}
professors.Add(professor);
Console.WriteLine($" {professor.Name} joined {name}");
}
// Leaving ends this one link; the professor stays in any other department
public void RemoveProfessor(Professor professor)
{
if (professors.Remove(professor))
{
Console.WriteLine($" {professor.Name} left {name}");
}
}
public int GetHeadcount() => professors.Count;
// Dissolving ends the department's links; the professors go on, free to join others
public List<Professor> Dissolve()
{
var freed = new List<Professor>(professors);
professors.Clear();
Console.WriteLine($" {name} dissolved: {freed.Count} professors are free");
return freed;
}
public void Show()
{
string names = string.Join(", ", professors.Select(p => p.Name));
string shown = names == "" ? "none" : names;
Console.WriteLine($" {name} ({GetHeadcount()}): {shown}");
}
}
public static class UniversityDemo
{
public static void Main()
{
// The professors are made first, outside any department
var smith = new Professor("Smith", "Algorithms");
var jones = new Professor("Jones", "Machine Learning");
var lee = new Professor("Lee", "Databases");
Console.WriteLine("Joining:");
var cs = new Department("Computer Science");
var maths = new Department("Mathematics");
cs.AddProfessor(smith);
cs.AddProfessor(jones);
cs.AddProfessor(lee);
maths.AddProfessor(smith); // one professor in two departments at once
cs.Show();
maths.Show();
Console.WriteLine("Dissolving:");
List<Professor> freed = cs.Dissolve();
cs.Show();
maths.Show();
string still = string.Join(", ", freed.Select(p => p.Describe()));
Console.WriteLine(" Still here: " + still);
Console.WriteLine("Joining again:");
var ece = new Department("Electrical & Computer Engineering");
foreach (Professor prof in freed)
{
ece.AddProfessor(prof);
}
maths.RemoveProfessor(smith);
maths.Show();
ece.Show();
}
}package main
import (
"fmt"
"slices"
"strings"
)
type Professor struct {
name string
specialization string
}
func NewProfessor(name, specialization string) *Professor {
return &Professor{name: name, specialization: specialization}
}
func (p *Professor) Name() string { return p.name }
func (p *Professor) Describe() string {
return fmt.Sprintf("Prof. %s (%s)", p.name, p.specialization)
}
// The department keeps pointers: it holds its professors and owns none of them
type Department struct {
name string
professors []*Professor // aggregation: held, never created here
}
func NewDepartment(name string) *Department {
return &Department{name: name}
}
func (d *Department) AddProfessor(p *Professor) {
if slices.Contains(d.professors, p) {
return
}
d.professors = append(d.professors, p)
fmt.Printf(" %s joined %s\n", p.name, d.name)
}
// Leaving ends this one link; the professor stays in any other department
func (d *Department) RemoveProfessor(p *Professor) {
if i := slices.Index(d.professors, p); i >= 0 {
d.professors = slices.Delete(d.professors, i, i+1)
fmt.Printf(" %s left %s\n", p.name, d.name)
}
}
func (d *Department) Headcount() int { return len(d.professors) }
// Dissolving ends the department's links; the professors go on, free to join others
func (d *Department) Dissolve() []*Professor {
freed := slices.Clone(d.professors)
d.professors = nil
fmt.Printf(" %s dissolved: %d professors are free\n", d.name, len(freed))
return freed
}
func (d *Department) Show() {
names := make([]string, 0, len(d.professors))
for _, p := range d.professors {
names = append(names, p.name)
}
shown := strings.Join(names, ", ")
if shown == "" {
shown = "none"
}
fmt.Printf(" %s (%d): %s\n", d.name, d.Headcount(), shown)
}
func main() {
// The professors are made first, outside any department
smith := NewProfessor("Smith", "Algorithms")
jones := NewProfessor("Jones", "Machine Learning")
lee := NewProfessor("Lee", "Databases")
fmt.Println("Joining:")
cs := NewDepartment("Computer Science")
maths := NewDepartment("Mathematics")
cs.AddProfessor(smith)
cs.AddProfessor(jones)
cs.AddProfessor(lee)
maths.AddProfessor(smith) // one professor in two departments at once
cs.Show()
maths.Show()
fmt.Println("Dissolving:")
freed := cs.Dissolve()
cs.Show()
maths.Show()
still := make([]string, 0, len(freed))
for _, p := range freed {
still = append(still, p.Describe())
}
fmt.Println(" Still here: " + strings.Join(still, ", "))
fmt.Println("Joining again:")
ece := NewDepartment("Electrical & Computer Engineering")
for _, prof := range freed {
ece.AddProfessor(prof)
}
maths.RemoveProfessor(smith)
maths.Show()
ece.Show()
}It prints:
Joining:
Smith joined Computer Science
Jones joined Computer Science
Lee joined Computer Science
Smith joined Mathematics
Computer Science (3): Smith, Jones, Lee
Mathematics (1): Smith
Dissolving:
Computer Science dissolved: 3 professors are free
Computer Science (0): none
Mathematics (1): Smith
Still here: Prof. Smith (Algorithms), Prof. Jones (Machine Learning), Prof. Lee (Databases)
Joining again:
Smith joined Electrical & Computer Engineering
Jones joined Electrical & Computer Engineering
Lee joined Electrical & Computer Engineering
Smith left Mathematics
Mathematics (0): none
Electrical & Computer Engineering (3): Smith, Jones, Lee
After the dissolve, Computer Science holds nobody, yet Mathematics still holds Smith, and all three professors are still there to join Electrical & Computer Engineering. When Smith then leaves Mathematics, only that one link ends: Smith stays in the new department. The professors are held by the program's own variables, smith, jones and lee, so nothing a department does can end them.
Three Questions for Aggregation
Three questions help you decide whether a has-a relationship is aggregation.
1. Does the part survive when the whole goes? A dissolved department leaves its professors in place, so that is aggregation. An order item means nothing once its order is gone, so that is composition.
2. Is the part made outside and handed in? The professors exist before any department does, and add_professor receives one that already exists. An order makes its own items when you add a line to it, which points to composition.
3. Can a part belong to more than one whole at a time? Smith is in Computer Science and Mathematics at once. Composition does not allow that: UML's composite aggregation "requires a part object be included in at most one composite object at a time". A room belongs to one building only, which points to composition.
When all three answers point to aggregation, draw the hollow diamond. When the part's life depends on the whole, it is composition, which Composition — Has-A with Dependent Lifecycle covers.
Common Mistakes
Mistake 1: aggregation where the part depends on the whole. A hollow diamond from Order to OrderItem says the items can outlive the order. They cannot: an order item is made by its order and means nothing without it. That is composition, drawn with a filled diamond.
Mistake 2: calling every has-a relationship aggregation. A person has a phone number, but the number is a value the person holds, an attribute. It is not a part made outside and handed in. Ask whether one object really holds the others as its members; if it does not, use a plain association or an attribute.
Mistake 3: diamonds everywhere. A hollow diamond on every list makes a diagram look precise when it says nothing more than a plain line. Fowler's advice is to explain it: "I would advise not using it yourself without some form of explanation." Draw the diamond where the whole and its parts matter to the design, and say what you mean by it.
Key Takeaways
- Aggregation is an association between a whole and its parts, where the parts can live without the whole.
- The parts are usually made outside the whole and handed in, and a part may belong to several wholes at once.
- In code it is a field of references, like any association. UML marks the whole's end with a hollow diamond, and gives that mark no meaning beyond association, so say what you mean when you draw it.
- Composition — Has-A with Dependent Lifecycle comes next: a whole whose parts go when it goes.