Вы находитесь на странице: 1из 56

Software Engineering

Exercise
Modeling with UML:
Sequence and Activity Diagrams

Lilith Al-Jadiri
Lehrstuhl für Angewandte Softwaretechnik
Institut für Informatik
Technische Universität München
aljadiri@in.tum.de

November 7, 2005
Content
27.10.05 Modeling with UML: Use Case and class diagrams
03.11.05 Modeling with UML: Sequence and activity
diagrams
10.11.05 Requirements Elicitation
17.11.05 Analysis
24.11.05 System Design
01.12.05 Dies Academicus
08.12.05 Object Design: Using Design Patterns
15.12.05 Object Design: Object Constraint Language
22.12.05 to 12.01.06 Mapping models to code
19.01.06 to 09.02.06 Miniproject: Asteroids
26.01.06 to 02.02.06 Testing

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 2
Exercise 1

Consider an ATM system. Identify at least three


different actors that interact with this system?

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 3
Exercise 2

Can the system under consideration be


represented as an actor?

Justify your answer.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 4
Exercise 3

What is the difference between a scenario and a


use case?

When do you use each construct?

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 5
Exercise 4
Draw a use case diagram for a ticket distributor for a train
system. The system includes two actors: a traveler, who
purchases different types of tickets, and a central
computer system, which maintains a reference database
for the tariff. Use cases should include:
BuyOneWayTicket, BuyWeeklyCard,
BuyMonthlyCard, UpdateTariff. Also include the
following exceptional cases: Time-Out (i.e., traveler took
too long to insert the right amount),
TransactionAborted (i.e., traveler selected the cancel
button without completing the transaction),
DistributorOutOfChange, and
DistributorOutOfPaper.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 6
Exercise 5

Write the flow of events and specify all fields for the
use case UpdateTariff that you drew in Exercise
4.

Do not forget to specify any relationships.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 7
Exercise 5: Use Case Template
Use case name UpdateTariff
Participating Actors ….
Flow of events 1. …
2. ….
3. …
:
:
:
Entry condition …..
Exit condition ….
Quality requirements ….

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 8
Exercise 5: Solution
Use case name UpdateTariff
Participating Actors Initiated by CentralComputerSystem
Flow of events 1. The CentralCo m puterSystem 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 CentralCo m puterSystem broadcasts the new database.
5. The ticket distributor system receives the new database
of tariff. Upon complete, the ticket distributor sends an
ack nowledgement to the CentralC omputerSystem
6. After ack nowledgment, the ticket distributor enables the
traveler interface and can issue tickets at the new tariff.
7. The CentralCo m puterSystem checks if all ticket distributors have
ack nowledged 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-
Software Engineeringof-order
Exercise 1
otherwise.
Lilith Al-Jadiri November 7, 2005 9
Exercise 6

Draw a class diagram representing a book defined


by the following statement:

“A book is composed of a number of parts, which in


turn are composed of a number of chapters.
Chapters are composed of sections.”

Focus only on classes and relationships.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 10
Exercise 7

Add multiplicity to the class diagram you produced


in Exercise 6.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 11
Exercise 6 & 7: Solution

Book
1
*
Part
1
*
Chapter
1
*
Section

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 12
Exercise 8

Draw an object diagram representing the first part


of the oose book (table of contents is on sheet 1).

Make sure that the object diagram you draw is


consistent with the class diagram of Exercise 6.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 13
Exercise 9

Extend the class diagram of Exercise 6 to include


the following attributes:
• book includes a publisher, publication date, and
an 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

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 14
Exercise 9: Solution

Book Part Chapter Section


publisher:Person title:String title:String title:String
publicationDate:Date number:Integer number:Integer number:Integer
ISBN:Integer[4] abstract:String

1 * 1 * 1 *

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 15
Exercise 10

Consider the class diagram of Exercise 9. Note that


the Part, Chapter, and Section classes all include
a title and a number attribute. Add an abstract
class and a generalization relationship to factor
out these two attributes into the abstract class.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 16
Exercise 10: Solution
NumberedComponent
title:String
number:Integer

Book Part Chapter Section


publisher:Person abstract:String
publicationDate:Date
ISBN:Integer[4]

1 * 1 * 1 *

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 17
Exercise 11

Draw a class diagram representing the relationship


between parents and children. Take into account
that a person can have both a parent and a child.
Annotate associations with roles and multiplicities.
Solution
child
Person
*
parent 2

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 18
Hand-out 2

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 19
Sequence Diagrams
• Used during requirements analysis
– To refine use case descriptions
TicketMachine
Passenger – to find additional objects
(“participating objects”)
selectZone()
• Used during system design
– to refine subsystem interfaces
• Used during Testing
insertCoins() – to specify expected behaviour and
validate output
• Classes are represented by
rectangles
pickupChange()
• Lifelines are represented by
dashed lines
• Messages are represented by
pickUpTicket() arrows
• Activations are represented by
narrow rectangles.
Software Engineering Exercise 1
Lilith Al-Jadiri November 7, 2005 20
Sequence Diagrams: Dataflow

ZoneButton TarifSchedule Display


Passenger

selectZone()
lookupPrice(selection)

price
displayPrice(price)
Dataflow
…continued on next slide...
• The source of an arrow indicates the activation which sent
the message
• Horizontal dashed arrows indicate data flow, for example
return results from a message

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 21
Sequence Diagrams: Iteration & Condition
…continued from previous slide...

ChangeProcessor CoinIdentifier Display CoinDrop


Passenger

*insertChange(coin) lookupCoin(coin)

price
Iteration displayPrice(owedAmount)

