modeling with uml: basic notations ii€¦ · © 2006 bernd bruegge software engineering ws...

42
1 © 2006 Bernd Bruegge Software Engineering WS 2006/2007 Modeling with UML: Basic Notations II Prof. Bernd Bruegge, Ph.D. Applied Software Engineering Technische Universitaet Muenchen Software Engineering I Lecture 3 8 November 2006

Upload: others

Post on 18-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

1© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Modeling with UML:Basic Notations II

Prof. Bernd Bruegge, Ph.D.Applied Software Engineering

Technische Universitaet Muenchen

Software Engineering ILecture 3

8 November 2006

Page 2: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

2© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Outline of this Class

• A more detailed view on

• Use case diagrams• Class diagrams• Sequence diagrams• Activity diagrams

Page 3: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

3© 2006 Bernd Bruegge Software Engineering WS 2006/2007

UML Use Case Diagrams

An Actor represents a role, thatis, a type of user of the system

Passenger

PurchaseTicket

Used during requirementselicitation and analysis torepresent external behavior

Use case model:The set of all use cases thatcompletely describe thefunctionality of the system andits environment

A use case represents a class offunctionality provided by the systemas an event flow

Page 4: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

4© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Actors

• An actor models an external entitywhich communicates with thesystem:

• User• External system• Physical environment

• An actor has a unique name and anoptional description

• Examples:• Passenger: A person in the train• GPS satellite: An external system that

provides the system with GPScoordinates.

Passenger

Page 5: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

5© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Use Case

• A use case represents a class offunctionality provided by thesystem as an event flow

• Use cases can be describedtextually

• A textual use case descriptionconsists of 6 parts:1. Unique name2. Participating actors3. Entry conditions4. Exit conditions5. Flow of events6. Special requirements

PurchaseTicket

Page 6: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

6© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Textual Use CaseDescription Example

1. Name: Purchase ticket

2. Participating actor:Passenger

3. Entry condition:• Passenger standing in

front of ticket distributor• Passenger has sufficient

money to purchase ticket

4. Exit condition:• Passenger has ticket

5. Flow of events:1. Passenger selects the

number of zones to betraveled

2. Ticket Distributordisplays the amount due

3. Passenger insertsmoney, at least theamount due

4. Ticket Distributor returnschange

5. Ticket Distributor issuesticket

6. Special requirements:None.

Passenger PurchaseTicket

Page 7: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

7© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Uses Cases can be related

• Extends Relationship• To represent seldom invoked use cases or exceptional

functionality

• Includes Relationship• To represent functional behavior common to more than

one use case.

Page 8: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

8© 2006 Bernd Bruegge Software Engineering WS 2006/2007

The <<extends>> Relationship• <<extends>> relationships

model exceptional or seldominvoked cases

• The exceptional event flowsare factored out of the mainevent flow for clarity

• Use cases representingexceptional flows can extendmore than one use case

• The direction of an<<extends>> relationship is tothe extended use case.

Passenger

PurchaseTicket

TimeOut

<<extends>>

NoChange

<<extends>>OutOfOrder

<<extends>>

Cancel

<<extends>>

Page 9: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

9© 2006 Bernd Bruegge Software Engineering WS 2006/2007

The <<includes>> Relationship• <<includes>> relationship

represents commonfunctionality needed in morethan one use case

• <<includes>> behavior isfactored out for reuse, notbecause it is an exception

• The direction of a<<includes>> relationship isto the using use case (unlikethe direction of the<<extends>> relationship).

Passenger

PurchaseSingleTicket

PurchaseMultiCard

<<includes>>

CollectMoney

<<includes>>

NoChange

<<extends>>

Cancel

<<extends>>

Cancel

<<extends>>

Page 10: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

10© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Class Diagrams

• Class diagrams represent the structure of thesystem

• Used• during requirements analysis to model application

domain concepts• during system design to model subsystems• during object design to specify the detailed behavior

and attributes of classes.

Table zone2priceEnumeration getZones()Price getPrice(Zone)

TarifSchedule

* *

Tripzone:Zone

Price: Price

Page 11: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

11© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Classes

• A class represents a concept• A class encapsulates state (attributes) and behavior

(operations)

Table zone2priceEnumeration getZones()Price getPrice(Zone)

TarifSchedule

zone2pricegetZones()getPrice()

TarifSchedule

Name

Attributes

Operations

Signature

TarifSchedule

The class name is the only mandatory information

Each attribute has a typeEach operation has a signature

Type

Page 12: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

12© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Instances

• An instance represents a phenomenon• The attributes are represented with their values• The name of an instance is underlined• The name can contain only the class name of the instance

(anonymous instance)

zone2price = {{‘1’, 0.20},{‘2’, 0.40},{‘3’, 0.60}}

tarif2006:TarifSchedulezone2price = {{‘1’, 0.20},{‘2’, 0.40},{‘3’, 0.60}}

