Monday, October 22, 2007

Multiplicity within UML Class Diagrams

Some of my students were asking for a way to remember on which end of an association to put cardinality indicators. UML technically refers to these as the association's multiplicity, rather than cardinality. Dare to be different, I guess.

To understand which cardinality to put where, use the following technique:

  1. Replace association name, with endpoint role names. Multiplicity is expressed with respect to the role.
  2. Remember that the multiplicity is expressed as association participation on a "per object" basis, not on the relationship as a whole.
  3. Read the association between class X and Y using the pattern "An object of class X has as "x's-role" "y's-multiplicity" objects of class Y.
See the example, left, with labeled names of associations employees and head between classes Employee and Department. Consider that a department has one head, and many possible employees, and that an Employee is part of zero or more departments, but may be the head of zero or one of these. It is not obvious on which association endpoint to put each multiplicity notation.

This will result in a new diagram, whether diagrammed or in our head. This diagram shows the roles each class has in each association.To follow the above process, we must first replace named association with named roles. This will The "employees" association name has been replaced with role names of "partOf" and "employs". The "head" association name has been replaced with role names "headOf" and "headedBy". With roles named, its time to place multiplicity indicators on our associations.

First, for the top employs/partOf roles we state that an employee is part of one department, while a department employs one or more employees. We place a 1 on the employs role, and a 1..* on the partOf role. For the headedBy/headOf roles we state that an employee optionally the head of one department, while a department is headed by exactly one employee. This results in our final diagram.

Cross-checking this diagram against our above rule, we see the diagram is stating the following:

An object of class Employee is part of 1 Department.
An object of class Department employs 1 or more Employees.
An object of class Employee is optionally head of 1 Department.
An object of class Department is headed by exactly 1 Employee.
Using this system, you should be able to read multiplicity on class diagrams, and effectively adorn your class diagrams with multiplicity indicators.

Want to learn UML? Consider taking our UML Course.

4 comments:

Anonymous said...

I was trying to refresh my UML memories from school, and your post was useful. Thanks!

Jeff Schmitz said...

Great! If you are located in St. Louis, consider taking our UML course (link in post).

Anonymous said...

Good dispatch and this enter helped me alot in my college assignement. Thank you on your information.

Anonymous said...

Nice post and this enter helped me alot in my college assignement. Say thank you you as your information.