Vous êtes sur la page 1sur 24

Technical University of Cluj-Napoca Computer Science Department

Object Oriented Programming Techniques

UML Diagrams

Ioan Salomie
Spring 2012

Programming Techniques

Associations
Binary

relationships among classes Label (optional)


describes the associations directions arrow
Association

ends (see next figure)

role name multiplicity


Design

decisions related to navigation among class relations


unidirectional bidirectional

OO Programming Techniques

Association
Name, Role, Multiplicity

* Student * advisee

* Research Proj * proposes 1 1 Professor advisor

OO Programming Techniques

Associations
Bi-directional
ResearchProj * public class ReseachProj { private Professor theProfessor; public ResearchProj() { .. .}; }

1 Professor

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

Java class skeleton

OO Programming Techniques

Associations
Uni-directional
ResearchProj * public class ResearchProj { public ResearchProj() { }; }

1 Professor

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

Java class skeleton

Ioan Salomie - OO Programming Techniques

Associations
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
Association classes
Association as Example

class

class Contract describe the association (Company, Person)


UML

representation

* Person

1 Company

Contract

Ioan Salomie - OO Programming Techniques

Associations
Qualifiers to reduce multiplicity

a) Association

b) Association traversal

c) Qualifier

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 part-of wheel frame

bicycle 1 frame

2 wheel

part-of 36..72 spoke Horiz bar spoke 1 Horiz bar

Ioan Salomie - OO Programming Techniques

10

Aggregation Examples
Strong and weak aggregation
1 Bank * Branch 1 1..* Customer

1 Director 1 Staff

1 Clerck 1..*

Car

1 Body

4 Wheel

1 Engine

Ioan Salomie - OO Programming Techniques

11

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

12

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

13

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
proper classes with attributes and operations

Notes
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

14

Class diagrams
Conceptual level

Example 1

Prof. Ioan Salomie Programming Techniques

15

Class diagrams
Conceptual level
Example 2

Catalogue

* Component

* CatalogueEntry

Assembly

Part

Prof. Ioan Salomie Programming Techniques

16

Class diagrams
Implementation level Example - Delivery department
Customer { abstract } #customerID : String #address: String getHistory(): String Order - dateReceived : Date - orderNumber : String - value : int - isPrepaid : bool dispatch() close() CorporateCustomer - name : String - creditLimit : int PersonalCustomer - creditCardNo : String getHistory(): String getHistory(): String - quantity : int - unitPrice : int 1 * OrderLine

Adapted from: M.Fowler UML Distilled, Addison Wesley 2000

1 Product

Prof. Ioan Salomie Programming Techniques

17

Class diagrams
Implementation level Example - TreeMap

Adapted from: R.C.Martin UML for Java Programmers, Prentice Hall, 2002

Prof. Ioan Salomie Programming Techniques

18

Object diagrams
Example TreeMap Object Diagram

Adapted from: R.C.Martin UML for Java Programmers, Prentice Hall, 2002

Prof. Ioan Salomie Programming Techniques

19

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

20

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

21

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

Use-case

actor

ATM

withdraw money

client check balance


Prof. Ioan Salomie Programming Techniques

22

UML use-case Diagram


Example

System Actor

DVM

Use - case

Buy drink

Update stock

Customer

Collect

Agent

Maintenance Engineer

Prof. Ioan Salomie Programming Techniques

23

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

24

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

25

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

26

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

27

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

28

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

29

Sequence diagrams Buy drink use-case

Client Insert(amount)

: Panel

: Controller

: Delivery

Transfer(amount) Select(item) Selected(item) deliver(item)

instance sequence diagram - covers only one scenario (best case)

Prof. Ioan Salomie Programming Techniques

30

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

31

Sequence diagrams
Buy drink use-case Generic Sequence diagrams
Client Insert(amount) Transfer(amount) Select(item) 1 [amount > price Check for change 2 [can do change] return(change) 3 [can do change] deliver the item 4 [can not do change] return(amount) cancel transaction deliver(item) 2 4 deliver(item) [amount = price] 1 3

: Panel

: Controller

: Delivery

generic sequence diagram

Prof. Ioan Salomie Programming Techniques

32

Sequence diagrams
Object Creation and Destruction
: object1 :object2

new Object() : anObject

: object1

:object2

: anObject

<< destroy>>

Prof. Ioan Salomie Programming Techniques

33

Sequence diagrams
Iterated messages
1 Assembly * Component class

: Assembly

: Component *

objects

: aClient

: Assembly

: Component *

cost() * cost()

Prof. Ioan Salomie Programming Techniques

34

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

35

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

36

Activity diagrams
Example Order processing
Adapted from: M.FowlerUML Distilled, Addison Wesley 2000
Start Fork Guard condition Prepare goods [rush ord] Branch Receive Order

Activity

Send Invoice

[else] Regular Delivery

Receive Payment

Overnignt Delivery

Merge Close Order End

Join

Prof. Ioan Salomie Programming Techniques

37

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

Prepare goods Delivery [rush ord] Overnignt Delivery [else] Regular Delivery

Send Invoice

Receive Payment

Close Order

Prof. Ioan Salomie Programming Techniques

38

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

39

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

40

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

41

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

42

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

43

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

Server

Prof. Ioan Salomie Programming Techniques

44

Component diagrams
<<Header>> Order.h <<includes >> <<Body>> Order.cpp

Componnet diagram showing dependencies in C++

<<o-Code>> Order.o

<<Exec >> GenOrder.exe

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

Component

Dependent

Prof. Ioan Salomie Programming Techniques

45

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

46

Deployment diagrams

PC client JDBC sun.jdbc

DBserver:DEC salesDB

Sales syst

Prof. Ioan Salomie Programming Techniques

47

Vous aimerez peut-être aussi