26
1 Examining Execution Examining Execution Sequences Sequences Introducing Sequence Diagrams

1 Examining Execution Sequences Introducing Sequence Diagrams

Embed Size (px)

Citation preview

Page 1: 1 Examining Execution Sequences Introducing Sequence Diagrams

1

Examining Execution SequencesExamining Execution Sequences

Introducing Sequence Diagrams

Page 2: 1 Examining Execution Sequences Introducing Sequence Diagrams

2

Takes a real-worldreal-world view of objects and classes.

The purposepurpose of having an object with data

is so that it can do somethingdo something for us. So it has a ResponsibilityResponsibility to do what we need,

that is, to provide a ServiceService to us or some other object, possibly a User object.

Responsibility-Driven Design (RDD) Responsibility-Driven Design (RDD)

Page 3: 1 Examining Execution Sequences Introducing Sequence Diagrams

3

we can say there is a we can say there is a

ResponsibilityResponsibilityto provide that service when to provide that service when

requestedrequested

to those who need it.to those who need it.

Wherever a service exists,Wherever a service exists,

Page 4: 1 Examining Execution Sequences Introducing Sequence Diagrams

4

The The knowledgeknowledge that an object maintains that an object maintains

(i.e., the data it carries),(i.e., the data it carries),

And the And the actionsactions an object can perform an object can perform

(i.e., its behaviors.)(i.e., its behaviors.)””

Responsibilities include two key items:Responsibilities include two key items:

Page 5: 1 Examining Execution Sequences Introducing Sequence Diagrams

5

The actionsactions (behaviors) each imply

a Responsibility to do somethingdo something, , e.g.:

An Invoice Invoice has a responsibility to print print itself,itself, And to updateupdate its Total Total attribute, Calculate and printprint its TaxTax and Grand Total.Grand Total. A Line Item Line Item has a responsibility to calculatecalculate and

return its Extended Price,Extended Price, And to printprint itself. itself.

Page 6: 1 Examining Execution Sequences Introducing Sequence Diagrams

6

The Invoice Invoice has a responsibility to

ensure that the Account Balance is updated.

This responsibility it then delegates to the AccountAccount object.

The AccountAccount object has a responsibility to update its BalanceBalance attribute.

Page 7: 1 Examining Execution Sequences Introducing Sequence Diagrams

7

In this scenario,

The invoice has no responsibility no responsibility to advise the Customer when it has been printed,

But it must notify the UserUser if it cannotnot complete its assigned task.

In this sense we can say that there is an

Agreement, Agreement, or a Contract Contract between the two classes of objects.

Page 8: 1 Examining Execution Sequences Introducing Sequence Diagrams

8

When, say, a Customer object sends a message,

The chosen Invoice object will provide a provide a serviceservice by printing itself.

The “agreement” between two classes is such that:

Page 9: 1 Examining Execution Sequences Introducing Sequence Diagrams

9

“ “ A list of services A list of services

an instance of one classan instance of one class

can request from can request from

an instance of another.”an instance of another.”

Wirfs-Brock et al (1990) describe a contract as:

Page 10: 1 Examining Execution Sequences Introducing Sequence Diagrams

10

Where the client requests a service from the Server object

But sometimes the server needs help to do its But sometimes the server needs help to do its task . . .task . . .

Note this is a Client-Server relationship,

Page 11: 1 Examining Execution Sequences Introducing Sequence Diagrams

11

The Invoice was unable to do the job alone,

And had to ask for help From the Line Item Line Item and AccountAccount classes. It delegated the updatingupdating to the Account

class, And multiplying Quantity by Unit Price multiplying Quantity by Unit Price to the

Line Item class.

In the last scenario,

Page 12: 1 Examining Execution Sequences Introducing Sequence Diagrams

12

Requires data that the server doesn’t have,

Or tasks that the server doesn’t know how to do,

So the server calls on other classes for help.

Often a service needed by a client

Page 13: 1 Examining Execution Sequences Introducing Sequence Diagrams

13

Object#1

Object#2

Request for serviceRequest for service

ResponseResponse

ClientClient ServerServer

It starts like this:It starts like this:

Page 14: 1 Examining Execution Sequences Introducing Sequence Diagrams

14

Object#1

Object#2

