What Is the Difference Between UML Aggregation and Composition?
UML Aggregation vs. Composition - What Have They Common?
An aggregation and a composition are UML associations. They are depicted as a line with a diamond on one end (although, different kinds of diamonds). The diamond points to the parent (container of aggregated objects), the unmarked line end should point to the children (aggregated objects).
UML Aggregation vs. Composition - What is Different?
An aggregation is a type of association that is depicted as a line with a hollow diamond on an end of the line. Aggregated objects can exist without their parent.
A composition is a type of association drawn as a line with a filled diamond on one side. Composed objects cannot exist with their parent.
The difference between them is that parts associated with a parent class using aggregations can be associated also to other classes and can exist with the parent class. On the contrary, the objects, that are associated using a composition, cannot exist without the class they are associated with.
UML Aggregation Example
We have three classes - a library department, a bookcase and a book. A library department is an aggregation of bookcases and a bookcase is an aggregation of books. A book can exist without a bookcase and also a bookcase can exist without the library department.
UML Composition Example
We have three classes representing real-word objects: a book, a chapter, and a page.
A book is a composition of multiple chapters. A chapter is a composition of multiple pages.
The pages cannot exist without their chapter and the chapters cannot exist without the book.