Vous êtes sur la page 1sur 22

Technical University of Cluj-Napoca

Computer Science Department

Object Oriented
Programming Techniques

UML Diagrams

Professor Ioan Salomie


Spring 2010

Prof. Ioan Salomie Programming Techniques

Associations
Binary relationships among
Label

classes

(optional)

describes the associations


directions arrow
Association ends (see

next figure)

role name
multiplicity
Design

decisions related to
navigation among class relations
unidirectional
bidirectional

Ioan Salomie - OO Programming Techniques

Association
Name, Role, Multiplicity

Student
*

Research Proj

advisee

*
proposes
1
1
Professor
advisor

Ioan Salomie - OO Programming Techniques

Bi-directional associations
ResearchProj
*

1
Professor

public class ReseachProj {


private Professor theProfessor;
public ResearchProj() { .. .};
}

public class Professor {


private ResearchProj[] rp = new
ResearchProj[MAX_RP];
public Professor() { };
}

Java class skeleton

Ioan Salomie - OO Programming Techniques

Uni-directional
associations
ResearchProj
*

1
Professor

public class ResearchProj {


public ResearchProj() { };
}

public class Professor {


private ReserachProj[] rp = new
ResearchProj[MAX_RP];
public Professor() { };
}

Java class skeleton

Ioan Salomie - OO Programming Techniques

Reflexive associations

prerequisites
0..*
Course

