L2

24
1 Programming Techniques Technical University of Cluj-Napoca Computer Science Department Object Oriented Programming Techniques UML Diagrams Ioan Salomie Spring 2012 2 OO 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

description

java2

Transcript of L2

Page 1: L2

1 Programming Techniques

Technical University of Cluj-Napoca

Computer Science Department

Object Oriented

Programming Techniques

UML Diagrams

Ioan Salomie

Spring 2012

2 OO 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

Page 2: L2

3 OO Programming Techniques

Association Name, Role, Multiplicity

Student Research Proj

Professor

* *

*

proposes

*

1

advisee

advisor

1

4 OO Programming Techniques

Associations Bi-directional

ResearchProj

*

1

public class ReseachProj {

private Professor theProfessor;

public ResearchProj() { .. .};

}

public class Professor {

private ResearchProj[] rp = new

ResearchProj[MAX_RP];

public Professor() { … };

} Professor

Java class skeleton

Page 3: L2

5 Ioan Salomie - OO Programming Techniques

Associations Uni-directional

ResearchProj

*

1

public class ResearchProj {

public ResearchProj() { … };

}

public class Professor {

private ReserachProj[] rp = new

ResearchProj[MAX_RP];

public Professor() { … };

} Professor

Java class skeleton

6 Ioan Salomie - OO Programming Techniques

Associations Reflexive associations

Course

class Course {

public:

public Course() { }

// the unbounded multiple association

// is stored in a Vector

private ArrayList prerequisites;

}

0..*

prerequisites

Page 4: L2

7 Ioan Salomie - OO Programming Techniques

Association Association classes

Association as class

Example

– class Contract describe the association

(Company, Person)

UML representation

Person Company

Contract

1 *

8 Ioan Salomie - OO Programming Techniques

Associations Qualifiers to reduce multiplicity

a) Association b) Association

traversal c) Qualifier

Page 5: L2

9 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

10 Ioan Salomie - OO Programming Techniques

Representation

bicycle

wheel frame

part-of

spoke Horiz bar

part-of

bicycle

wheel frame

2

spoke Horiz bar

36..72

1

UML

1

Page 6: L2

11 Ioan Salomie - OO Programming Techniques

Aggregation Examples

Strong and weak aggregation

1 1

Director Clerck

Branch Customer

Staff

1..* 1

1..* 1

Bank 1

*

Car

Body Wheel Engine

1 4 1

12 Prof. Ioan Salomie – Programming Techniques

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

Page 7: L2

13 Prof. Ioan Salomie – Programming Techniques

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

14 Prof. Ioan Salomie – Programming Techniques

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

Page 8: L2

15 Prof. Ioan Salomie – Programming Techniques

Class diagrams Conceptual level

Example 1

16 Prof. Ioan Salomie – Programming Techniques

Class diagrams Conceptual level

Component

Assembly Part

* CatalogueEntry

Catalogue

1 1 *

Example 2

Page 9: L2

17 Prof. Ioan Salomie – Programming Techniques

Class diagrams Implementation level

Example - Delivery department

Customer

{ abstract }

#customerID : String

#address: String

getHistory(): String

CorporateCustomer

- name : String

- creditLimit : int

getHistory(): String

PersonalCustomer

- creditCardNo : String

getHistory(): String

Order

- dateReceived : Date

- orderNumber : String

- value : int

- isPrepaid : bool

dispatch()

close()

OrderLine

- quantity : int

- unitPrice : int

*

1

Product

1

1

1 *

Adapted from: M.Fowler–

UML Distilled, Addison Wesley 2000

18 Prof. Ioan Salomie – Programming Techniques

Class diagrams Implementation level

Example - TreeMap

Adapted from: R.C.Martin–

UML for Java Programmers, Prentice Hall, 2002

Page 10: L2

19 Prof. Ioan Salomie – Programming Techniques

Object diagrams Example – TreeMap Object Diagram

Adapted from: R.C.Martin–

UML for Java Programmers, Prentice Hall, 2002

20 Prof. Ioan Salomie – Programming Techniques

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, use-

cases and their relationships

Page 11: L2

21 Prof. Ioan Salomie – Programming Techniques

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

22 Prof. Ioan Salomie – Programming Techniques

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

check balance

withdraw money

client

ATM actor

System Use-case

Page 12: L2

23 Prof. Ioan Salomie – Programming Techniques

UML use-case Diagram Example