[owedAmount<0] returnChange(-owedAmount)
Condition
…continued on next slide...

• Iteration is denoted by a * preceding the message name


• Condition is denoted by boolean expression in [ ] before the
message name
Software Engineering Exercise 1
Lilith Al-Jadiri November 7, 2005 22
Creation and destruction
…continued from previous slide...

ChangeProcessor
Passenger
Creation
createTicket(selection)

Ticket
print()

free()
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.
Software Engineering Exercise 1
Lilith Al-Jadiri November 7, 2005 23
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 you
may need. Draw only the first five message
sends.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 24
Exercise 1 contd.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 25
Exercise 1: Solution

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 26
Exercise 2

Draw a sequence diagram for the ReportIncident use


case of Figure 2. Draw only the first five message
sends. Make sure it is consistent with the
sequence diagram of Exercise 1.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 27
Exercise 2 contd.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 28
Exercise 2: Solution

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 29
Activity Diagrams

• An activity diagram is a special case of a state


chart diagram
• The states are activities (“functions”)
• An activity diagram is useful to depict the
workflow in a system

Handle Document Archive


Incident Incident Incident

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 30
Activity Diagrams allow to model Decisions

[lowPriority]
Open Allocate
Incident Resources

[fire & highPriority]

[not fire & highPriority]


Notify
Fire Chief

Notify
Police Chief

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 31
Activity Diagrams allow to model Concurrency

• Synchronization of multiple activities


• Splitting the flow of control into multiple threads

Allocate
Splitting Resources Synchronization

Open Coordinate Archive


Incident Resources Incident

Document
Incident

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 32
Activity Diagrams: Grouping of Activities
• Activities may be grouped into swimlanes to
denote the object or subsystem that implements
the activities.

Allocate Dispatcher
Resources

Open Coordinate Archive


Incident Resources Incident

FieldOfficer
Document
Incident

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 33
Exercise 3

Consider the process of ordering a pizza over the


phone. Draw an activity diagram representing
each step of the process, from the moment you
pick up the phone to the point where you start
eating the pizza. Do not represent any
exceptions. Include activities that others need to
perform.

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 34
Exercise 3: Solution

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 35
Exercise 4

Add exception handling to the activity diagram you


developed in Exercise 3. Consider at least three
exceptions (e.g. delivery person wrote down
wrong address, deliver person brings wrong
pizza, store out of anchovies).

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 36
Exercise 4: Solution

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 37
Classes in a Class Diagram
• Class name only Example
Bank
Class Name Account

• With Details Example

Class Name Bank Account


attributes double balance
methods deposit()
withdraw()

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 38
Relationships
• Inheritance (arrow)
– example: between Secretary and Employee
• Composition/Aggregation (diamond)
– example: between Car and Wheel
• Association (line)
– example: between Borrower and Book

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 39
Inheritance

Employee

public class Secretary extends Employee {



}

Secretary

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 40
Composition/Aggregation

4
Car Wheel
w[]

public class Car {


Wheel w[];
...
public Car() {
w = new Wheel[4]; Note: [ ] in diagram
… is sometimes left out
} since w does not need
... to be an array
}

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 41
Association

1 3
Borrower Book
currBorr bk[]

public class Borrower { public class Book {


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

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 42
Notational Details
• Cardinality
– Specifies the number of objects that may participate in
the relationship
• Roles and Navigability
– Specifies relationship name and access
• Aggregation versus Composition
• Dependencies

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 43
Cardinality
• Also known as multiplicity
– Exact number (mandatory)
– Range (e.g., 0..5)
– * (many-valued)
• Specifies the number of objects that may be
associated with an object of the other class
• For associations, multiplicity is specified on both
participants

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 44
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

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 45
Uni-directional Navigability

FastFood pc PriceChecker
Counter
getPrice()

public class FastFoodCounter {


PriceChecker pc;
… public class PriceChecker {
public void add( … ) { // no access to counter
… }
double pr = pc.getPrice();

}

}

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 46
Bi-directional Navigability

1 3
Borrower Book
currBorr bk[]

public class Borrower { public class Book {


Book bk[]; Borrower currBorr;
… …
public Borrower() { }
bk = new Book[3];
}
Note: double arrowheads may
}
be omitted (bi-directional
navigability assumed)

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 47
Aggregation versus Composition
• Part-of relationships
• Aggregation
– Part may be independent of the whole but the whole
requires the part
– Unfilled diamond
• Composition (“stronger” form of aggregation)
– Part is created and destroyed with the whole
– Filled diamond
• Definitions and distinctions between aggregation
and composition still “under debate”

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 48
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

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 49
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

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 50
Composition/Aggregation Notations

MovieTheater BoxOffice
1 1
whole part
0..*
composition

0..* aggregation

Movie
part

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 51
Composition vs. Aggregation
• In real world there are 7 or 8 varieties of
whole/part relationships, but UML has only two
constructs – composition and aggregation
• It can be very difficult, and confusing to choose
one
• UML 2.0 has essentially deprecated aggregation
– So, don’t use it (choose composition or association)
– But you might see it

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 52
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

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 53
Dependency Example

Restaurant uses Parser

processOrders() getOrder()

public class Restaurant {



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

}

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 54
Association/Aggregation/Composition

Window
scrollbar[2]: Slider
title: Header
body: Panel

Window
1 1 1

scroll 2 title 1 body 1


Slider Header Panel

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 55
Mandatory Parts

4
Car Wheel
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
}

Software Engineering Exercise 1


Lilith Al-Jadiri November 7, 2005 56

Вам также может понравиться