UML Association vs Aggregation vs Composition

Categories:

Recommended

UML Association vs Aggregation vs Composition

5-6 minutes

Consider the differences and similarities between the classes of the following objects: pets , dogs , tails , owners .

The figure below shows the three types of association connectors: association, aggregation, and composition. We will go over them in this UML guide.

The figure below shows a generalization. We will talk about it later on in this UML guide.

Association

If two classes in a model need to communicate with each other, there must be a link between them, and that can be represented by an association (connector).

Association can be represented by a line between these classes with an arrow indicating the navigation direction . In case an arrow is on both sides, the association is known as a bidirectional association.

We can indicate the multiplicity of an association by adding multiplicity adornments to the line denoting the association. The example indicates that a Student has one or more Instructors:

  • A single student can associate with multiple teachers:
  • The example indicates that every Instructor has one or more Students:
  • We can also indicate the behavior of an object in an association (i.e., the role of an object)using role names.

Association vs Aggregation vs Composition

The question “What is the difference between association, aggregation, and composition “has been frequently asked lately.

Aggregation and Composition are subsets of association meaning they are specific cases of association . In both aggregation and composition object of one class “owns” object of another class. But there is a subtle difference:

Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.

Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don’t exist separate to a House.

Composition Example:

We should be more specific and use the composition link in cases where in addition to the part-of relationship between Class A and Class B – there’s a strong lifecycle dependency between the two, meaning that when Class A is deleted then Class B is also deleted as a result

Aggregation Example:

It’s important to note that the aggregation link doesn’t state in any way that Class A owns Class B nor that there’s a parent-child relationship (when parent deleted all its child’s are being deleted as a result) between the two. Actually, quite the opposite! The aggregation link is usually used to stress the point that Class A instance is not the exclusive container of Class B instance, as in fact the same Class B instance has another container/s.

Summing it up –

To sum it up association is a very generic term used to represent when one class used the functionalities provided by another class. We say it’s a composition if one parent class object owns another child class object and that child class object cannot meaning fully exist without the parent class object. If it can then it is called Aggregation.

Category:

VP Flipbook Maker

This flipbook was powered by Visual Paradigm Online. You can create one as well by upload your own PDF documents. Try out this online flipbook maker for free now!