Handout2 Extra

Preview:

Citation preview

November 7, 2005

Lilith Al-JadiriLehrstuhl für Angewandte Softwaretechnik

Institut für InformatikTechnische Universität München

aljadiri@in.tum.de

Software EngineeringExercise

Modeling with UML:Sequence and Activity Diagrams

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 2

Content27.10.05 Modeling with UML: Use Case and class diagrams03.11.05 Modeling with UML: Sequence and activity

diagrams10.11.05 Requirements Elicitation17.11.05 Analysis24.11.05 System Design01.12.05 Dies Academicus08.12.05 Object Design: Using Design Patterns15.12.05 Object Design: Object Constraint Language22.12.05 to 12.01.06 Mapping models to code19.01.06 to 09.02.06 Miniproject: Asteroids26.01.06 to 02.02.06 Testing

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 3

Exercise 1

Consider an ATM system. Identify at least threedifferent actors that interact with this system?

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 4

Exercise 2

Can the system under consideration berepresented as an actor?

Justify your answer.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 5

Exercise 3

What is the difference between a scenario and ause case?

When do you use each construct?

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 6

Exercise 4Draw a use case diagram for a ticket distributor for a trainsystem. The system includes two actors: a traveler, whopurchases different types of tickets, and a centralcomputer system, which maintains a reference databasefor the tariff. Use cases should include:BuyOneWayTicket, BuyWeeklyCard,BuyMonthlyCard, UpdateTariff. Also include thefollowing exceptional cases: Time-Out (i.e., traveler tooktoo long to insert the right amount),TransactionAborted (i.e., traveler selected the cancelbutton without completing the transaction),DistributorOutOfChange, andDistributorOutOfPaper.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 7

Exercise 5

Write the flow of events and specify all fields for theuse case UpdateTariff that you drew in Exercise4.

Do not forget to specify any relationships.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 8

Exercise 5: Use Case Template

Use case name UpdateTariff Participating Actors …. Flow of events 1. … 2. …. 3. … :

: :

Entry condition ….. Exit condition …. Quality requirements ….

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 9

Exercise 5: SolutionUse case name UpdateTariff

Participating Actors Initiated by CentralComputerSystem

Flow of events 1. The CentralComputerSystem activates the “UpdateTariff” function of the

ticket distributors available on the network.

2. The ticket distributor disables the traveler interface and

posts a sign indicating that the ticket distributor is under

maintenance.

3. The ticket distributor waits for the new database from the

CentralCo mputerSystem.

4. After waiting a minute for the ticket distributors to reach a waiting state,

the CentralComputerSystem broadcasts the new database.

5. The ticket distributor system receives the new database

of tariff. Upon complete, the ticket distributor sends an

acknowledgement to the CentralComputerSystem

6. After acknowledgment, the ticket distributor enables the

traveler interface and can issue tickets at the new tariff.

7. The CentralComputerSystem checks if all ticket distributors have

acknowledged the new database. If not, the CentralComputerSystem

invokes the CheckNonRespo ndingDistributo rs

use case.

Entry condition The ticket distributor is connected to a network reachable by the

CentralComputerSystem.

Exit condition The ticket distributor can issue tickets under the new tariff, OR

The ticket distributor is disabled and displays a sign denoting that it is

under maintenance.

Quality requirements The ticket distributor stays offline at most 2 minutes and is considered out-

of-order otherwise.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 10

Exercise 6

Draw a class diagram representing a book definedby the following statement:

“A book is composed of a number of parts, which inturn are composed of a number of chapters.Chapters are composed of sections.”

Focus only on classes and relationships.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 11

Exercise 7

Add multiplicity to the class diagram you producedin Exercise 6.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 12

Exercise 6 & 7: Solution

Book

Part

Chapter

Section

1*

1

*

*

1

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 13

Exercise 8

Draw an object diagram representing the first partof the oose book (table of contents is on sheet 1).

Make sure that the object diagram you draw isconsistent with the class diagram of Exercise 6.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 14

Exercise 9

