I want to understand the difference between composition and aggregation relationship.
Does anyone know a site that explains the UML class diagrams for all relationships?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Composition and aggregation have to do with cascading delete behavior.
Do the child objects have a life of their own beyond the parent? If yes, you need aggregation.
If the parent is deleted, do the children need to be deleted as well? If yes, you need composition.
So let’s say you have a model where there’s a School class, a Building class, and a Student class. A School has a one-to-many relationship with Building and a one-to-many relationship with Student.
The School-to-Building relationship is an example of composition. If you close the School, you might decide to bulldoze the buildings.
The School-to-Student relationship is aggregation. If you close the school, you certainly won’t decide to murder all the Students.
You can read what Uncle Bob Martin has to say about it here.