Request for serviceRequest for service

ResponseResponse

ClientClient ServerServer

Then the server asks for help:Then the server asks for help:

Object#3

Request for assistanceRequest for assistance

Help responseHelp response

ServerServerClientClient

Page 15: 1 Examining Execution Sequences Introducing Sequence Diagrams

15

Note that Note that for this request, for this request,

the server is the server is now a client.now a client.

Note that Note that for this request, for this request,

the server is the server is now a client.now a client.

Object#1

Object#2

Object#3

Request for serviceRequest for service Request for assistanceRequest for assistance

Help responseHelp responseResponseResponse

ClientClient ServerServerServerServer ClientClient

Page 16: 1 Examining Execution Sequences Introducing Sequence Diagrams

16

A request from a client to a server,A request from a client to a server,

for a service that will help the client fulfill for a service that will help the client fulfill

one of its own responsibilities.one of its own responsibilities.

This we call a collaborationcollaboration

Page 17: 1 Examining Execution Sequences Introducing Sequence Diagrams

17

As Actors (users) walk through each Use Case

Note which class is being queried or updatedat each step of the procedure.

Then check if an appropriate responsibility has already been identified,

And if not then add it.

Use Case ScenariosUse Case Scenarios

Walk-through the system,

Page 18: 1 Examining Execution Sequences Introducing Sequence Diagrams

18

Does this class have everything it needs to do this?

If not, what is missing? What data is missing? What additional tasks are needed

before, during, or After the main task?

These questions must be asked about each These questions must be asked about each responsibility you have discovered:responsibility you have discovered:

Page 19: 1 Examining Execution Sequences Introducing Sequence Diagrams

19

Can any of them? If so, record the collaboration. If not, then

Add an attribute or responsibility, or Define a new class, or Browse available class libraries.

What classes in the model can supply these additional tasks?

Page 20: 1 Examining Execution Sequences Introducing Sequence Diagrams

20

Requires help from a contracting server,

Who needs a “subcontractor” who needs another one, and so on.

Sometimes a client responsibility

Page 21: 1 Examining Execution Sequences Introducing Sequence Diagrams

21

Documents these interactions,showing the pattern of calls among a

Society of ObjectsSociety of Objects

(i.e., a bunch of objects that regularly hold conversations – might

constitute a subsystem)

A Sequence Diagram:A Sequence Diagram:

Page 22: 1 Examining Execution Sequences Introducing Sequence Diagrams

22

Use a class nameclass name

Objects are shown along the top.

The colon ( :: ) comes afterafter the instance name and beforebefore the class name

:Window :Position

Or both.

Joe:

Or an instance nameinstance name

Steve:ManagerOld:Occupancy

Page 23: 1 Examining Execution Sequences Introducing Sequence Diagrams

23

The vertical axis is TimeTime

Steve:Manager:Window :Position Joe:Old:Occupancy

Shown as dashed lines, and reading down

tt

Each vertical Time axis is called a LifelineLifeline

Page 24: 1 Examining Execution Sequences Introducing Sequence Diagrams

24

We place a skinny box on each lifeline

Chapter 11: Responsibilities and CollaborationsChapter 11: Responsibilities and Collaborations 11.8. Sequence Diagrams11.8. Sequence Diagrams

Steve:Manager:Window :Position Joe:Old:Occupancy

This is called a focus of controlfocus of control

or an activation lineactivation line It shows what part of its life the object is active

tt

Page 25: 1 Examining Execution Sequences Introducing Sequence Diagrams

25

To begin handling a “Promotion”, a screen object queries a PositionPosition instance.

:Window

A dashed arrow indicates the response

Joe:Steve:Manager:Position

displayPosName(posNo)

Then the screen object sends a message to archive the old position occupancy data, with today’s date as parameter

Old:Occupancy

archiveOldOccupancy(date)

Page 26: 1 Examining Execution Sequences Introducing Sequence Diagrams

26

Joe:

success()

archiveOldOccupancy(date)

displayPosName(posNo)

Then we send a message to « create« create » » a new OccupancyOccupancy instance

:Window

Then a pair of unansweredunanswered messages to advise the Manager and Employee instances

Steve:ManagerOld:Occupancy:Position

New:Occupancy

« create »

advise()

advise()