DVM

Buy drink

Update stock

Maintenance

Collect Customer

Agent

Engineer

System Use - case

Actor

24 Prof. Ioan Salomie – Programming Techniques

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)

Page 13: L2

25 Prof. Ioan Salomie – Programming Techniques

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

26 Prof. Ioan Salomie – Programming Techniques

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

Page 14: L2

27 Prof. Ioan Salomie – Programming Techniques

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)

28 Prof. Ioan Salomie – Programming Techniques

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

Page 15: L2

29 Prof. Ioan Salomie – Programming Techniques

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

30 Prof. Ioan Salomie – Programming Techniques

Sequence diagrams

“Buy drink” use-case

: Panel : Controller : Delivery

Insert(amount)

Select(item)

Transfer(amount)

deliver(item)

Client

Selected(item)

instance sequence diagram

- covers only one scenario (best case)

Page 16: L2

31 Prof. Ioan Salomie – Programming Techniques

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’

32 Prof. Ioan Salomie – Programming Techniques

Sequence diagrams Buy drink use-case

Generic Sequence diagrams

: Panel : Controller : Delivery

Insert(amount)

Select(item)

Transfer(amount)

deliver(item)

Client

[amount = price]

1

[amount > price

Check for change

1

2 3

4

[can do change]

deliver the item

3

[can do change]

return(change)

2

[can not do change]

return(amount)

‘cancel transaction’

4

generic sequence diagram

deliver(item)

Page 17: L2

33 Prof. Ioan Salomie – Programming Techniques

Sequence diagrams Object Creation and Destruction

: object1 :object2

: anObject

new Object()

: object1 :object2 : anObject

… << destroy>>

34 Prof. Ioan Salomie – Programming Techniques

Sequence diagrams Iterated messages

: aClient : Assembly : Component *

cost()

Assembly Component 1 *

* cost()

: Assembly : Component *

class

objects

Page 18: L2

35 Prof. Ioan Salomie – Programming Techniques

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

36 Prof. Ioan Salomie – Programming Techniques

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

Page 19: L2

37 Prof. Ioan Salomie – Programming Techniques

Activity diagrams Example – Order processing Adapted from: M.Fowler–UML Distilled, Addison Wesley 2000

Activity

Receive Order

Prepare goods

Send Invoice

Overnignt Delivery

Regular Delivery

Receive Payment

Close Order

Start

Fork

Join

Branch

Merge

End

[rush ord] [else]

Guard

condition

38 Prof. Ioan Salomie – Programming Techniques

Activity diagrams Composite activities

Receive Order

Prepare goods Send

Invoice

Overnignt Delivery

Regular Delivery

Receive Payment

Close Order

[rush ord] [else]

Delivery

[adapted from m.fowler]

Page 20: L2

39 Prof. Ioan Salomie – Programming Techniques

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

40 Prof. Ioan Salomie – Programming Techniques

Activity diagrams Swim lanes

Receive Order

Prepare goods Send

Invoice

Delivery Receive Payment

Close Order

Accounting

Dept.

Warehouse Customer

Service

Page 21: L2

41 Prof. Ioan Salomie – Programming Techniques

Package diagrams

Package 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:

– 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

42 Prof. Ioan Salomie – Programming Techniques

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

Page 22: L2

43 Prof. Ioan Salomie – Programming Techniques

Package diagrams Example Adapted from: M.Fowler–UML Distilled, Addison Wesley 2000

Order Capture

GUI

Order Capture

Application

Graphics

Library

Mailing List

GUI

Mailing List

Application

Orders Customers

Domain

Database

Interface

{ abstract}

Oracle

Interface

MSQL

Interface

44 Prof. Ioan Salomie – Programming Techniques

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

Client Server

<<communication>>

Page 23: L2

45 Prof. Ioan Salomie – Programming Techniques

Component diagrams

<<includes >> <<Body>>

Order.cpp

<<Header>>

Order.h

<<o-Code>>

Order.o

<<Exec >>

GenOrder.exe

Component

Dependent

Componnet

diagram

showing

dependencies

in C++

Dependency

of a compon.

on the interf.

of another

component

46 Prof. Ioan Salomie – Programming Techniques

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

Page 24: L2

47 Prof. Ioan Salomie – Programming Techniques

Deployment diagrams

PC client

sun.jdbc

Sales syst

DBserver:DEC

salesDB

JDBC