Uml for Java Programmers

47
UML for Java Programmers Object Mentor, Inc. Copyright 1998-2001 by Object Mentor, Inc All Rights Reserved www.objectmentor.com www.junit.o rg www. xprogramming .com fitnesse.or g Or How Uncle Bob uses UML.

Transcript of Uml for Java Programmers

Page 1: Uml for Java Programmers

UML for Java ProgrammersObject Mentor, Inc.

Copyright 1998-2001 by Object Mentor, IncAll Rights Reserved

www.objectmentor.com

www.junit.orgwww.xprogramming.comfitnesse.org

Or

How Uncle Bob uses UML.

Page 2: Uml for Java Programmers

2

The three modes of UML

ConceptualTied to human language.

SpecificationA description of source code yet to be written.

ImplementationA description of source code that already exists.

Page 3: Uml for Java Programmers

3

Conceptual

Tied to human language:

Animal

Dog

“An Animal ISA Dog”

Page 4: Uml for Java Programmers

4

Hmmm…

Page 5: Uml for Java Programmers

5

Tied to human language

Animal

Dog Martini

Cigar

LoungeChair

LedZepplin

«SitsInA» «DrinksA»

«GroovesTo» «SmokesA»

{W ayLoud}

Page 6: Uml for Java Programmers

6

Models of the “Real World”

Do not relate to software structure.

Now it’s time for: Uncle Bob’s rant….

Human language is not the best foundation for software structure.

A Circle ISA Ellipse.

Page 7: Uml for Java Programmers

7

A Circle ISA Ellipse

Ellipse

Circle

User

Page 8: Uml for Java Programmers

8

Specification and Implementation

Are tied to code.

Follow the same rules.

Differ only in detail.

Sometimes.

Page 9: Uml for Java Programmers

9

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) {…}}

Page 10: Uml for Java Programmers

10

Here’s some UML for that.

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

Page 11: Uml for Java Programmers

11

Classes and Relationships.

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

Rectangles represent classes, and arrows represent relationships.

Page 12: Uml for Java Programmers

12

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.

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

Page 13: Uml for Java Programmers

13

Relationship Names.The name on an association maps to the name of the

variable that holds the reference.

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

public class TreeMap { TreeMapNode topNode = null;}

Page 14: Uml for Java Programmers

14

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.

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

class TreeMapNode { private TreeMapNode nodes[] = new TreeMapNode[2];}

Page 15: Uml for Java Programmers

15

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.

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

Page 16: Uml for Java Programmers

16

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

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

Page 17: Uml for Java Programmers

17

Optional.

Most of the notations shown are optional

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

Uncle Bob is going to rant again…

Page 18: Uml for Java Programmers

18

<<Grimace>>.

CarSteeringWheel

Engine

But Uncle Bob, you didn’t talk about Aggregation or Composition.

Rant….

Page 19: Uml for Java Programmers

19

Object Diagrams.

:TreeMap

- itsKey = "Martin"

:TreeMapNode

- itsKey = "Bob"

:TreeMapNode

- itsKey = "Don"

:TreeMapNode

- itsKey = "Alan"

:TreeMapNode

- itsKey = "Paul"

:TreeMapNode

- itsKey = "Sam"

:TreeMapNode

- itsKey = "Robin"

:TreeMapNode

nodes[GREATER]nodes[LESS]

nodes[LESS] nodes[LESS]nodes[GREATER] nodes[GREATER]

topNode

Page 20: Uml for Java Programmers

20

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

- itsKey = "Martin"

:TreeMapNode

- itsKey = "Bob"

:TreeMapNode

- itsKey = "Don"

:TreeMapNode

- itsKey = "Alan"

:TreeMapNode

- itsKey = "Paul"

:TreeMapNode

- itsKey = "Sam"

:TreeMapNode

- itsKey = "Robin"

:TreeMapNode

nodes[GREATER]nodes[LESS]

nodes[LESS] nodes[LESS]nodes[GREATER] nodes[GREATER]

topNode

Page 21: Uml for Java Programmers

21

Object Names are Underlined.the rectangle icons represent objects. You can tell that they are

objects because their names are underlined.

:TreeMap

- itsKey = "Martin"

:TreeMapNode

- itsKey = "Bob"

:TreeMapNode

- itsKey = "Don"

:TreeMapNode

- itsKey = "Alan"

:TreeMapNode

- itsKey = "Paul"

:TreeMapNode

- itsKey = "Sam"

:TreeMapNode

- itsKey = "Robin"

:TreeMapNode

nodes[GREATER]nodes[LESS]

nodes[LESS] nodes[LESS]nodes[GREATER] nodes[GREATER]

topNode

Page 22: Uml for Java Programmers

22

Object : ClassThe name after the colon is the name of the

class that the object belongs to.

:TreeMap

- itsKey = "Martin"

:TreeMapNode

- itsKey = "Bob"

:TreeMapNode

- itsKey = "Don"

:TreeMapNode

- itsKey = "Alan"

:TreeMapNode

- itsKey = "Paul"

:TreeMapNode

- itsKey = "Sam"

:TreeMapNode

- itsKey = "Robin"

:TreeMapNode

nodes[GREATER]nodes[LESS]

nodes[LESS] nodes[LESS]nodes[GREATER] nodes[GREATER]

topNode

Page 23: Uml for Java Programmers

23

Values.

Note that the lower compartment of each object shows the value of that object’s itsKey variable.

:TreeMap

- itsKey = "Martin"

:TreeMapNode

- itsKey = "Bob"

:TreeMapNode

- itsKey = "Don"