:TarifSchedule

Page 13: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

13© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Actor vs Class vs Object

• Actor• An entity outside the system to be modeled,

interacting with the system (“Passenger”)

• Class• An abstraction modeling an entity in the application or

solution domain• The class is part of the system model (“User”, “Ticket

distributor”, “Server”)

• Object• A specific instance of a class (“Joe, the passenger who

is purchasing a ticket from the ticket distributor”)

Page 14: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

14© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Associations

Associations denote relationships between classes

PriceZone

Enumeration getZones()Price getPrice(Zone)

TarifSchedule TripLeg

* *

The multiplicity of an association end denotes how manyobjects the instance of a class can legitimately reference.

Page 15: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

15© 2006 Bernd Bruegge Software Engineering WS 2006/2007

1-to-1 and 1-to-many Associations

1-to-1 association

1-to-many association

Polygon

draw()

Point

x: Integer

y: Integer

*

Country

name:String

City

name:String

11

Page 16: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

16© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Many-to-Many Associations

StockExchange

Company

tickerSymbolLists

**

Name of Assocation

Page 17: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

17© 2006 Bernd Bruegge Software Engineering WS 2006/2007

From Problem Statement To Object Model

Class Diagram:

StockExchange Company

tickerSymbolLists

**

Problem Statement: A stock exchange lists many companies.Each company is uniquely identified by a ticker symbol

Page 18: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

18© 2006 Bernd Bruegge Software Engineering WS 2006/2007

From Problem Statement to Code

Problem Statement : A stock exchange lists many companies. Each company is identified by a ticker symbol

Class Diagram:

private Vector m_Company = new Vector();

public int m_tickerSymbol; private Vector m_StockExchange = new Vector();

public class StockExchange{

};

public class Company{

};

Java Code

StockExchange Company

tickerSymbolLists **

Associationsare mapped to

Attributes!

Page 19: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

19© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Aggregation• An aggregation is a special case of association denoting

a “consists-of” hierarchy• The aggregate is the parent class,

the components are the children classes

Exhaust system

Mufflerdiameter

Tailpipediameter

1 0..2

TicketMachine

ZoneButton3

A solid diamond denotes composition: A strong form ofaggregation where the life time of the component instances iscontrolled by the aggregate (“the whole controls/destroys theparts”)

Page 20: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

20© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Qualifiers

• Qualifiers can be used to reduce the multiplicityof an association

DirectoryFile

filename

Without qualification1 *

With qualification

0..1Directory File

1filename

Page 21: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

21© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Qualification (2)

*StockExchangeCompanyLists *tickerSymbol

1

StockExchange

Company

tickerSymbolLists

**

Page 22: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

22© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Inheritance

• Inheritance is another special case of anassociation denoting a “kind-of” hierarchy

• Inheritance simplifies the analysis model byintroducing a taxonomy

• The children classes inherit the attributes andoperations of the parent class.

Button

ZoneButtonCancelButton

Page 23: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

23© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Object Modeling in Practice

Class Identification: Name of Class, Attributes and Methods

Is Foo the right name?

Foo

AmountCustomerId

Deposit()Withdraw()GetBalance()

Page 24: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

24© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Object Modeling in Practice: Brainstorming

Foo

AmountCustomerId

Deposit()Withdraw()GetBalance()

Account

AmountCustomerId

Deposit()Withdraw()GetBalance()Is Foo the right name?

“Dada”

AmountCustomerId

Deposit()Withdraw()GetBalance()

Page 25: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

25© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Object Modeling in Practice: More classes

Account

Amount

Deposit()Withdraw()GetBalance()

Customer

NameCustomerId

CustomerIdAccountIdBank

Name

1) Find New Classes2) Review Names, Attributes and Methods

Page 26: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

26© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Object Modeling in Practice: Associations

Account

Amount

Deposit()Withdraw()GetBalance()

Customer

NameCustomerId

CustomerIdAccountIdAccountIdBank

Name

1) Find New Classes2) Review Names, Attributes and Methods

3) Find Associations between Classes

owns

4) Label the generic assocations

6) Review associations

*2

*?has

5) Determine the multiplicity of the assocations

Page 27: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

27© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Practice Object Modeling: Find Taxonomies

SavingsAccount

Withdraw()

CheckingAccount

Withdraw()

MortgageAccount

Withdraw()

Account

Amount

Deposit()Withdraw()GetBalance()

CustomerIdAccountIdAccountId

Customer

Name

CustomerId()

Has*Bank

Name*

Page 28: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

28© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Practice Object Modeling: Simplify, Organize

SavingsAccount

Withdraw()

CheckingAccount

Withdraw()

MortgageAccount

Withdraw()

Account

Amount

Deposit()Withdraw()GetBalance()

CustomerIdAccountIdAccountIdShow Taxonomies

separately

Page 29: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

29© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Practice Object Modeling: Simplify, Organize

Customer

Name

CustomerId()

