You are on page 1of 47

UML for Java Programmers

Object Mentor, Inc.


Or How Uncle Bob uses UML.

www.objectmentor.com

fitnesse.org

www.xprogramming.com

www.junit.org

Copyright 1998-2001 by Object Mentor, Inc All Rights Reserved

The three modes of UML


Conceptual
Tied to human language.

Specification
A description of source code yet to be written.

Implementation
A description of source code that already exists.

Conceptual
Tied to human language:

Animal

An Animal ISA Dog


Dog

Hmmm

Tied to human language


Animal

Lounge Chair

SitsInA Dog

DrinksA Martini

Led Zepplin

GroovesTo {WayLoud}

SmokesA Cigar

Models of the Real World


Do not relate to software structure.

Now its time for: Uncle Bobs rant. Human language is not the best foundation for software structure.
A Circle ISA Ellipse.

A Circle ISA Ellipse

User

Ellipse

Circle

Specification and Implementation


Are tied to code. Follow the same rules. Differ only in detail.
Sometimes.

Example:
public class TreeMap { TreeMapNode topNode = null; public void add(Comparable key, Object value) {} public Object get(Comparable key) {} } class TreeMapNode { private Comparable itsKey; private Object itsValue; private TreeMapNode nodes[] = new TreeMapNode[2]; public TreeMapNode(Comparable key, Object value) {} public Object find(Comparable key) {} public void add(Comparable key, Object value) {} }

Heres some UML for that.


2 reeMap + add(key, value) + get(key) topNode nodes

TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

10

Classes and Relationships.


Rectangles represent classes, and arrows represent relationships.

2 TreeMap + add(key, value) + get(key) topNode

nodes

TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

11

Associations.
In this diagram all the relationships are associations. Associations are simple data relationships in which one object holds a reference to, and invokes methods upon, the other.

2 TreeMap + add(key, value) + get(key) topNode

nodes

TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

12

Relationship Names.
The name on an association maps to the name of the variable that holds the reference.

public class TreeMap { TreeMapNode topNode = null; }


2 TreeMap + add(key, value) + get(key) topNode nodes TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

13

Multiplicity.
A number next to an arrowhead typically shows the number of instances held by the relationship. If that number is greater than one then some kind of container, like an array, is implied. class TreeMapNode { private TreeMapNode nodes[] = new TreeMapNode[2]; }
2 TreeMap + add(key, value) + get(key) topNode nodes TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

14

Class Icons.
Class icons can have more than one compartment. The top compartment always holds the name of the class. The other compartments describe functions and variables.

2 TreeMap + add(key, value) + get(key) topNode

nodes

TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

15

Interface.
The interface notation means that Comparable is an interface.

2 TreeMap + add(key, value) + get(key) topNode

nodes

TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

16

Optional.
Most of the notations shown are optional

Uncle Bob is going to rant again


2 TreeMap + add(key, value) + get(key) topNode nodes

TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

17

<<Grimace>>.
But Uncle Bob, you didnt talk about Aggregation or Composition.

Rant.
Car Steering Wheel

Engine

18

Object Diagrams.
:TreeMap topNode :TreeMapNode - itsKey = "Martin"

nodes[LESS] :TreeMapNode - itsKey = "Bob"

nodes[GREATER] :TreeMapNode - itsKey = "Robin"

nodes[LESS] :TreeMapNode - itsKey = "Alan"

nodes[GREATER] :TreeMapNode - itsKey = "Don"

nodes[LESS] :TreeMapNode - itsKey = "Paul"

nodes[GREATER] :TreeMapNode - itsKey = "Sam"

19

Freeze Frame.
It shows a set of objects and relationships at a particular moment in the execution of the system. You can view it as a snapshot of memory.

:TreeMap

topNode :TreeMapNode - itsKey = "Martin"

nodes[LESS] :TreeMapNode - itsKey = "Bob"

nodes[GREATER] :TreeMapNode - itsKey = "Robin"

nodes[LESS] :TreeMapNode - itsKey = "Alan"

nodes[GREATER] :TreeMapNode - itsKey = "Don"

nodes[LESS] :TreeMapNode - itsKey = "Paul"

nodes[GREATER] :TreeMapNode - itsKey = "Sam"

20

Object Names are Underlined.


the rectangle icons represent objects. You can tell that they are objects because their names are underlined.

:TreeMap

topNode :TreeMapNode - itsKey = "Martin"

nodes[LESS] :TreeMapNode - itsKey = "Bob"

nodes[GREATER] :TreeMapNode - itsKey = "Robin"

nodes[LESS] :TreeMapNode - itsKey = "Alan"

nodes[GREATER] :TreeMapNode - itsKey = "Don"

nodes[LESS] :TreeMapNode - itsKey = "Paul"

nodes[GREATER] :TreeMapNode - itsKey = "Sam"

21

Object : Class
The name after the colon is the name of the class that the object belongs to.
:TreeMap topNode :TreeMapNode - itsKey = "Martin"

nodes[LESS] :TreeMapNode - itsKey = "Bob"

nodes[GREATER] :TreeMapNode - itsKey = "Robin"

nodes[LESS] :TreeMapNode - itsKey = "Alan"

nodes[GREATER] :TreeMapNode - itsKey = "Don"

nodes[LESS] :TreeMapNode - itsKey = "Paul"

nodes[GREATER] :TreeMapNode - itsKey = "Sam"

22

Values.
Note that the lower compartment of each object shows the value of that objects itsKey variable.
:TreeMap topNode :TreeMapNode - itsKey = "Martin"