:TreeMapNode

- itsKey = "Alan"

:TreeMapNode

- itsKey = "Paul"

:TreeMapNode

- itsKey = "Sam"

:TreeMapNode

- itsKey = "Robin"

:TreeMapNode

nodes[GREATER]nodes[LESS]

nodes[LESS] nodes[LESS]nodes[GREATER] nodes[GREATER]

topNode

Page 24: Uml for Java Programmers

24

Links.The relationships between the objects are called

links, and are derived from the associations.

:TreeMap

- itsKey = "Martin"

:TreeMapNode

- itsKey = "Bob"

:TreeMapNode

- itsKey = "Don"

:TreeMapNode

- itsKey = "Alan"

:TreeMapNode

- itsKey = "Paul"

:TreeMapNode

- itsKey = "Sam"

:TreeMapNode

- itsKey = "Robin"

:TreeMapNode

nodes[GREATER]nodes[LESS]

nodes[LESS] nodes[LESS]nodes[GREATER] nodes[GREATER]

topNode

+ add(key, value)+ get(key)

TreeMap

+ add(key, value)+ find(key)

TreeMapNodetopNode

2 nodes

Comparable«interface»

Object

itsKey

itsValue

Page 25: Uml for Java Programmers

25

Sequence Diagrams.

:TreeMap

add(key, value)

topNode:TreeMapNode

keyvalue

[topNode == null]

add(key, value)

[topNode != null]

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

Page 26: Uml for Java Programmers

26

Actor.

The stick figure (actor) represents an unknown caller.

:TreeMap

add(key, value)

topNode:TreeMapNode

keyvalue

[topNode == null]

add(key, value)

[topNode != null]

Page 27: Uml for Java Programmers

27

Lifelines.

:TreeMap

add(key, value)

topNode:TreeMapNode

keyvalue

[topNode == null]

add(key, value)

[topNode != null]

The dashed lines are lifelines, they show the lifetime of the objects they

descend from.

Page 28: Uml for Java Programmers

28

Messages.

:TreeMap

add(key, value)

topNode:TreeMapNode

keyvalue

[topNode == null]

add(key, value)

[topNode != null]

The long arrows are messages sent between the objects.

Page 29: Uml for Java Programmers

29

Guards.

:TreeMap

add(key, value)

topNode:TreeMapNode

keyvalue

[topNode == null]

add(key, value)

[topNode != null]

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

Page 30: Uml for Java Programmers

30

Construction.The message arrow that terminates on the

TreeMapNode icon represents construction.

:TreeMap

add(key, value)

topNode:TreeMapNode

keyvalue

[topNode == null]

add(key, value)

[topNode != null]

Page 31: Uml for Java Programmers

31

Data TokensThe little arrows with circles are called data tokens.

They show any arguments or return values.

:TreeMap

add(key, value)

topNode:TreeMapNode

keyvalue

[topNode == null]

add(key, value)

[topNode != null]

Page 32: Uml for Java Programmers

32

Activations.The skinny rectangle below TreeMap is called

an activation. It depicts how much time the add method executes.

:TreeMap

add(key, value)

topNode:TreeMapNode

keyvalue

[topNode == null]

add(key, value)

[topNode != null]

Page 33: Uml for Java Programmers

33

Collaboration Diagrams.

:TreeMap

topNode:TreeMapNode

1: add(key, value)

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

Page 34: Uml for Java Programmers

34

Same data as Sequence Diagrams.Collaboration diagrams make the

relationships between the objects clear.

:TreeMap

topNode:TreeMapNode

1: add(key, value)

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

Page 35: Uml for Java Programmers

35

Links (again).The objects are connected by relationships called

links. A link exists wherever one object can send a message to another.

:TreeMap

topNode:TreeMapNode

1: add(key, value)

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

Page 36: Uml for Java Programmers

36

Messages.Traveling over those links are the messages

themselves. They are depicted as the smaller arrows.

:TreeMap

topNode:TreeMapNode

1: add(key, value)

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

Page 37: Uml for Java Programmers

37

Message Labels.The messages are labeled with the name of the message, its sequence number, and any

guards that apply.

:TreeMap

topNode:TreeMapNode

1: add(key, value)

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

Page 38: Uml for Java Programmers

38

Sequence Numbers.The dot structure of the sequence number

shows the calling hierarchy.

:TreeMap

topNode:TreeMapNode

1: add(key, value)

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

Page 39: Uml for Java Programmers

39

State Diagrams.

Locked

Unlocked

coin / Unlock

pass / Lockcoin / Thankyou

pass / Alarm

A Subway Turnstile.

Page 40: Uml for Java Programmers

40

States.There are two states named Locked and Unlocked.

Locked

Unlocked

coin / Unlock

pass / Lockcoin / Thankyou

pass / Alarm

Page 41: Uml for Java Programmers

41

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.

Locked

Unlocked

coin / Unlock

pass / Lockcoin / Thankyou

pass / Alarm

Page 42: Uml for Java Programmers

42

Transitions.The arrows are called transitions. They are labeled with

the event that triggers the transition and the action that the transition performs.

Locked

Unlocked

coin / Unlock

pass / Lockcoin / Thankyou

pass / Alarm

Page 43: Uml for Java Programmers

43

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.

Locked

Unlocked

coin / Unlock

pass / Lockcoin / Thankyou

pass / Alarm

Page 44: Uml for Java Programmers

44

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.

Page 45: Uml for Java Programmers

45

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

Page 46: Uml for Java Programmers

46

Documentation.

Page 47: Uml for Java Programmers

47

Where to get this presentation.