Account

Amount

Deposit()Withdraw()GetBalance()

CustomerIdAccountIdAccountId

Bank

Name Has**

Use the 7+-2 heuristicsor 5+-2!

Page 30: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

30© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Packages

• Packages help you to organize UML models toincrease their readability

• We can use the UML package mechanism toorganize classes into subsystems

• Any complex system can be decomposed intosubsystems, where each subsystem is modeled asa package.

Account

CustomerBank

Page 31: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

31© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Sequence Diagrams

• Used during analysis• To refine use case descriptions• to find additional objects

(“participating objects”)

• Used during system design• to refine subsystem interfaces

• Instances are represented byrectangles. Actors by stickyfigures

• Lifelines are represented bydashed lines

• Messages are represented byarrows

• Activations are representedby narrow rectangles.

selectZone()

pickupChange()

pickUpTicket()

insertCoins()

TicketMachinePassenger

Focus on Controlflow

Messages ->Operations on

participating Object

zone2priceselectZone()insertCoins()pickupChange()pickUpTicket()

TicketMachine

Page 32: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

32© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Sequence Diagrams can also model theFlow of Data

• The source of an arrow indicates the activation which sentthe message

• Horizontal dashed arrows indicate data flow, for examplereturn results from a message

Passenger

selectZone()

ZoneButton TarifSchedule Display

lookupPrice(selection)

displayPrice(price)

price

Dataflow…continued on next slide...

Page 33: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

33© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Sequence Diagrams: Iteration & Condition

• Iteration is denoted by a * preceding the message name• Condition is denoted by boolean expression in [ ] before

the message name

Passenger ChangeProcessor

insertChange(coin)

CoinIdentifier Display CoinDrop

displayPrice(owedAmount)

lookupCoin(coin)

price

[owedAmount<0] returnChange(-owedAmount)

Iteration

Condition

…continued on next slide...

…continued from previous slide...

*

Page 34: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

34© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Creation and destruction

• Creation is denoted by a message arrow pointing to the object.• Destruction is denoted by an X mark at the end of the

destruction activation.• In garbage collection environments, destruction can be used to

denote the end of the useful life of an object.

Passenger ChangeProcessor

…continued from previous slide...

Ticket

createTicket(selection)

free()

Creation of Ticket

Destruction of Ticket

print()

Page 35: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

35© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Sequence Diagram Properties

• UML sequence diagram represent behavior interms of interactions

• Useful to identify or find missing objects• Time consuming to build, but worth the

investment• Complement the class diagrams (which

represent structure).

Page 36: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

36© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Outline of this Class

• A more detailed view on

Use case diagramsClass diagramsSequence diagramsActivity diagrams

Page 37: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

37© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Activity Diagrams

• An activity diagram is a special case of a statechart diagram

• The states are activities (“functions”)• An activity diagram is useful to depict the

workflow in a system

HandleIncident

DocumentIncident

ArchiveIncident

Page 38: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

38© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Activity Diagrams allow to model Decisions

OpenIncident

NotifyPolice Chief

NotifyFire Chief

AllocateResources

[fire & highPriority]

[not fire & highPriority]

[lowPriority]

Decision

Page 39: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

39© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Activity Diagrams can model Concurrency

• Synchronization of multiple activities• Splitting the flow of control into multiple threads

OpenIncident

AllocateResources

CoordinateResources

DocumentIncident

ArchiveIncident

SynchronizationSplitting

Page 40: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

40© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Activity Diagrams: Grouping of Activities

• Activities may be grouped into swimlanes todenote the object or subsystem that implementsthe activities.

OpenIncident

AllocateResources

CoordinateResources

DocumentIncident

ArchiveIncident

Dispatcher

FieldOfficer

Page 41: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

41© 2006 Bernd Bruegge Software Engineering WS 2006/2007

Activity Diagram vs. Statechart Diagram

HandleIncident

DocumentIncident

ArchiveIncident

Active Inactive Closed ArchivedIncident-Handled

Incident-Documented

Incident-Archived

Statechart Diagram for IncidentFocus on the set of attributes of a single abstraction (object, system)

Activity Diagram for Incident(Focus on dataflow in a system)

TriggerlesstransitionCompletion of activity

causes state transition

Event causesstate transition

Page 42: Modeling with UML: Basic Notations II€¦ · © 2006 Bernd Bruegge Software Engineering WS 2006/2007 3 UML Use Case Diagrams An Actor represents a role, that is, a type of user of

42© 2006 Bernd Bruegge Software Engineering WS 2006/2007

UML Summary

• UML provides a wide variety of notations forrepresenting many aspects of softwaredevelopment

• Powerful, but complex

• UML is a programming language• Can be misused to generate unreadable models• Can be misunderstood when using too many exotic

features

• We concentrate on a few notations:• Functional model: Use case diagram• Object model: class diagram• Dynamic model: sequence diagrams, statechart and

activity diagrams