nodes[LESS] :TreeMapNode - itsKey = "Bob"

nodes[GREATER] :TreeMapNode - itsKey = "Robin"

nodes[LESS] :TreeMapNode - itsKey = "Alan"

nodes[GREATER] :TreeMapNode - itsKey = "Don"

nodes[LESS] :TreeMapNode - itsKey = "Paul"

nodes[GREATER] :TreeMapNode - itsKey = "Sam"

23

Links.
The relationships between the objects are called links, and are derived from the associations.

:TreeMap
2 TreeMap + add(key, value) + get(key) topNode nodes

topNode :TreeMapNode - itsKey = "Martin"

TreeMapNode + add(key, value) + find(key) itsKey interface Comparable

itsValue Object

24

nodes[LESS] :TreeMapNode - itsKey = "Bob"

nodes[GREATER] :TreeMapNode - itsKey = "Robin"

nodes[LESS] :TreeMapNode - itsKey = "Alan"

nodes[GREATER] :TreeMapNode - itsKey = "Don"

nodes[LESS] :TreeMapNode - itsKey = "Paul"

nodes[GREATER] :TreeMapNode - itsKey = "Sam"

Sequence Diagrams.
public void add(Comparable key,Object value){ if (topNode == null) topNode = new TreeMapNode(key, value); else topNode.add(key, value); }

:TreeMap add(key, value) value key topNode: TreeMapNode

[topNode == null]

add(key, value) [topNode != null]

25

Actor.
The stick figure (actor) represents an unknown caller.

:TreeMap add(key, value) value key topNode: TreeMapNode

[topNode == null]

add(key, value) [topNode != null]

26

Lifelines.
The dashed lines are lifelines, they show the lifetime of the objects they descend from.
:TreeMap add(key, value) value key topNode: TreeMapNode

[topNode == null]

add(key, value) [topNode != null]

27

Messages.
The long arrows are messages sent between the objects.

:TreeMap add(key, value) value key topNode: TreeMapNode

[topNode == null]

add(key, value) [topNode != null]

28

Guards.
The boolean expressions inside square brackets are called guards. They show which path is taken.

:TreeMap add(key, value) value key topNode: TreeMapNode

[topNode == null]

add(key, value) [topNode != null]

29

Construction.
The message arrow that terminates on the TreeMapNode icon represents construction.

:TreeMap add(key, value) value key topNode: TreeMapNode

[topNode == null]

add(key, value) [topNode != null]

30

Data Tokens
The little arrows with circles are called data tokens. They show any arguments or return values.

:TreeMap add(key, value) value key topNode: TreeMapNode

[topNode == null]

add(key, value) [topNode != null]

31

Activations.
The skinny rectangle below TreeMap is called an activation. It depicts how much time the add method executes.

:TreeMap add(key, value) value key topNode: TreeMapNode

[topNode == null]

add(key, value) [topNode != null]

32

Collaboration Diagrams.
1: add(key, value) :TreeMap

[topNode != null] 1.1:add(key, value) topNode :TreeMapNode

33

Same data as Sequence Diagrams.


Collaboration diagrams make the relationships between the objects clear.
1: add(key, value) :TreeMap

[topNode != null] 1.1:add(key, value) topNode :TreeMapNode

34

Links (again).
The objects are connected by relationships called links. A link exists wherever one object can send a message to another.
1: add(key, value) :TreeMap

[topNode != null] 1.1:add(key, value) topNode :TreeMapNode

35

Messages.
Traveling over those links are the messages themselves. They are depicted as the smaller arrows.
1: add(key, value) :TreeMap

[topNode != null] 1.1:add(key, value) topNode :TreeMapNode

36

Message Labels.
The messages are labeled with the name of the message, its sequence number, and any guards that apply.
1: add(key, value) :TreeMap

[topNode != null] 1.1:add(key, value) topNode :TreeMapNode

37

Sequence Numbers.
The dot structure of the sequence number shows the calling hierarchy.
1: add(key, value) :TreeMap

[topNode != null] 1.1:add(key, value) topNode :TreeMapNode

38

State Diagrams.
A Subway Turnstile.

coin / Unlock pass / Alarm Locked

Unlocked pass / Lock

coin / Thankyou

39

States.
There are two states named Locked and Unlocked.

coin / Unlock pass / Alarm Locked

Unlocked pass / Lock

coin / Thankyou

40

Events.
Two events may be sent to the machine. The coin event means that the user has dropped a coin into the turnstile. The pass event means that the user has passed through the turnstile.

coin / Unlock pass / Alarm Locked

Unlocked pass / Lock

coin / Thankyou

41

Transitions.
The arrows are called transitions. They are labeled with the event that triggers the transition and the action that the transition performs.

coin / Unlock pass / Alarm Locked

Unlocked pass / Lock

coin / Thankyou

42

Interpretation.
If we are in the Locked state and we get a coin event, then we transition to the Unlocked state and we invoke the Unlock function. If we are in the Unlocked state and we get a pass event, then we transition to the Locked state and we invoke the Lock function. If we are in the Unlocked state and we get a coin event, then we stay in the Unlocked state and we call the Thankyou function. If we are in the Locked state and we get a pass event, then we stay in the Locked state and we call the Alarm function.
coin / Unlock pass / Alarm Locked

Unlocked pass / Lock

coin / Thankyou

43

Sufficiency.
The diagrams shown in this chapter are enough for most purposes.

Most programmers could live without any more knowledge of UML that what is shown here.

44

Tools.
Now ask me what I think about tools..

45

Documentation.

46

Where to get this presentation.

47

You might also like