Transcript CaseStudy_4

Optional Case Study - Chapter 4
Outline
4.1 Introduction
4.2 Class Operations
4.3 Creating Class Operations
4.4 Class Clock, Building
4.5 Class Scheduler
4.6 Class Person
4.7 Class Floor, FloorButton, ElevatorButton
4.8 Class Door
4.9 Sequence Diagrams
4.10 Conclusion
 2000 Deitel & Associates, Inc. All rights reserved.
4.1 Introduction
• Previous chapters
– Chapter 2 - identified classes
– Chapter 3 - determined many attributes of our classes
• This chapter
– Determine operations (behaviors)
• Chapter 5
– Focus on interactions between classes
 2000 Deitel & Associates, Inc. All rights reserved.
4.2 Class Operations
• Class operations
– Service class provides to clients
• Radio - setting station, volume
– Objects usually do not perform operations spontaneously
• Operations invoked by sending object (client object)
• Sends message to receiving object (server object)
• Requests object perform specific operation
• Deriving operations
– Examine problem statement for verbs and verb phrases
– Relate phrases to particular classes
 2000 Deitel & Associates, Inc. All rights reserved.
4.2 Class Operations (II)
Cla ss
Verb p hra ses
Elevator
moves, arrives at a floor, resets the elevator button, sounds
the elevator bell, signals its arrival to a floor, opens its
door, closes its door
Clock
ticks every second
Scheduler
randomly schedules times, creates a person, tells a person
to step onto a floor, verifies that a floor is unoccupied,
delays creating a person by one second
Person
steps onto floor, presses floor button, presses elevator
button, enters elevator, exits elevator
Floor
resets floor button, turns off light, turns on light
FloorButton
summons elevator
ElevatorButton signals elevator to move
Door
(opening of door) signals person to exit elevator, (opening
of door) signals person to enter elevator
Bell
none in problem statement
Light
none in problem statement
Building
none in problem statement
 2000 Deitel & Associates, Inc. All rights reserved.
4.3 Creating Class Operations
• Creating operations
– Examine verb phrase
"moves" verb is listed with Elevator
–
–
Should "moves" be an operation?
No message tells elevator to move
• Moves in response to a button, on condition that door is closed
• "Moves" should not be an operation
– "Arrives at floor" should not be an operation
• Elevator itself decides when to arrive, based on time
– "resets elevator button" - implies elevator sends message to
elevator button, telling it to reset
• ElevatorButton needs an operation to provide this service to the
elevator
 2000 Deitel & Associates, Inc. All rights reserved.
4.3 Creating Class Operations (II)
• Format
– Write operation name in bottom compartment in class diagram
– Write operations as function names, include return type,
parameters
resetButton() : void
• Function takes no parameters, returns nothing
• Other verbs
– Bell - provides service of ringing
– Floor - signal arrival of elevator
– Door - open and close
 2000 Deitel & Associates, Inc. All rights reserved.
4.3 Creating Class Operations (III)
Elevator
currentFloor : int = 1
direction : enum = up
capacity : int = 1
arrivalTime : int
moving : bool = false
processTime( time : int ) : void
personEnters( ) : void
personExits( ) : void
summonElevator( ) : void
prepareToLeave( ) : void
Scheduler
Clock
Door
time : int = 0
open : bool = false
getTime( ) : int
tick( ) : void
openDoor( ) : void
closeDoor( ) : void
Floor
Bell
occupied : bool = false
elevatorArrived( ) : void
isOccupied( ) : bool
<none yet>
ringBell( ) : void
FloorButton
floor1ArrivalTime : int
floor2ArrivalTime : int
pressed : bool = false
processTime( time : int ) : void
pressButton( ) : void
resetButton( ) : void
Light
on : bool = false
turnOff( ) : void
turnOn( ) : void
Person
ID : int
ElevatorButton
pressed : bool = false
stepOntoFloor( ) : void
exitElevator( ) : void
enterElevator( ) : void
 2000 Deitel & Associates, Inc. All rights reserved.
resetButton( ) : void
pressButton( ) : void
Building
<none yet>
runSimulation( ) : void
4.4 Class Clock, Building
• Class Clock
– Has phrase "ticks every second"
– "Getting the time" is an operation clock provides
• Is the ticking also an operation?
• Look at how simulation works
• Building, once per second, will
–
–
–
–
Get time from clock
Give time to scheduler
Give time to elevator
Building has full responsibility for running simulation, therefore it
must increment clock
– getTime and tick therefore operations of Clock
– processTime operation of Scheduler and Elevator
 2000 Deitel & Associates, Inc. All rights reserved.
