Site icon Henry's Notes

UML Association vs Aggregation vs Composition with EXAMPLE

Relationships in UML diagram are used to represent a connection between various things. A relationship is a connection amongst things such as structural, behavioral, or grouping things in the unified modeling language.

Following are the different types of standard relationships in UML,

Other than these, UML allows the use of aggregation and a composition relationship.

UML Association

It is a structural relationship that represents objects can be connected or associated with another object inside the system. Following constraints can be applied to the UML Association relationship.

We can also create a class that has UML Association properties; it is called as an association class.

Reflexive Association

The reflexive association is a subtype of association relationship in UML. In a reflexive association, the instances of the same class can be related to each other. An instance of a class is also said to be an object.

Reflexive association states that a link or a connection can be present within the objects of the same class.

Example:

Let us consider an example of a class fruit.

The fruit class has two instances, such as mango and apple. Reflexive association states that a link between mango and apple can be present as they are instances of the same class, such as fruit.

Reflexive Association

Directed Association

As the name suggests, the directed association is related to the direction of flow within association classes.

In a directed association, the flow is directed. The association from one class to another class flows in a single direction only.

It is denoted using a solid line with an arrowhead.

UML Association Example:

You can say that there is a directed association relationship between a server and a client.

A server can process the requests of a client. This flow is unidirectional, that flows from server to client only. Hence a directed association relationship can be present within servers and clients of a system.

Directed Association

UML Composition

It is not a standard UML Relationship, but it is still used in various applications.

Composite aggregation is a subtype of aggregation relation with characteristics as:

Composite aggregation is described as a binary association decorated with a filled black diamond at the aggregate (whole) end.

The folder could contain many files, while each File has exactly one Folder parent. If a folder is deleted, all contained files are removed as well.

In a composite aggregation, an object may be a part of only one composite at a time.

UML Composition Example:

For example, in a windowing system, a Frame belongs to precisely one Window. In a composite aggregation, the whole system is responsible for the disposition of its parts, which means that the composite must manage the creation and destruction of its parts.

Composition

UML Aggregation

An aggregation is a subtype of an association relationship in UML. Aggregation and composition are both the types of association relationship in UML. An aggregation relationship can be described in simple words as “an object of one class can own or access the objects of another class.”

In an aggregation relationship, the dependent object remains in the scope of a relationship even when the source object is destroyed.

UML Aggregation Example:

Let us consider an example of a car and a wheel.

A car needs a wheel to function correctly, but a wheel doesn’t always need a car. It can also be used with the bike, bicycle, or any other vehicles but not a particular car. Here, the wheel object is meaningful even without the car object. Such type of relationship is called UML Aggregation relation.

Aggregation

Association vs. Aggregation vs. Composition

Association Aggregation Composition
Association relationship is denoted using an arrow. Aggregation relationship is denoted using a straight line with an empty arrowhead at one end. Composition relationship is denoted using a straight line with a filled arrowhead at any one of the ends.
Association can exist between two or more classes in UML. Aggregation is a part of an association relationship. The composition is a part of an association relationship.
There can be one-one, one-many, many-one, and many-many association present between the association classes. Aggregation is considered as a weak type of association. The composition is considered as a strong type of association.
In an association relationship, one or more objects can be associated with each other. In an aggregation relationship, objects that are associated with each other can remain in the scope of a system without each other. In a composition relationship, objects that are associated with each other cannot remain in the scope without each other.
Objects are linked with each other. Linked objects are not dependent upon the other object. Objects are highly dependent upon each other.
In UML Association, deleting one element may or may not affect another associated element. In UML Aggregation, deleting one element does not affect another associated element. In UML Composition, deleting one element affects another associated element.
Example:
A teacher is associated with multiple students.
Or
a teacher provides instructions to the students.
Example:
A car needs a wheel, but it doesn’t always require the same wheel. A car can function adequately with another wheel as well.
Example:
A file is placed inside the folder. If one deletes the folder, then the file associated with that given folder is also deleted.

Summary

 

Source: guru99

Exit mobile version