Aggregation
Weak ownership - parts can exist without the whole.
Aggregation is a “has-a” relationship where one class contains another, but the contained class can exist independently. It represents a weak ownership relationship.
What is Aggregation?
Section titled “What is Aggregation?”Aggregation represents:
- “Has-a” relationship (weak ownership)
- Parts can exist independently of the whole
- Parts can belong to multiple wholes
- Lifecycle independence - parts aren’t destroyed when whole is destroyed
Key Characteristics
Section titled “Key Characteristics”- Weak ownership - Container doesn’t own the parts
- Independent lifecycle - Parts can exist without container
- Multi-ownership - Parts can belong to multiple containers
- Hollow diamond in UML diagrams
Basic Aggregation Example
Section titled “Basic Aggregation Example”Visual Representation
Section titled “Visual Representation”Real-World Example: Shopping Cart
Section titled “Real-World Example: Shopping Cart”Aggregation vs Composition
Section titled “Aggregation vs Composition”| Feature | Aggregation | Composition |
|---|---|---|
| Ownership | Weak | Strong |
| Lifecycle | Independent | Dependent |
| Multi-ownership | Yes | No |
| UML Symbol | Hollow diamond | Filled diamond |
| Example | University → Students | Car → Engine |
Key Takeaways
Section titled “Key Takeaways”When to Use Aggregation
Section titled “When to Use Aggregation”Use Aggregation when:
- Parts can exist without the whole
- Parts can belong to multiple wholes
- You need flexibility in relationships
- Parts have independent lifecycle
- Relationship is “part-of” but not essential
Examples:
- University → Students (students can transfer)
- Shopping Cart → Products (products exist independently)
- Team → Players (players can change teams)
- Library → Books (books can be removed)
- Department → Employees (employees can move departments)