Extend the class diagram of Exercise 6 to includethe following attributes:

• book includes a publisher, publication date, andan ISB

• a part includes a title and a number• a chapter includes a title, a number, and an

abstract• a section includes a title and a number

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 15

Exercise 9: Solution

Bookpublisher:PersonpublicationDate:DateISBN:Integer[4]

Part Chapter Sectiontitle:Stringnumber:Integer

title:Stringnumber:Integerabstract:String

title:Stringnumber:Integer

1 * 1 * 1 *

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 16

Exercise 10

Consider the class diagram of Exercise 9. Note thatthe Part, Chapter, and Section classes all includea title and a number attribute. Add an abstractclass and a generalization relationship to factorout these two attributes into the abstract class.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 17

Exercise 10: Solution

Bookpublisher:PersonpublicationDate:DateISBN:Integer[4]

Part Chapter Sectionabstract:String

1 * 1 * 1 *

title:Stringnumber:Integer

NumberedComponent

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 18

Exercise 11

Draw a class diagram representing the relationshipbetween parents and children. Take into accountthat a person can have both a parent and a child.Annotate associations with roles and multiplicities.

Person

2*child

parent

Solution

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 19

Hand-out 2

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 20

Sequence Diagrams• Used during requirements analysis

– To refine use case descriptions– to find additional objects

(“participating objects”)• Used during system design

– to refine subsystem interfaces• Used during Testing

– to specify expected behaviour andvalidate output

• Classes are represented byrectangles

• Lifelines are represented bydashed lines

• Messages are represented byarrows

• Activations are represented bynarrow rectangles.

selectZone()

pickupChange()

pickUpTicket()

insertCoins()

TicketMachinePassenger

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 21

Sequence Diagrams: Dataflow

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

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 22

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

*

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 23

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

Destruction

print()

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 24

Exercise 1

Draw a sequence diagram for the warehouseOnFire

scenario of Figure 1. Include the objects bob, alice,

john, FRIEND, and instances of other classes youmay need. Draw only the first five messagesends.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 25

Exercise 1 contd.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 26

Exercise 1: Solution

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 27

Exercise 2

Draw a sequence diagram for the ReportIncident usecase of Figure 2. Draw only the first five messagesends. Make sure it is consistent with thesequence diagram of Exercise 1.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 28

Exercise 2 contd.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 29

Exercise 2: Solution

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 30

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

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 31

Activity Diagrams allow to model Decisions

OpenIncident

NotifyPolice Chief

NotifyFire Chief

AllocateResources

[fire & highPriority]

[not fire & highPriority]

[lowPriority]

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 32

Activity Diagrams allow to model Concurrency

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

OpenIncident

AllocateResources

CoordinateResources

DocumentIncident

ArchiveIncident

SynchronizationSplitting

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 33

Activity Diagrams: Grouping of Activities• Activities may be grouped into swimlanes to

denote the object or subsystem that implementsthe activities.

OpenIncident

AllocateResources

CoordinateResources

DocumentIncident

ArchiveIncident

Dispatcher

FieldOfficer

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 34

Exercise 3

Consider the process of ordering a pizza over thephone. Draw an activity diagram representingeach step of the process, from the moment youpick up the phone to the point where you starteating the pizza. Do not represent anyexceptions. Include activities that others need toperform.

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 35

Exercise 3: Solution

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 36

Exercise 4

Add exception handling to the activity diagram youdeveloped in Exercise 3. Consider at least threeexceptions (e.g. delivery person wrote downwrong address, deliver person brings wrongpizza, store out of anchovies).

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 37

Exercise 4: Solution

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 38

Classes in a Class Diagram• Class name only Example

• With Details Example

Class NameBank

Account

Class Nameattributesmethods

Bank Accountdouble balance

deposit()withdraw()

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 39

Relationships• Inheritance (arrow)

– example: between Secretary and Employee• Composition/Aggregation (diamond)

– example: between Car and Wheel• Association (line)

– example: between Borrower and Book

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 40

Inheritance

Secretary