class Course {
public:
public Course() { }
// the unbounded multiple association
// is stored in a Vector
private ArrayList prerequisites;

Ioan Salomie - OO Programming Techniques

Association classes
Association

as class

Example

class Contract describe the association


(Company, Person)
UML

representation

Person

Company

Contract

Ioan Salomie - OO Programming Techniques

Aggregation
Special form

of association

Relationship

"has a" or "part of"


Weak

aggregation
Strong aggregation
UML aggregation indicator
Properties
transitive
not reflexive

Ioan Salomie - OO Programming Techniques

Representation
UML

bicycle

bicycle
part-of
wheel

1
frame

2
wheel

frame

part-of
36..72
spoke

Horiz bar

spoke

Horiz bar

Ioan Salomie - OO Programming Techniques

Aggregation Examples
Strong and weak aggregation
1

Bank

1..*

Branch

Customer

Clerck

Director
1

1..*
Staff

Car

Body

Wheel

1
Engine

Ioan Salomie - OO Programming Techniques

10

UML Diagrams
Model

A complete system description


UML

diagrams for visualizing models

Use case diagram


capturing requirements and illustrating user
interactions with the system

Class and object diagrams - illustrate logical


structure of the system
Sequence, Collaboration, State diagrams illustrate system behaviour
Activity diagrams - illustrate the flow of
events in a use case
Package diagrams - shows packages of
classes and their dependencies
Component diagrams - illustrate physical
structure of software
Deployment diagrams - shows the mapping
of software to hardware configurations
Prof. Ioan Salomie Programming Techniques

11

Class and object diagrams


Describes the

classes and objects


in the system and the
relationships among them
static relationships
associations
aggregation
generalization (inheritance)

For a

class can show

attributes
operations
constraints

Prof. Ioan Salomie Programming Techniques

12

Class diagrams
Conceptual level

Catalogue

Component

Assembly

*
CatalogueEntry

Part

Prof. Ioan Salomie Programming Techniques

13

Class diagrams
Delivery department Detailed level
Adapted from: M.FowlerUML Distilled, Addison Wesley 2000

Customer
{ abstract }

#customerID : String
#address: String
getHistory(): String

Order

- dateReceived : Date
- orderNumber : String
- value : int
- isPrepaid : bool
dispatch()
close()

CorporateCustomer

PersonalCustomer

- creditCardNo : String

- name : String
- creditLimit : int

OrderLine
getHistory(): String

getHistory(): String

- quantity : int
- unitPrice : int

1
1
Product
Prof. Ioan Salomie Programming Techniques

14

Class diagrams
Three perspectives

Conceptual
draw a diagram representing the concepts in the
problem domain
not related to implementation
focused on
functionality
relationships among concepts

Specification
software perspective
behavioural approach (interfaces) not implementation

Implementation

Notes

proper classes with attributes and operations


the lines between the three approaches is not very
clear
not big differences between conceptual and
specification
there are notable differences between specification
diagrams and implementation ones

Prof. Ioan Salomie Programming Techniques

15

Use-case concepts and diagrams


Use-cases

introduced in SE by Ivar Jacobson (1992)


central concept in Rational Unified Process
(RUP) process and UML
a use case represents a set of scenarios related
to how the system is used
Use

cases help to discover

System entities,
System actors
Attributes and
Behavior

UML

introduces use case diagrams to


graphically capture system actors, usecases and their relationships

Prof. Ioan Salomie Programming Techniques

16

Use-case concepts and diagrams


Use-case

actor

Actor - a role a human person, machine or


software play during system operation
The actor interacts (communicates) with the
system
Actor roles
Use-case

actor identification

Main actors and how they interact with the


system to achieve system functionality
Further iterations - identify and classify other
actors
usually they support the primary actors

Use-cases

and actors

A use case describes the way actors interact


with the system
Use cases are built after the identification of the
primary actors
Prof. Ioan Salomie Programming Techniques

17

UML use-case diagram


Example

Use case check balance


Use-case description

Client inserts the card into ATM


System prompts for PIN
System validates the PIN
System ask for an operation. Client selects check balance
System communicates with the ATM network
System prints balance

System

actor

Use-case

ATM

withdraw money

client
check balance
Prof. Ioan Salomie Programming Techniques

18

UML use-case Diagram


Example

System
Actor

DVM

Use - case

Buy drink

Update stock

Customer

Agent

Collect

Maintenance
Engineer

Prof. Ioan Salomie Programming Techniques

19

Use-case description
Use

case - should address the


following questions [jacobsen]
what are the main tasks performed by
the actor
what info the actor will bring, acquire
or change
what information does the actor desire
about the system

Use-case

document

document about the sequence of actor system interaction (can be written in


natural language)

Prof. Ioan Salomie Programming Techniques

20

Use-case description
Use

case example [Fowler]


Actor: customer
System: on line e-Commerce system
Description:
The customer browses the catalog and adds
desired items to the shopping basket. When
the customer wishes to pay the customer
describes the shipping and credit card
information and confirms the sale. The
system checks the authorization on the credit
card and confirms the sale both immediately
and with a follow-up e-mail.
This

scenario is one way of how things


can happen
The credit card authorization might fail
that is another scenario

Prof. Ioan Salomie Programming Techniques

21

Interaction diagrams
Describe how

groups of objects

collaborate
Typically captures the behaviour
of a single use-case
shows the objects involved and the
messages passed among the objects
Shows the

collaboration among
the objects of a use-case

Prof. Ioan Salomie Programming Techniques

22

Sequence diagrams
Concepts

Shows the objects involved in the interaction


Lifeline
dashed vertical line below the objects
shows the period of time the objects play a certain role
in a use case

Message
arrows pointing from a lifeline of a sending object to
the lifeline of a receiving object
messages have names and may have arguments
each message moves the flow of control from one
object to another

Scenario
a use case may contain more scenarios
one of the scenario is the best case scenario
other scenarios can describe different events and
conditions in the use case
example:
use case buy drink from a vending machine
best-case scenario
no change to return scenario (will return the amount and
cancel transaction)

Prof. Ioan Salomie Programming Techniques

23

Sequence diagrams
Concepts
Activation

narrow rectangle below the object along


with the lifeline
the period of time during which the object
is processing the message
when an object finishes processing a
message the control returns to the sender
of the message. This marks the end of the
activation corresponding to that message.
it is marked by a dashed arrow going from
the bottom of the activation rectangle
back to the lifeline of the object that send
the message
activations and return messages are
optional in a sequence diagram
In the

course of message processing


one object may send messages to other
objects
Prof. Ioan Salomie Programming Techniques

24

Sequence diagrams
Buy drink use-case
Vending machine components (objects):
front panel, controller, delivery unit,
store
1. Client inserts the money into machine
front panel
2. The client makes a selection
3. The money go to the controller
4. The controller checks if the selected
item is in the store unit
5. Assuming a best case scenario, the
item exists, the controller updates the
cash and items and ask the delivery
unit to deliver the item from the store
6. The deliver unit delivers the items in
the front of the machine
Prof. Ioan Salomie Programming Techniques

25

Sequence diagrams
Buy drink use-case

Client

: Panel

: Controller

: Delivery

Insert(amount)
Transfer(amount)
Select(item)

Selected(item)
deliver(item)

instance sequence diagram


- covers only one scenario (best case)

Prof. Ioan Salomie Programming Techniques

26

Sequence diagrams
Buy drink use-case
Generic Sequence diagrams
Takes

into account the flow of control


Allow more scenarios in the diagram
The next diagram shows the sequence
diagram for incorrect-amount-of-money
scenario
Each condition causes a fork of control
in the message separating the message
into paths
Conditions are represented in square
brackets
[amount = price]

Another

scenario may involve out-of-

drink

Prof. Ioan Salomie Programming Techniques

27

Sequence diagrams
Buy drink use-case
Generic Sequence diagrams
Client

: Panel

: Controller

: Delivery

Insert(amount)
Transfer(amount)
[amount = price]

Select(item)
1
[amount > price
Check for change
2
[can do change]
return(change)
3
[can do change]
deliver the item

deliver(item)

4
deliver(item)

generic sequence diagram

4
[can not do change]
return(amount)
cancel transaction

Prof. Ioan Salomie Programming Techniques

28

Sequence diagrams
Object Creation and Destruction
: object1

:object2

new Object()
: anObject

: object1

:object2

: anObject

<< destroy>>

Prof. Ioan Salomie Programming Techniques

29

Sequence diagrams
Iterated messages
1
Assembly

: Assembly

Component

objects

: Component *

: aClient

: Assembly

class

: Component *

cost()
* cost()

Prof. Ioan Salomie Programming Techniques

30

Activity diagrams
Concepts

activity a state of performing some


tasks (a method of a class, a real world
activity (e.g. digging, eating, etc.))
activity diagram describes sequences of
activities
support for conditional and parallel
behavior

transition
branch
a decision point at which there are two or
more possible path of flow of control
single incoming transition
several guarded outgoing transitions
the branch is mutual exclusive
[else] all others are false
guard boolean expression
Prof. Ioan Salomie Programming Techniques

31

Activity diagrams
Concepts

(cont.)

merge
multiple entry transitions
single output
marks the end of conditional behavior

fork
one incoming transition
several outgoing transitions
when the incoming trans. is triggered, the
outgoing transitions are taken in parallel
can specify concurrent programming

join
multiple entry transitions
one outgoing transition
synchronization role

The

representation symbols of the main


concepts are shown in the activity
diagram example on the next slide
Prof. Ioan Salomie Programming Techniques

32

Activity diagrams
Example Order processing
Adapted from: M.FowlerUML Distilled, Addison Wesley 2000
Start
Receive
Order

Fork
Guard
condition
Prepare
goods
[rush ord]

Activity

Branch

[else]

Overnignt
Delivery

Send
Invoice

Receive
Payment

Regular
Delivery

Merge

Join
Close
Order

End

Prof. Ioan Salomie Programming Techniques

33

Activity diagrams
Composite activities
[adapted from m.fowler]
Receive
Order

Prepare
goods

Send
Invoice

Delivery
[rush ord]
Overnignt
Delivery

[else]

Receive
Payment

Regular
Delivery

Close
Order

Prof. Ioan Salomie Programming Techniques

34

Activity diagrams
Swim lanes
The activities and

actions of an
activity diagram can be grouped
into swim lanes
The activities in each lane can be
associated to a department (part) of
an organization
The lane borders are strictly
conceptual delimited

Prof. Ioan Salomie Programming Techniques

35

Activity diagrams
Swim lanes
Warehouse

Customer
Service

Accounting
Dept.

Receive
Order

Prepare
goods

Send
Invoice
Receive
Payment

Delivery

Close
Order

Prof. Ioan Salomie Programming Techniques

36

Package diagrams
diagrams help decomposition
of large systems into subsystems
In UML a package is a collection of
modelling elements that are grouped
together because they are logically related
When defining packages, the principles of
cohesion and coupling should be applied
UML Package symbol (see next slide)
may contain:
Package

classes, instances, text, other packages


A

package diagram shows packages of


classes and their dependencies
Dependency
two elements are dependent if changes to one
element may cause changes to the other

Prof. Ioan Salomie Programming Techniques

37

Package diagrams

Dependencies with classes


sending messages
one class defines an internal object instance of another
class
one class appears as parameter in an operation of
another class
inheritance

Ideally
only changing to a class interface should affect any
other class
m.fowler: the art of large-scale design involves
minimizing dependencies

During analysis in the early stages of design,


package diagrams can be used to show logical
grouping of class diagrams into sub-systems
packages
During implementation, package diagrams can be
used to show the grouping of physical
components into sub-systems
Prof. Ioan Salomie Programming Techniques

38

Package diagrams
Example
Adapted from: M.FowlerUML Distilled, Addison Wesley 2000

Order Capture
GUI

Graphics
Library

Mailing List
GUI

Order Capture
Application

Mailing List
Application

Domain

Orders

Customers

Database
Interface
{ abstract}

Oracle
Interface

MSQL
Interface
Prof. Ioan Salomie Programming Techniques

39

Component diagrams

System component
physical elements like files, executables, etc.

A component diagram shows how system


components relate to each other
Difference between package diagram and
component diagram
package diagram logical grouping of design elements
component diagrams physical components

Component diagrams can be combined with


deployment diagrams to show the physical
location of components of the system

<<communication>>
Client

Prof. Ioan Salomie Programming Techniques

Server

40

Component diagrams
<<Header>>
Order.h

Componnet
diagram
showing
dependencies
in C++

Dependency
of a compon.
on the interf.
of another
component

<<includes >>

<<Body>>
Order.cpp

<<o-Code>>
Order.o

<<Exec >>
GenOrder.exe

Component

Dependent

Prof. Ioan Salomie Programming Techniques

41

Deployment diagrams
Show

the physical architecture of the


system
Show the configuration of run-time
processing elements and the software
components and processes located on
them
The configuration shows the nodes and
communication associations
Nodes are typically used to show
computers, while communication
associations show the network and
protocols used to communicate between
nodes
Nodes are shown as 3D cubes
Deployment

diagrams can show either


types of machines or their names and / or
the active components within the nodes
Prof. Ioan Salomie Programming Techniques

42

Deployment diagrams

PC client

DBserver:DEC
JDBC

sun.jdbc

salesDB

Sales syst

Prof. Ioan Salomie Programming Techniques

43

Vous aimerez peut-être aussi