UML for Java Programmers - Brigham Young...

47
Object Mentor, Inc. Copyright 1998-2001 by Object Mentor, Inc All Rights Reserved www.objectmentor.com www.junit.org www.xprogramming.com fitnesse.org Or How Uncle Bob uses UML.

Transcript of UML for Java Programmers - Brigham Young...

����������

� ��� � � ��

Object 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.

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.

3

Conceptual

Tied to human language:

Animal

Dog

“An Animal ISA Dog”

4

Hmmm…

5

Tied to human language

Animal

Dog Martini

Cigar

LoungeChair

LedZepplin

«SitsInA» «DrinksA»

«GroovesTo» «SmokesA»

{WayLoud}

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.

7

A Circle ISA Ellipse

Ellipse

Circle

User

8

Specification and Implementation

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

Sometimes.

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

}

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

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.

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

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;

}

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];

}

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

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

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…

18

<<Grimace>>.

Car SteeringWheel

Engine

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

Rant….

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

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

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

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

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

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

25

Sequence Diagrams.public void add(Comparable key,Object value){

if (topNode == null)topNode = new TreeMapNode(key, value);

elsetopNode.add(key, value);

}

26

Actor.

The stick figure (actor) represents an unknown caller.

27

Lifelines.

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

descend from.

28

Messages.

The long arrows are messages sent between the objects.

29

Guards.The boolean expressions inside square brackets are

called guards. They show which path is taken.

30

Construction.The message arrow that terminates on the

TreeMapNode icon represents construction.

31

Data TokensThe little arrows with circles are called data tokens.

They show any arguments or return values.

32

Activations.The skinny rectangle below TreeMap is called

an activation. It depicts how much time the addmethod executes.

33

Collaboration Diagrams.

:TreeMap

topNode:TreeMapNode

1: add(key, value)

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

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)

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)

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)

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)

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)

39

State Diagrams.

Locked

Unlocked

coin / Unlock

pass / Lockcoin / Thankyou

pass / Alarm

A Subway Turnstile.

40

States.There are two states named Locked and Unlocked.

Locked

Unlocked

coin / Unlock

pass / Lockcoin / Thankyou

pass / Alarm

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

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

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

44

Sufficiency.The diagrams shown in this chapter are enough

for most purposes.

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

45

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

46

Documentation.

47

Where to get this presentation.