4.5 Class Scheduler
• Class Scheduler
– "randomly schedules times", "delays creating a person by one
second"
– Scheduler performs these actions itself, does not provide service to
clients
– "create person" - special case
• A Person object cannot respond to a create message because it does
not yet exist
• Creation left to implementation details, not an operation of a class
• More Chapter 7
– "tells a person to step onto a floor" - class Person should have a
function that the scheduler can invoke
• stepOntoFloor - operation of class Person
 2000 Deitel & Associates, Inc. All rights reserved.
4.6 Class Person
• More verb phrases
– "verifies a floor is unoccupied" - class Floor needs a service to
let other objects know if a floor is occupied or not
• isOccupied returns true or false
• Class Person
– "presses floor button", "presses elevator button"
• Place operation pressButton under classes FloorButton and
ElevatorButton
– "enter elevator", "exit elevator" - suggests class Elevator needs
operations to correspond to these actions
• Discover what, if any, actions operations perform when concentrate
on implementation
 2000 Deitel & Associates, Inc. All rights reserved.
4.7 Class Floor, FloorButton, ElevatorButton
• Class Floor
– "resets floor button" - resetButton operation
– "turns off light", "turns on light" - turnOff and turnOn in class
Light
• Class FloorButton and ElevatorButton
– "summons elevator" - summonElevator operation in class
Elevator
– "signals elevator to move" - elevator needs to provide a "move"
service
• Before it can move, must close door
• prepareToLeave operation (performs necessary actions before
moving) in class Elevator
 2000 Deitel & Associates, Inc. All rights reserved.
4.8 Class Door
• Class Door
– Phrases imply door sends message to person to tell it to exit or
enter elevator
– exitElevator and enterElevator in class Person
• Notes
– Do not overly worry about parameters or return types
– Only want a basic understanding of each class
– As we continue design, number of operations may vary
• New operations needed
• Some current operations unnecessary
 2000 Deitel & Associates, Inc. All rights reserved.
4.9 Sequence Diagrams
• Sequence Diagram
– Model our "Simulation loop"
– Focuses on how messages are sent between objects over time
• Format
– Each object represented by a rectangle at top of diagram
• Name inside rectangle (objectName)
– Lifeline - dashed line, represents progression of time
• Actions occur along lifeline in chronological order, top to bottom
– Line with arrowhead - message between objects
•
•
•
•
Invokes corresponding operation in receiving object
Arrowhead points to lifeline of receiving object
Name of message above message line, includes parameters
Parameter name followed by colon and parameter type
 2000 Deitel & Associates, Inc. All rights reserved.
4.9 Sequence Diagrams (II)
: Building
{currentTime < totalTime}
: Clock
: Scheduler
tick( )
getTime( )
time
processTime( currentTime : int )
processTime( currentTime : int )
 2000 Deitel & Associates, Inc. All rights reserved.
: Elevator
4.9 Sequence Diagrams (III)
• Flow of control
– If object returns flow of control or returns a value, return message
(dashed line with arrowhead) goes back to original object
• Clock object returns time in response to getTime message
received from Building object
• Activations
– Rectangles along lifelines - represent duration of an activity
• Height corresponds to duration
• Timing constraint
– In our diagram, text to far left
– While currentTime < totalTime objects keep sending
messages as in the diagram
 2000 Deitel & Associates, Inc. All rights reserved.
building : Building
scheduler : Scheduler
processTime( time )
floor1 : Floor
floor2 : Floor
isOccupied( ) : bool
bool converge
The two lifelines
[occupied = true]
scheduler
second floor same way as the first
[occupied =handles
false]
When
returns control to building
createfinished,: Person
personArrives( )
building sends processTime message to
scheduler delayArrival( floor1 )
scheduleArrival( floor1 )
scheduler decides whether to create a new person
Can only create a new person if floor unoccupied
Checks by
sending
isOccupied
floor
floor1
returns
true
or false message toisOccupied(
) : bool
object
scheduler's lifeline splits - conditional executionbool
of activities.
Condition supplied for each lifeline.
After new
Person
[occupied
= false] object created, it steps on first floor
create
Person
If true - scheduler calls Person
delayArrival
object :sends
personArrives
message to
personArrives( )
[occupied = true]
Not an operation - not invokedfloor1
by another
object
object
If false - scheduler
creates
new Person
delayArrival( floor2
) scheduler
schedules new arrival for floor1
scheduleArrival(
floor2 )
object
calls its own scheduleArrival
function (not an
When new objects created, rectangle
corresponds to
operation)
time. Message "create" sent from one object to
another (arrowhead points to new object).
 2000 Deitel & Associates, Inc. All rights reserved.
4.10 Conclusion
• Discussed operations of classes
– Introduced sequence diagrams to illustrate operations
• Chapter 5
– Examine how objects interact with each other
 2000 Deitel & Associates, Inc. All rights reserved.