Employee

public class Secretary extends Employee { …}

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 41

Composition/Aggregation

Car Wheel4

w[]

public class Car { Wheel w[]; ... public Car() { w = new Wheel[4]; … } ...}

Note: [ ] in diagramis sometimes left outsince w does not needto be an array

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 42

Association

Borrower BookcurrBorr bk[]

31

public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; }}

public class Book { Borrower currBorr; …}

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 43

Notational Details• Cardinality

– Specifies the number of objects that may participate inthe relationship

• Roles and Navigability– Specifies relationship name and access

• Aggregation versus Composition• Dependencies

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 44

Cardinality• Also known as multiplicity

– Exact number (mandatory)– Range (e.g., 0..5)– * (many-valued)

• Specifies the number of objects that may beassociated with an object of the other class

• For associations, multiplicity is specified on bothparticipants

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 45

Roles and Navigability• Role name placed on the side of a participant• Let A and B be associated classes and let rrr be

the role specified on B’s side– rrr is the role of B in the relationship– rrr is a member in class A– rrr refers to one or more (depending on multiplicity) B

objects• An arrowhead indicates the ability to access B

participant(s) from A

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 46

Uni-directional Navigability

PriceChecker

getPrice()

pcFastFoodCounter

public class FastFoodCounter { PriceChecker pc; … public void add( … ) { … double pr = pc.getPrice(); … } …}

public class PriceChecker { // no access to counter}

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 47

Bi-directional Navigability

Borrower BookcurrBorr bk[]

31

public class Borrower { Book bk[]; … public Borrower() { bk = new Book[3]; }}

public class Book { Borrower currBorr; …}

Note: double arrowheads maybe omitted (bi-directionalnavigability assumed)

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 48

Aggregation versus Composition• Part-of relationships• Aggregation

– Part may be independent of the whole but the wholerequires the part

– Unfilled diamond• Composition (“stronger” form of aggregation)

– Part is created and destroyed with the whole– Filled diamond

• Definitions and distinctions between aggregationand composition still “under debate”

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 49

Association/Aggregation/Composite• Composition

– E.g., Glider has a component Tail– Composite object is the whole– Component is the part

• Composite cannot exist without component• A component is part of only one composite

– Stronger: composite should create component– Stronger: if delete composite, component goes away

too, i.e., cascading delete– Typically a composite has many different kinds of

components

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 50

Whole/Part Associations• Aggregation, weaker than composition

– E.g., City is an aggregate of houses– Aggregate is the whole– Constituent is the part

• Aggregate may exist without constituents• Each object may be part of more than one

aggregate• Typically, constituents are of same class

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 51

Composition/Aggregation Notations

MovieTheater BoxOffice

Movie

1 1

0..*

0..*

composition

aggregation

whole

part

part

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 52

Composition vs. Aggregation• In real world there are 7 or 8 varieties of

whole/part relationships, but UML has only twoconstructs – composition and aggregation

• It can be very difficult, and confusing to chooseone

• UML 2.0 has essentially deprecated aggregation– So, don’t use it (choose composition or association)– But you might see it

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 53

Dependencies• Some classes use other classes but are not

related to them in ways previously discussed• Not relationships in the sense that participants do not become

attributes in another class

• Most common example:– As local variables in (or arguments to) a method of the

class

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 54

Dependency Example

Parser

getOrder()

usesRestaurant

processOrders()

public class Restaurant { … public void processOrders() { Parser p = new Parser(…); // call getOrder() in this method } …}

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 55

Association/Aggregation/Composition

Window

Slider Header Panel

1 1 1

2 11scroll title body

Windowscrollbar[2]: Slidertitle: Headerbody: Panel

November 7, 2005Software Engineering Exercise 1Lilith Al-Jadiri 56

Mandatory Parts

Car Wheel4

wheels

public class Car{private Wheel wheels[4]; // wheel objects are created externally ...public Car(Wheel w1, Wheel w2, … ) … // wheels required in constructor // w1, w2, … will be checked for null values}

Recommended