What is a Unary association in UML

Viewed 477

While I googled to understand the Unary associations, I got the following two explanations:

the first is:

A unary relationship is when both participants in the relationship are the same entity. For Example: Subjects may be prerequisites for other subjects, or one employee manages many Employees.

and the second is:

Class B knows about ClassA.

Class A does not know about ClassB.

Now lets look at the following example:

You can see the Person and Address relationship below. We call this relationship as has-a relationship since person has a address. So Person knows the address but address does not know anything about person

Am I misunderstanding something?

2 Answers

Common language

The arity of an association is about how many classes are associated. This is an ambiguous concept since some understand different classes, whereas others understand instances.

When applied to unary, the first interpretation would mean reflexive association (or self-association, i.e. a class associated with iteself), whereas the second would mean a class associated with nothing (not very useful: any class could be associated with nothing else).

UML perspective

Fortunately, the UML specifications are much more precise than the common language:

An Association specifies a semantic relationship that can occur between typed instances. It has at least two memberEnds represented by Properties, each of which has the type of the end. More than one end of the Association may have the same type.

So in UML there is no "unary association". It's binary, ternary, or n-ary (terms used in the specs). There is no special term in UML for a binary association with the same class at both ends. But reflexive or self-association are terms which are more popular than unary.

E/R modeling

The term "unary" is popular in the context of entity-relationship modeling, to describe a relation in a relational database. Relations correspond more or less to an association in UML, and entities to classes, but there are some subtle semantic differences. E/R has its foundation in the set theory. And if a relation is between the same entities, it means in fact that only one set is involved. This is probably why unary is more popular in this context.

Merriam-webster defines Unary as

having, consisting of, or acting on a single element, item, or component

So the first explanation is correct one since this type of association acts on a single class. The term Unary however is not used in UML and might be confusing.
UML uses the term binary to indicate that an association has two ends, and ternary or n-ary to indicate an association has multiple ends. The Unary association you are talking about is actually a binary association to itself, also known as reflexive association.

enter image description here

Not to be confused with a Unidirectional association, which is an association that is only navigable (has an arrow) to one side.

enter image description here

Related