Aggregation versus Composition [closed]

As a rule of thumb:
enter image description here

class Person {
    private Heart heart;
    private List<Hand> hands;
}

class City {
    private List<Tree> trees;
    private List<Car> cars
}

In composition (Person, Heart, Hand), “sub objects” (Heart, Hand) will be destroyed as soon as Person is destroyed.

In aggregation (City, Tree, Car) “sub objects” (Tree, Car) will NOT be destroyed when City is destroyed.

The bottom line is, composition stresses on mutual existence, and in aggregation, this property is NOT required.

Leave a Comment