What is difference between Composition and Aggregation (Composition vs Aggregation) - Notes

What is difference between Composition and Aggregation (Composition vs Aggregation)

  • 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.

What is difference between Composition and Aggregation (Composition vs Aggregation)