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.

2

Conceptual
Tied to human language:

Animal

³An Animal ISA Dog´
Dog

3

Hmmm«

4

Tied to human language Animal Lounge Chair SitsInA» Dog DrinksA» Martini Led Zepplin GroovesTo» {WayLoud} SmokesA» Cigar 5 .

Now it¶s time for: Uncle Bob¶s rant«. A Circle ISA Ellipse.Models of the ³Real World´ Do not relate to software structure. 6 . Human language is not the best foundation for software structure.

A Circle ISA Ellipse User Ellipse Circle 7 .

Follow the same rules. 8 . Differ only in detail.Specification and Implementation Are tied to code. Sometimes.

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

value) + get(key) topNode nodes TreeMapNode + add(key. 2 reeMap + add(key.Here¶s some UML for that. value) + find(key) itsKey interface» Comparable itsValue Object 10 .

Classes and Relationships. value) + get(key) topNode nodes TreeMapNode + add(key. value) + find(key) itsKey interface» Comparable itsValue Object 11 . 2 TreeMap + add(key. Rectangles represent classes. and arrows represent relationships.

2 TreeMap + add(key. the other. value) + find(key) itsKey interface» Comparable itsValue Object 12 . value) + get(key) topNode nodes TreeMapNode + add(key. In this diagram all the relationships are associations.Associations. and invokes methods upon. Associations are simple data relationships in which one object holds a reference to.

value) + find(key) itsKey interface» Comparable itsValue Object 13 .Relationship Names. value) + get(key) topNode nodes TreeMapNode + add(key. } 2 TreeMap + add(key. public class TreeMap { TreeMapNode topNode = null. The name on an association maps to the name of the variable that holds the reference.

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

Class icons can have more than one compartment. value) + find(key) itsKey interface» Comparable itsValue Object 15 . value) + get(key) topNode nodes TreeMapNode + add(key.Class Icons. 2 TreeMap + add(key. The other compartments describe functions and variables. The top compartment always holds the name of the class.

Interface. The «interface» notation means that Comparable is an interface. value) + get(key) topNode nodes TreeMapNode + add(key. value) + find(key) itsKey interface» Comparable itsValue Object 16 . 2 TreeMap + add(key.

Optional. value) + find(key) itsKey interface» Comparable itsValue Object 17 . value) + get(key) topNode nodes TreeMapNode + add(key. Most of the notations shown are optional Uncle Bob is going to rant again« 2 TreeMap + add(key.

<<Grimace>>. you didn¶t talk about Aggregation or Composition. Car Steering Wheel Engine 18 . But Uncle Bob. Rant«.

:TreeMap topNode :TreeMapNode .Object Diagrams.itsKey = "Sam" 19 .itsKey = "Paul" nodes[GREATER] :TreeMapNode .itsKey = "Bob" nodes[GREATER] :TreeMapNode .itsKey = "Don" nodes[LESS] :TreeMapNode .itsKey = "Robin" nodes[LESS] :TreeMapNode .itsKey = "Alan" nodes[GREATER] :TreeMapNode .itsKey = "Martin" nodes[LESS] :TreeMapNode .

itsKey = "Don" nodes[LESS] :TreeMapNode .itsKey = "Alan" nodes[GREATER] :TreeMapNode . 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.itsKey = "Bob" nodes[GREATER] :TreeMapNode . :TreeMap topNode :TreeMapNode .itsKey = "Paul" nodes[GREATER] :TreeMapNode .itsKey = "Robin" nodes[LESS] :TreeMapNode .itsKey = "Martin" nodes[LESS] :TreeMapNode .Freeze Frame.itsKey = "Sam" 20 .

Object Names are Underlined.itsKey = "Martin" nodes[LESS] :TreeMapNode .itsKey = "Bob" nodes[GREATER] :TreeMapNode .itsKey = "Sam" 21 .itsKey = "Robin" nodes[LESS] :TreeMapNode . the rectangle icons represent objects.itsKey = "Paul" nodes[GREATER] :TreeMapNode .itsKey = "Don" nodes[LESS] :TreeMapNode . :TreeMap topNode :TreeMapNode . You can tell that they are objects because their names are underlined.itsKey = "Alan" nodes[GREATER] :TreeMapNode .

itsKey = "Alan" nodes[GREATER] :TreeMapNode .itsKey = "Sam" 22 .itsKey = "Bob" nodes[GREATER] :TreeMapNode .itsKey = "Don" nodes[LESS] :TreeMapNode .itsKey = "Paul" nodes[GREATER] :TreeMapNode . :TreeMap topNode :TreeMapNode .Object : Class The name after the colon is the name of the class that the object belongs to.itsKey = "Robin" nodes[LESS] :TreeMapNode .itsKey = "Martin" nodes[LESS] :TreeMapNode .

itsKey = "Don" nodes[LESS] :TreeMapNode .Values.itsKey = "Sam" 23 .itsKey = "Martin" nodes[LESS] :TreeMapNode .itsKey = "Paul" nodes[GREATER] :TreeMapNode .itsKey = "Robin" nodes[LESS] :TreeMapNode . Note that the lower compartment of each object shows the value of that object¶s itsKey variable.itsKey = "Bob" nodes[GREATER] :TreeMapNode .itsKey = "Alan" nodes[GREATER] :TreeMapNode . :TreeMap topNode :TreeMapNode .

Links.itsKey = "Don" nodes[LESS] :TreeMapNode . value) + find(key) itsKey interface» Comparable itsValue Object 24   nodes[LESS] :TreeMapNode . :TreeMap 2 TreeMap + add(key.itsKey = "Paul" nodes[GREATER] :TreeMapNode .itsKey = "Bob" nodes[GREATER] :TreeMapNode .itsKey = "Robin" nodes[LESS] :TreeMapNode .itsKey = "Sam" .itsKey = "Martin" TreeMapNode + add(key.itsKey = "Alan" nodes[GREATER] :TreeMapNode . value) + get(key) topNode nodes topNode :TreeMapNode . and are derived from the associations. The relationships between the objects are called links.

Object value){ if (topNode == null) topNode = new TreeMapNode(key. value) value key topNode: TreeMapNode [topNode == null] add(key. } :TreeMap add(key.add(key. value). else topNode. public void add(Comparable key.Sequence Diagrams. value) [topNode != null] 25 . value).

value) value key topNode: TreeMapNode [topNode == null] add(key. The stick figure (actor) represents an unknown caller. value) [topNode != null] 26 .Actor. :TreeMap add(key.

Lifelines. they show the lifetime of the objects they descend from. The dashed lines are lifelines. value) value key topNode: TreeMapNode [topNode == null] add(key. value) [topNode != null] 27 . :TreeMap add(key.

value) [topNode != null] 28 . The long arrows are messages sent between the objects. :TreeMap add(key.Messages. value) value key topNode: TreeMapNode [topNode == null] add(key.

Guards. They show which path is taken. value) [topNode != null] 29 . :TreeMap add(key. value) value key topNode: TreeMapNode [topNode == null] add(key. The boolean expressions inside square brackets are called guards.

value) value key topNode: TreeMapNode [topNode == null] add(key. :TreeMap add(key.Construction. The message arrow that terminates on the TreeMapNode icon represents construction. value) [topNode != null] 30 .

They show any arguments or return values. value) [topNode != null] 31 .Data Tokens The little arrows with circles are called data tokens. :TreeMap add(key. value) value key topNode: TreeMapNode [topNode == null] add(key.

value) value key topNode: TreeMapNode [topNode == null] add(key. The skinny rectangle below TreeMap is called an activation. It depicts how much time the add method executes. :TreeMap add(key. value) [topNode != null] 32 .Activations.

value) topNode :TreeMapNode 33 . value) :TreeMap [topNode != null] 1.Collaboration Diagrams.1:add(key. 1: add(key.

1:add(key. value) topNode :TreeMapNode 34 . value) :TreeMap [topNode != null] 1. Collaboration diagrams make the relationships between the objects clear.Same data as Sequence Diagrams. 1: add(key.

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.Links (again).1:add(key. value) topNode :TreeMapNode 35 .

They are depicted as the smaller arrows.Messages. value) topNode :TreeMapNode 36 . 1: add(key. value) :TreeMap [topNode != null] 1. Traveling over those links are the messages themselves.1:add(key.

Message Labels. value) :TreeMap [topNode != null] 1. its sequence number. and any guards that apply. The messages are labeled with the name of the message.1:add(key. 1: add(key. value) topNode :TreeMapNode 37 .

1:add(key. value) :TreeMap [topNode != null] 1. value) topNode :TreeMapNode 38 . 1: add(key.Sequence Numbers. The dot structure of the sequence number shows the calling hierarchy.

State Diagrams. coin / Unlock pass / Alarm Locked Unlocked pass / Lock coin / Thankyou 39 . A Subway Turnstile.

coin / Unlock pass / Alarm Locked Unlocked pass / Lock coin / Thankyou 40 .States. There are two states named Locked and Unlocked.

Two events may be sent to the machine.Events. The pass event means that the user has passed through the turnstile. coin / Unlock pass / Alarm Locked Unlocked pass / Lock coin / Thankyou 41 . The coin event means that the user has dropped a coin into the turnstile.

coin / Unlock pass / Alarm Locked Unlocked pass / Lock coin / Thankyou 42 . The arrows are called transitions.Transitions. They are labeled with the event that triggers the transition and the action that the transition performs.

then we transition to the Unlocked state and we invoke the Unlock function. then we stay in the Locked state and we call the Alarm function.Interpretation. then we stay in the Unlocked state and we call the Thankyou 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 Locked state and we get a coin event. coin / Unlock pass / Alarm Locked Unlocked pass / Lock coin / Thankyou 43 . If we are in the Unlocked state and we get a coin event. If we are in the Locked state and we get a pass event.

The diagrams shown in this chapter are enough for most purposes. 44 . Most programmers could live without any more knowledge of UML that what is shown here.Sufficiency.

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

46 .Documentation.

Where to get this presentation. 47 .