Class diagram
A class diagram gives an overview of the structure of a system by showing its
classes and the relationships among them. Class diagrams are
static - they display what interacts but not what happens when
they interact.
The diagram below models a customer order from a retail catalog. The
central class is the Order. Associated with the Order is the
Customer making the purchase and the Payment. A Payment is one of
three kinds: Cash, Check, or Credit. The order contains OrderDetails
(line items), each with its associated Item.
UML class notation is a rectangle divided into three parts:
class name, attributes, and
operations. Names of abstract classes,
such as Payment, are in italics. Relationships between classes are
the connecting links.
Our class diagram has three kinds of relationships.
- Association: a relationship between instances of the two
classes. There is an association between two classes if an
instance of one class must know about the other in order to
perform its work. In a diagram, an association is a link
connecting two classes.
- Aggregation: an association in which one class belongs to a
collection. An aggregation has a diamond end pointing to the
part containing the whole. In our diagram, Order "has a"
collection of OrderDetails.
- Generalization: an inheritance link indicating one class is
a superclass of the other. A generalization has a triangle
pointing to the superclass. Payment is a superclass of Cash,
Check, and Credit.
An association has two ends. An end may have a
role name to
clarify the nature of the association. For example, an OrderDetail
is a line item of each Order.
A navigability arrow on an association shows which direction the
association can be traversed or queried. An OrderDetail can be
queried about its Item, but Item cannot query OrderDetail. The arrow also
lets you know who "owns" the association's implementation; in this
case, OrderDetail has an Item. Associations with no navigability
arrows are bi-directional.
The multiplicity of an association end is the number of possible
instances of the class associated with a single instance of the
other end. Multiplicities are single numbers or ranges of numbers.
In our example, there can be only one Customer for each Order, but a
Customer can have any number of Orders.
This table gives the most common multiplicities.
|
Multiplicities |
Meaning |
|
0..1 |
Zero or One instance.
The notation n .. m indicates n to m instances. |
|
0..* or * |
No limit on the number of instances (including none). |
|
1 |
exactly one instance |
|
1..* |
at least one instance |
A class diagram can show even more information such as:
- Compositions
- Class member visibility and scope
- Dependencies
- Constraints
- Interfaces
Associations in which an object is part of a whole are
called aggregations. Composition
signifies a strong association in which the part
can belong to only one whole - the part cannot exist without the
whole. Composition is denoted by a filled diamond at the whole end.
This diagram shows that a BoxOffice belongs to exactly one MovieTheater.
Or in other words the MovieTheater "owns a" BoxOffice. Destroy the MovieTheater and the BoxOffice goes away!
The collection of Movies is not so closely bound to the MovieTheater.
In this case the MovieTheater "has a" Movie.
The class notation is a 3-piece rectangle with the class name,
attributes, and operations. Attributes and operations can be labeled
according to access and scope. Here is a new expanded Order class.
The illustration uses the following UML conventions.
- Static members are underlined. Instance members are not.
- The operations follow this form:
<access specifier> <name> ( <parameter list>) : <return type>
- Access specifiers appear in front of each member.
Symbol |
Access |
|
+ |
public |
|
- |
private |
|
# |
protected |
A dependency is a relation between two classes in which a change
in one may force changes in the other. Dependencies are drawn as
dotted lines. In the diagram below, the Co_op class depends on the Company
to which a student has been assigned on-the-job training.
If you decide to modify Company, you may have to change the Co_op
class as
well.
A constraint is a condition that every implementation of the
design must satisfy. Constraints can be placed on any kind of model
element. They are written in curly braces { }.
Our diagram has two constraints. The one near the top says that
a Section can be part of a CourseSchedule only if it is not
canceled. The {xor} constraint says that a Student must have a
CourseSchedule or a Co_op position but not both.
An interface is a set of operation signatures. In C++,
interfaces are implemented as abstract classes with only pure
virtual members. In Java and Ada, they're implemented directly.
The class diagram below is a model of a professional conference.
The classes of interest to the conference are SessionTalk, which is
a single presentation, and Session, which is a one-day collection of
related SessionTalks. The ShuttleSchedule with its list of
ShuttleStops is important to the attendees staying at remote hotels.
The diagram has one constraint, that the ShuttleStops are ordered.
There are three interfaces in the diagram: IDated, ILocatable,
and ITimed. The names of interfaces typically begin with the letter
I. A class, such as ShuttleStop, with operations matching those in
an interface, such as ILocatable, is a realization/implementation of the interface.
An interface is a kind of stereotype. Stereotypes, which provide
a way of extending UML, are new kinds of model elements created from
existing kinds. A stereotype element contains the stereotype name in guillemets
(angle-brace pairs).
There are two acceptable notations for interfaces in the UML.
The first is illustrated above. The second uses the lollipop or
pushpin notation.
In lollipop notation, the interfaces are circles with lines
connected to the realizing classes. Since it is more compact but
leaves out some detail, the lollipop notation simplifies the
original diagram.
Top-of-page
| Use-Case
| Class
| Package
| Sequence
| Communication
| State
| Activity
| Deployment