This is a printer-friendly version. It omits exercises, optional topics (i.e., four-star topics), and other extra content such as learning outcomes.
UML class diagrams describe the structure (but not the behavior) of an OOP solution. These are possibly the most often used diagrams in the industry and an indispensable tool for an OO programmer.
An example class diagram:
The basic UML notations used to represent a class:
A Table
class shown in UML notation:
Java
|
|
Python
|
The 'Operations' compartment and/or the 'Attributes' compartment may be omitted if such details are not important for the task at hand. 'Attributes' always appear above the 'Operations' compartment. All operations should be in one compartment rather than each operation in a separate compartment. Same goes for attributes.
The visibility of attributes and operations is used to indicate the level of access allowed for each attribute or operation. The types of visibility and their exact meanings depend on the programming language used. Here are some common visibilities and how they are indicated in a class diagram:
+
: public-
: private#
: protected~
: package privatevisibility | Java | Python |
---|---|---|
- private |
private |
at least two leading underscores (and at most one trailing underscores) in the name |
# protected |
protected |
one leading underscore in the name |
+ public |
public |
all other cases |
~ package private |
default visibility | not applicable |
Table
class with visibilities shown:
We use a solid line to show an association between two classes.
This example shows an association between the Admin
class and the Student
class:
We use arrow heads to indication the navigability of an association.
Logic
is aware of Minefield
, but Minefield
is not aware of Logic
class Logic{
Minefield minefield;
}
class Minefield{
...
}
class Logic:
def __init__(self, minefield):
self.minefield = minefield
# ...
class Minefield:
# ...
Here is an example of a bidirectional navigability; each class is aware of the other.
Navigability can be shown in class diagrams as well as object diagrams.
According to this object diagram the given Logic
object is associated with and aware of two MineField
objects.
Association Role labels are used to indicate the role played by the classes in the association.
This association represents a marriage between a Man
object and a Woman
object. The respective roles played by objects of these two classes are husband
and wife
.
Note how the variable names match closely with the association roles.
Java
|
|
Python
|
The role of Student
objects in this association is charges
(i.e. Admin is in charge of students)
Java
|
|
Python
|
Association labels describe the meaning of the association. The arrow head indicates the direction in which the label is to be read.
In this example, the same association is described using two different labels.
Admin
class is associated with Student
class because an Admin
object uses a Student
object.Admin
class is associated with Student
class because a Student
object is used by an Admin
object.Commonly used multiplicities:
0..1
: optional, can be linked to 0 or 1 objects1
: compulsory, must be linked to one object at all times.*
: can be linked to 0 or more objects.n..m
: the number of linked objects must be n
to m
inclusive In the diagram below, an Admin
object administers (in charge of) any number of students but a Student
object must always be under the charge of exactly one Admin
object
In the diagram below,
UML uses a dashed arrow to show dependencies.
Two examples of dependencies:
Notation:
In the class diagram below, there are two enumerations in use:
In UML class diagrams, underlines denote class-level attributes and variables.
In the class below, totalStudents
attribute and the getTotalStudents
method are class-level.
Association classes are denoted as a connection to an association link using a dashed line as shown below.
In this example Loan
is an association class because it stores information about the borrows
association between the User
and the Book
.
UML uses a solid diamond symbol to denote composition.
Notation:
A Book
consists of Chapter
objects. When the Book
object is destroyed, its Chapter
objects are destroyed too.
You can use a triangle and a solid line (not to be confused with an arrow) to indicate class inheritance.
Notation:
Examples: The Car
class inherits from the Vehicle
class. The Cat
and Dog
classes inherit from the Pet
class.
An interface is shown similar to a class with an additional keyword << interface >>
. When a class implements an interface, it is shown similar to class inheritance except a dashed line is used instead of a solid line.
The AcademicStaff
and the AdminStaff
classes implement the SalariedStaff
interface.
You can use italics or {abstract}
(preferred) keyword to denote abstract classes/methods.
Example:
A UML sequence diagram captures the interactions between multiple objects for a given scenario.
Consider the code below.
class Machine {
Unit producePrototype() {
Unit prototype = new Unit();
for (int i = 0; i < 5; i++) {
prototype.stressTest();
}
return prototype;
}
}
class Unit {
public void stressTest() {
}
}
Here is the sequence diagram to model the interactions for the method call producePrototype()
on a Machine
object.
Notation:
This sequence diagram shows some interactions between a human user and the Text UI of a
The player runs the newgame
action on the TextUi
object which results in the TextUi
showing the minefield to the player. Then, the player runs the clear x y
command; in response, the TextUi
object shows the updated minefield.
The :TextUi
in the above example denotes an unnamed instance of the class TextUi. If there were two instances of TextUi
in the diagram, they can be distinguished by naming them e.g. TextUi1:TextUi
and TextUi2:TextUi
.
Arrows representing method calls should be solid arrows while those representing method returns should be dashed arrows.
Note that unlike in object diagrams, the class/object name is not underlined in sequence diagrams.
[Common notation error] Activation bar too long: The activation bar of a method cannot start before the method call arrives and a method cannot remain active
after the method had returned. In the two sequence diagrams below, the one on the left commits this error because the activation bar starts before the method Foo#xyz()
is called and remains active after the method returns.
[Common notation error] Broken activation bar: The activation bar should remain unbroken from the point the method is called until the method returns.
In the two sequence diagrams below, the one on the left commits this error because the activation bar for the method Foo#abc()
is not contiguous, but appears as two pieces instead.
Notation:
The Logic
object creates a Minefield
object.
UML uses an X
at the end of the lifeline of an object to show it's deletion.
💡 Although object deletion is not that important in languages such as Java that support automatic memory management, you can still show object deletion in UML diagrams to indicate the point at which the object ceases to be used.
Notation:
Note how the diagrams shows the deletion of the Minefield
object
Notation:
The Player
calls the mark x,y
command or clear x y
command repeatedly until the game is won or lost.
UML can show a method of an object calling another of its own methods.
Notation:
The markCellAt(...)
method of a Logic
object is calling its own updateState(...)
method.
In this variation, the Book#write()
method is calling the Chapter#getText()
method which in turn does a call back by calling the getAuthor()
method of the calling
object.
UML uses alt
frames to indicate alternative paths.
Notation:
Minefield
calls the Cell#setMine
if the cell is supposed to be a mined cell, and calls the Cell:setMineCount(...)
method otherwise.
UML uses opt
frames to indicate optional paths.
Notation:
Logic#markCellAt(...)
calls Timer#start()
only if it is the first move of the player.
To reduce clutter, activation bars and return arrows may be omitted if they do not result in ambiguities or loss of information. Informal operation descriptions such as those given in the example below can be used, if more precise details are not required for the task at hand.
A minimal sequence diagram
An object diagram shows an object structure at a given point of time.
An example object diagram:
Notation:
Notes:
car1:Car
are underlined.objectName:ClassName
is meant to say 'an instance of ClassName
identified as objectName
'.:Car
which is meant to say 'an unnamed instance of a Car object'.Some example objects:
A solid line indicates an association between two objects.
An example object diagram showing two associations:
A UML Activity diagram (AD) can model workflows.
An example activity diagram:
[source:wikipeida]
An activity diagram (AD) captures an activity of actions and control flows that makes up the activity.
Note the slight difference between the start node and the end node which represent the start and the end of the activity, respectively.
This activity diagram shows the action sequence of the activity a passenger rides the bus:
A branch node shows the start of alternate paths. Each control flow exiting a branch node has a guard condition : a boolean condition that should be true for execution to take that path. Only one of the guard condition can be true at any time.
A merge node shows the end of alternate paths.
Both branch nodes and merge nodes are diamond shapes. Guard conditions must be in square brackets.
The AD below shows alternate paths involved in the workflow of the activity shop for product:
Fork nodes indicate the start of
Join nodes indicate the end of parallel paths.
Both have the same notation: a bar.
In a
In this activity diagram (from an online shop website) the actions User browsers products and System records browsing data happen in parallel. Both of them need to finish before the log out action can take place.
Compared to the notation for a class diagrams, object diagrams differ in the following ways:
:
before the class nameFurthermore, multiple object diagrams can correspond to a single class diagram.
Both object diagrams are derived from the same class diagram shown earlier. In other words, each of these object diagrams shows ‘an instance of’ the same class diagram.