Download presentation
Presentation is loading. Please wait.
Published byAlaina Todd Modified over 9 years ago
1
2000 Deitel & Associates, Inc. All rights reserved. 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
2
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
3
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
4
2000 Deitel & Associates, Inc. All rights reserved. 4.2 Class Operations (II)
5
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
6
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
7
2000 Deitel & Associates, Inc. All rights reserved. 4.3 Creating Class Operations (III) open : bool = false Door openDoor( ) : void closeDoor( ) : void Bell ringBell( ) : void on : bool = false Light turnOff( ) : void turnOn( ) : void pressed : bool = false FloorButton pressButton( ) : void resetButton( ) : void time : int = 0 Clock getTime( ) : int tick( ) : void occupied : bool = false Floor elevatorArrived( ) : void isOccupied( ) : bool Person ID : int stepOntoFloor( ) : void exitElevator( ) : void enterElevator( ) : void pressed : bool = false ElevatorButton resetButton( ) : void pressButton( ) : void 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 floor1ArrivalTime : int floor2ArrivalTime : int processTime( time : int ) : void Building runSimulation( ) : void
8
2000 Deitel & Associates, Inc. All rights reserved. 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
9
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
10
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
11
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
12
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
13
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
14
2000 Deitel & Associates, Inc. All rights reserved. 4.9 Sequence Diagrams (II) : Building { currentTime < totalTime } tick( ) getTime( ) processTime( currentTime : int ) time : Scheduler: Clock: Elevator
15
2000 Deitel & Associates, Inc. All rights reserved. 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
16
2000 Deitel & Associates, Inc. All rights reserved. processTime( time ) bool isOccupied( ) : bool : Person scheduleArrival( floor1 ) personArrives( ) [ occupied = false ] create [ occupied = false ] create [ occupied = true ] bool isOccupied( ) : bool personArrives( ) scheduleArrival( floor2 ) building : Building scheduler : Scheduler floor1 : Floor floor2 : Floor : Person delayArrival( floor1 ) delayArrival( floor2 ) building sends processTime message to scheduler scheduler decides whether to create a new person Can only create a new person if floor unoccupied Checks by sending isOccupied message to floor object floor1 returns true or false scheduler 's lifeline splits - conditional execution of activities. Condition supplied for each lifeline. If true - scheduler calls delayArrival Not an operation - not invoked by another object If false - scheduler creates new Person object When new objects created, rectangle corresponds to time. Message "create" sent from one object to another (arrowhead points to new object). After new Person object created, it steps on first floor Person object sends personArrives message to floor1 object scheduler schedules new arrival for floor1 calls its own scheduleArrival function (not an operation) The two lifelines converge scheduler handles second floor same way as the first When finished, returns control to building
17
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.