2003 prentice hall, inc. all rights reserved. appendix e – elevator model outline e.1 introduction...
TRANSCRIPT
2003 Prentice Hall, Inc. All rights reserved.
Appendix E – Elevator Model
OutlineE.1 IntroductionE.2 Class ElevatorSimulationE.3 Classes Location and FloorE.4 Class Door and ElevatorDoor E.5 Class ButtonE.6 Class ElevatorShaftE.7 Classes Light and BellE.8 Class ElevatorE.9 Class PersonE.10 Artifacts RevisitedE.11 Conclusion
2003 Prentice Hall, Inc. All rights reserved.
E.2 Class ElevatorSimulation
• ElevatorSimulation– “Ties together” the objects that comprise the elevator
simulation model
– Sends events from MVC model to view
– Instantiates Person object (as per user request)
– Allows Floor to obtain reference to ElevatorShaft
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Line 12
Lines 16-20
Lines 23-28
1 // ElevatorSimulation.java2 // Elevator simulation model with ElevatorShaft and two Floors3 package com.deitel.jhtp5.elevator.model;4 5 // Java core packages6 import java.util.*;7 8 // Deitel packages9 import com.deitel.jhtp5.elevator.event.*;10 import com.deitel.jhtp5.elevator.ElevatorConstants;11 12 public class ElevatorSimulation implements ElevatorSimulationListener,13 ElevatorConstants {14 15 // declare two-Floor architecture in simulation16 private Floor firstFloor;17 private Floor secondFloor;18 19 // ElevatorShaft in simulation20 private ElevatorShaft elevatorShaft;21 22 // objects listening for events from ElevatorModel23 private Set personMoveListeners;24 private DoorListener doorListener;25 private ButtonListener buttonListener;26 private LightListener lightListener;27 private BellListener bellListener;28 private ElevatorMoveListener elevatorMoveListener;
ElevatorSimulation implements ElevatorSimulationListener, which
inherits from all listener interfaces
Use class diagram to determine associations with Floor and ElevatorShaft
Declare listeners that receive events from model (and will send these events to ElevatorView)
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Line 31
Lines 37-46
Lines 49-53
29 30 // cumulative number of people in simulation31 private int numberOfPeople = 0;32 33 // constructor instantiates ElevatorShaft and Floors34 public ElevatorSimulation()35 {36 // instantiate firstFloor and secondFloor objects37 firstFloor = new Floor( FIRST_FLOOR_NAME );38 secondFloor = new Floor( SECOND_FLOOR_NAME );39 40 // instantiate ElevatorShaft object41 elevatorShaft =42 new ElevatorShaft( firstFloor, secondFloor );43 44 // give elevatorShaft reference to first and second Floor45 firstFloor.setElevatorShaft( elevatorShaft );46 secondFloor.setElevatorShaft( elevatorShaft );47 48 // register for events from ElevatorShaft49 elevatorShaft.setDoorListener( this );50 elevatorShaft.setButtonListener( this );51 elevatorShaft.addElevatorMoveListener( this );52 elevatorShaft.setLightListener( this );53 elevatorShaft.setBellListener( this );54 55 // instantiate Set for ElevatorMoveListener objects56 personMoveListeners = new HashSet( 1 );57 58 } // end ElevatorModel constructor
Instantiate Floors and ElevatorShaft, then
assign the ElevatorShaft reference to each Floor
Register ElevatorSimulation for events from ElevatorShaft
Use class diagram to determine attributes
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Lines 75-91
Lines 78-86
59 60 // return Floor with given name61 private Floor getFloor( String name )62 {63 if ( name.equals( FIRST_FLOOR_NAME ) )64 return firstFloor;65 else66 67 if ( name.equals( SECOND_FLOOR_NAME ) )68 return secondFloor;69 else70 return null;71 72 } // end method getFloor73 74 // add Person to Elevator Simulator75 public void addPerson( String floorName )76 {77 // instantiate new Person and place on Floor78 Person person = 79 new Person( numberOfPeople, getFloor( floorName ) );80 person.setName( Integer.toString( numberOfPeople ) );81 82 // register listener for Person events83 person.setPersonMoveListener( this );84 85 // start Person thread86 person.start();
Method for adding Person to simulation model
Instantiate Person, register ElevatorModel to receive events from that Person and
start Person’s thread
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Lines 94-97
Lines 100-103
Lines 107-153
87 88 // increment number of Person objects in simulation89 numberOfPeople++;90 91 } // end method addPerson92 93 // invoked when Elevator has departed from Floor94 public void elevatorDeparted( ElevatorMoveEvent moveEvent )95 {96 elevatorMoveListener.elevatorDeparted( moveEvent );97 }98 99 // invoked when Elevator has arrived at destination Floor100 public void elevatorArrived( ElevatorMoveEvent moveEvent )101 {102 elevatorMoveListener.elevatorArrived( moveEvent );103 }104 105 // send PersonMoveEvent to listener, depending on event type106 private void sendPersonMoveEvent( 107 int eventType, PersonMoveEvent event )108 {109 Iterator iterator = personMoveListeners.iterator();110 111 while ( iterator.hasNext() ) {112 113 PersonMoveListener listener = 114 ( PersonMoveListener ) iterator.next();
When Elevator has arrived at Floor, notify
listener (ElevatorView)
When Person performs some action (event) in model, determine which event was sent,
then forward the event to any listeners
When Elevator has departed from Floor, notify
listener (i.e., Elevator-View, in this simulation)
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Lines 108-153
115 116 // send Event to this listener, depending on eventType117 switch ( eventType ) {118 119 // Person has been created120 case Person.PERSON_CREATED:121 listener.personCreated( event );122 break;123 124 // Person arrived at Elevator125 case Person.PERSON_ARRIVED:126 listener.personArrived( event );127 break;128 129 // Person entered Elevator130 case Person.PERSON_ENTERING_ELEVATOR:131 listener.personEntered( event );132 break;133 134 // Person pressed Button object135 case Person.PERSON_PRESSING_BUTTON:136 listener.personPressedButton( event );137 break;138 139 // Person exited Elevator140 case Person.PERSON_EXITING_ELEVATOR:141 listener.personDeparted( event );142 break;
When Person performs some action (event) in model,
determine which event was sent, then forward the event to
any listeners
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Lines 156-159
Lines 162-165
Lines 168-171
143 144 // Person exited simulation145 case Person.PERSON_EXITED:146 listener.personExited( event );147 break;148 149 default:150 break;151 }152 }153 } // end method sendPersonMoveEvent154 155 // invoked when Person has been created in model156 public void personCreated( PersonMoveEvent moveEvent )157 {158 sendPersonMoveEvent( Person.PERSON_CREATED, moveEvent );159 }160 161 // invoked when Person has arrived at Floor's Button162 public void personArrived( PersonMoveEvent moveEvent )163 {164 sendPersonMoveEvent( Person.PERSON_ARRIVED, moveEvent );165 }166 167 // invoked when Person has pressed Button168 public void personPressedButton( PersonMoveEvent moveEvent )169 {170 sendPersonMoveEvent( Person.PERSON_PRESSING_BUTTON, 171 moveEvent );172 }
When Person has pressed Button, notify listeners
When Person has been created, notify listeners
When Person has arrived at Elevator,
notify listeners
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Lines 175-179
Lines 182-186
Lines 189-192
Lines 195-198
173 174 // invoked when Person has entered Elevator175 public void personEntered( PersonMoveEvent moveEvent )176 {177 sendPersonMoveEvent( Person.PERSON_ENTERING_ELEVATOR, 178 moveEvent );179 }180 181 // invoked when Person has departed from Elevator182 public void personDeparted( PersonMoveEvent moveEvent )183 {184 sendPersonMoveEvent( Person.PERSON_EXITING_ELEVATOR,185 moveEvent );186 }187 188 // invoked when Person has exited Simulation189 public void personExited( PersonMoveEvent moveEvent )190 {191 sendPersonMoveEvent( Person.PERSON_EXITED, moveEvent );192 }193 194 // invoked when Door has opened195 public void doorOpened( DoorEvent doorEvent )196 {197 doorListener.doorOpened( doorEvent );198 }199
When Person has entered Elevator, notify listeners
When Person has left Elevator, notify listeners
When Person has exited simulation, notify listeners
When Door has opened, notify listeners
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Lines 201-204
Lines 207-210
Lines 213-216
Lines 219-222
Lines 225-228
200 // invoked when Door has closed201 public void doorClosed( DoorEvent doorEvent )202 {203 doorListener.doorClosed( doorEvent );204 }205 206 // invoked when Button has been pressed207 public void buttonPressed( ButtonEvent buttonEvent )208 {209 buttonListener.buttonPressed( buttonEvent );210 }211 212 // invoked when Button has been reset213 public void buttonReset( ButtonEvent buttonEvent )214 {215 buttonListener.buttonReset( buttonEvent );216 }217 218 // invoked when Bell has rung219 public void bellRang( BellEvent bellEvent )220 {221 bellListener.bellRang( bellEvent );222 }223 224 // invoked when Light has turned on225 public void lightTurnedOn( LightEvent lightEvent )226 {227 lightListener.lightTurnedOn( lightEvent );228 }
When Door has closed, notify listeners
When Button has been pressed, notify listeners
When Button has been reset, notify listeners
When Bell has rung, notify listeners
When Light has been turned on, notify listeners
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Lines 231-234
Lines 237-247
Lines 250-254
229 230 // invoked when Light has turned off231 public void lightTurnedOff( LightEvent lightEvent )232 {233 lightListener.lightTurnedOff( lightEvent );234 }235 236 // set listener for ElevatorModelListener237 public void setElevatorSimulationListener( 238 ElevatorSimulationListener listener )239 {240 // ElevatorModelListener extends all interfaces below241 addPersonMoveListener( listener );242 setElevatorMoveListener( listener );243 setDoorListener( listener );244 setButtonListener( listener );245 setLightListener( listener );246 setBellListener( listener );247 }248 249 // set listener for PersonMoveEvents250 public void addPersonMoveListener(251 PersonMoveListener listener )252 {253 personMoveListeners.add( listener );254 }255
Allow PersonListener to listen for PersonMoveEvents from model
Allow ElevatorSimulationListener to listen for all events from model
When Light has been turned off, notify listeners
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorSimulation.javaClass ElevatorSimulation represents the MVC model in our elevator simulation.
Lines 257-260
Lines 263-266
Lines 269-273
Lines 276-279
Lines 282-285
256 // set listener for DoorEvents257 public void setDoorListener( DoorListener listener )258 {259 doorListener = listener;260 }261 262 // set listener for ButtonEvents263 public void setButtonListener( ButtonListener listener )264 {265 buttonListener = listener;266 }267 268 // add listener for ElevatorMoveEvents269 public void setElevatorMoveListener(270 ElevatorMoveListener listener )271 {272 elevatorMoveListener = listener;273 }274 275 // set listener for LightEvents276 public void setLightListener( LightListener listener )277 {278 lightListener = listener;279 }280 281 // set listener for BellEvents282 public void setBellListener( BellListener listener )283 {284 bellListener = listener;285 }286 }
Allow ElevatorMoveListener to listen for ElevatorMoveEvents from model
Allow LightListener to listen for LightEvents from model
Allow BellListener to listen for BellEvents from model
Allow DoorListener to listen for DoorEvents from model
Allow ButtonListener to listen for ButtonEvents from model
2003 Prentice Hall, Inc. All rights reserved.
Fig. E.2 Class diagram showing realizations in the elevator model
(Part 1).
ElevatorSimulation
ElevatorSimulationListener
Elevator
ButtonListener DoorListener BellListener
ElevatorShaft
ButtonListener DoorListenerLightListenerElevatorMove-ListenerBellListener
Light BellElevatorDoor Button
ElevatorMoveListener
2003 Prentice Hall, Inc. All rights reserved.
Fig. E.3 Class diagram showing realizations in the elevator model
(Part 2).
ElevatorSimulation
Button-Listener
Door-Listener
ElevatorMove-Listener
PersonMove-Listener
Bell-Listener
Light-Listener
ElevatorSimulationListener
2003 Prentice Hall, Inc. All rights reserved.
Fig. E.4 Classes and implemented listener interfaces from Fig. E.2.
Class implements Listener ElevatorSimulation ElevatorSimulationListener ElevatorSimulationListener PersonMoveListener
ElevatorMoveListener ButtonListener DoorListener BellListener LightListener
ElevatorDoor, Light, Bell, Button
ElevatorMoveListener
Elevator ButtonListener DoorListener BellListener
ElevatorShaft LightListener ButtonListener DoorListener BellListener ElevatorMoveListener
Person DoorListener Fig. E.4 Classes and implemented listener interfaces from Fig. E.2.
2003 Prentice Hall, Inc. All rights reserved.
E.3 Classes Location and Floor
• Location– Represents location in the simulation
• Person has reference to Location– We can know each Person’s whereabouts
– Abstract superclass
– Subclasses: Floor and Elevator• Floor represents first or second floor in model
• (Elevator is discussed later)
2003 Prentice Hall, Inc.All rights reserved.
Outline
Location.java Location superclass that represents a location in the simulation.
Line 11
Lines 26-29
1 // Location.java2 // Abstract superclass representing location in simulation3 package com.deitel.jhtp5.elevator.model;4
5 // Deitel packages6 import com.deitel.jhtp5.elevator.event.*;7
8 public abstract class Location {9
10 // name of Location11 private String locationName;12
13 // set name of Location14 protected void setLocationName( String name )15 {16 locationName = name;17 }18
19 // return name of Location20 public String getLocationName()21 {22 return locationName;23 }24
25 // return Button at Location26 public abstract Button getButton();27
28 // return Door object at Location29 public abstract Door getDoor();30 }
Name of Location can equal “elevator,” “first floor” or “second floor”
Classes Floor and Elevator implement these abstract methods to
return appropriate objects
2003 Prentice Hall, Inc.All rights reserved.
Outline
Floor.javaClass Floor—a subclass of Location—represents a Floor across which a Person walks to the Elevator.
1 // Floor.java2 // Represents a Floor located next to an ElevatorShaft3 package com.deitel.jhtp5.elevator.model;4 5 // Deitel packages6 import com.deitel.jhtp5.elevator.ElevatorConstants;7 8 public class Floor extends Location 9 implements ElevatorConstants {10 11 // reference to ElevatorShaft object12 private ElevatorShaft elevatorShaft;13 14 // Floor constructor sets name of Floor15 public Floor( String name )16 {17 setLocationName( name );18 }19
2003 Prentice Hall, Inc.All rights reserved.
Outline
Floor.javaClass Floor—a subclass of Location—represents a Floor across which a Person walks to the Elevator.
Lines 21-33
Lines 36-48
20 // get first or second Floor Button, using Location name21 public Button getButton()22 {23 if ( getLocationName().equals( FIRST_FLOOR_NAME ) )24 return getElevatorShaft().getFirstFloorButton();25 else26
27 if ( getLocationName().equals( SECOND_FLOOR_NAME ) )28 return getElevatorShaft().getSecondFloorButton();29 else30
31 return null;32
33 } // end method getButton34
35 // get first or second Floor Door, using Location name36 public Door getDoor()37 {38 if ( getLocationName().equals( FIRST_FLOOR_NAME ) )39 return getElevatorShaft().getFirstFloorDoor();40 else 41
42 if ( getLocationName().equals( SECOND_FLOOR_NAME ) )43 return getElevatorShaft().getSecondFloorDoor();44 else45
46 return null;47
48 } // end method getDoor
Implement Location’s abstract method getButton to return Button
on either first or second Floor
Implement Location’s abstract method getDoor to return
Door on either first or second Floor
2003 Prentice Hall, Inc.All rights reserved.
Outline
Floor.javaClass Floor—a subclass of Location—represents a Floor across which a Person walks to the Elevator.
49 50 // get ElevatorShaft reference51 public ElevatorShaft getElevatorShaft()52 {53 return elevatorShaft;54 }55 56 // set ElevatorShaft reference57 public void setElevatorShaft( ElevatorShaft shaft )58 {59 elevatorShaft = shaft;60 }61 }
2003 Prentice Hall, Inc. All rights reserved.
E.4 Class Door and ElevatorDoor
• Door– Signals Person when to enter and exit Elevator– Subclass ElevatorDoor
2003 Prentice Hall, Inc.All rights reserved.
Outline
Door.javaClass Door, which represents a Door in the model, informs listeners when a Door has opened or closed.
Line 11
Lines 20 and 28
1 // Door.java2 // Sends DoorEvents to DoorListeners when opened or closed3 package com.deitel.jhtp5.elevator.model;4 5 // Java core packages6 import java.util.*;7 8 // Deitel packages9 import com.deitel.jhtp5.elevator.event.*;10 11 public class Door {12 13 // represent whether Door is open or closed14 private boolean open = false;15 16 // time before Door closes automatically17 public static final int AUTOMATIC_CLOSE_DELAY = 3000;18 19 // Set of DoorListeners20 private Set doorListeners;21 22 // location where Door opened or closed23 private Location doorLocation;24 25 // Door constructor instantiates Set for DoorListeners26 public Door()27 {28 doorListeners = new HashSet( 1 );29 }
Door listens for ElevatorMoveEvents; when Elevator arrives, Door opens
HashSet stores listener for DoorEvents (i.e., when Door opens and closes)
2003 Prentice Hall, Inc.All rights reserved.
Outline
Door.javaClass Door, which represents a Door in the model, informs listeners when a Door has opened or closed. Lines 32-49
Line 56
30 31 // add Door listener32 public void addDoorListener( DoorListener listener )33 {34 // prevent other objects from modifying doorListeners35 synchronized( doorListeners )36 {37 doorListeners.add( listener );38 }39 }40 41 // remove Door listener42 public void removeDoorListener( DoorListener listener )43 {44 // prevent other objects from modifying doorListeners45 synchronized( doorListeners )46 {47 doorListeners.remove( listener );48 }49 }50 51 // open Door and send all listeners DoorEvent objects52 public synchronized void openDoor( Location location )53 {54 if ( !open ) {55 56 open = true;57
Allow DoorListeners to register and unregister to receive DoorEvents
Open Door if it is closed
2003 Prentice Hall, Inc.All rights reserved.
Outline
Door.javaClass Door, which represents a Door in the model, informs listeners when a Door has opened or closed.
Lines 66-73
Lines 78-95
58 // obtain iterator from Set59 Iterator iterator;60 synchronized( doorListeners )61 {62 iterator = new HashSet( doorListeners ).iterator();63 }64 65 // get next DoorListener66 while ( iterator.hasNext() ) {67 DoorListener doorListener = 68 ( DoorListener ) iterator.next();69 70 // send doorOpened event to this DoorListener71 doorListener.doorOpened(72 new DoorEvent( this, location ) );73 }74 75 doorLocation = location;76 77 // declare Thread that ensures automatic Door closing78 Thread closeThread = new Thread( 79 new Runnable() {80 81 public void run()82 {83 // close Door if open for more than 3 seconds84 try {85 Thread.sleep( AUTOMATIC_CLOSE_DELAY );86 closeDoor( doorLocation );87 }
Notify DoorListeners that Door has opened
Use Thread to enable Door to close itself automatically
after three seconds
2003 Prentice Hall, Inc.All rights reserved.
Outline
Door.javaClass Door, which represents a Door in the model, informs listeners when a Door has opened or closed.
Line 106
88 89 // handle exception if interrupted90 catch ( InterruptedException exception ) {91 exception.printStackTrace();92 } 93 }94 } // end anonymous inner class95 );96 97 closeThread.start();98 }99 100 // notify all waiting threads that the door has opened101 notifyAll();102 103 } // end method openDoor104 105 // close Door and send all listeners DoorEvent objects106 public synchronized void closeDoor( Location location )107 { 108 if ( open ) {109 110 open = false;111 112 // obtain iterator from Set113 Iterator iterator;114 synchronized( doorListeners )115 {116 iterator = new HashSet( doorListeners ).iterator();117 }
Close Door if it is open
2003 Prentice Hall, Inc.All rights reserved.
Outline
Door.javaClass Door, which represents a Door in the model, informs listeners when a Door has opened or closed.
Lines 125-126
Lines 133-136
118 119 // get next DoorListener120 while ( iterator.hasNext() ) {121 DoorListener doorListener = 122 ( DoorListener ) iterator.next();123 124 // send doorClosed event to this DoorListener125 doorListener.doorClosed(126 new DoorEvent( this, location ) );127 }128 }129 130 } // end method closeDoor131 132 // return whether Door is open or closed133 public synchronized boolean isDoorOpen()134 {135 return open;136 }137 }
When Elevator arrives, open Door
Notify DoorListeners that Door has closed
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorDoor.javaClass ElevatorDoor opens in response to elevatorArrived messages and opens and closes the Floor Doors.
Line 11
Lines 14-20
1 // ElevatorDoor.java2 // Opens and closes floor Door when elevator arrives and departs.3 package com.deitel.jhtp5.elevator.model;4 5 // Java core packages6 import java.util.*;7 8 // Deitel packages9 import com.deitel.jhtp5.elevator.event.*;10 11 public class ElevatorDoor extends Door implements ElevatorMoveListener {12 13 // open ElevatorDoor and corresponding Floor Door14 public synchronized void openDoor( Location location )15 { 16 location.getDoor().openDoor( location );17 18 super.openDoor( location );19 20 } // end method openDoor21
ElevatorDoor extends Door and implements ElevatorMoveListener
Override method openDoor
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorDoor.javaClass ElevatorDoor opens in response to elevatorArrived messages and opens and closes the Floor Doors.
Lines 23-29
22 // close ElevatorDoor and Corresponding Floor Door23 public synchronized void closeDoor( Location location )24 { 25 location.getDoor().closeDoor( location );26 27 super.closeDoor( location );28 29 } // end method closeDoor30 31 // invoked when Elevator has departed32 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {}33 34 // invoked when Elevator has arrived35 public void elevatorArrived( ElevatorMoveEvent moveEvent )36 {37 openDoor( moveEvent.getLocation() );38 }39 }
Override method closeDoor
2003 Prentice Hall, Inc. All rights reserved.
E.5 Class Button
• Button– Signals Elevator to move between Floors
2003 Prentice Hall, Inc.All rights reserved.
Outline
Button.javaClass Button, which represents a Button in the model, informs listeners when a Button has been pressed or reset.
Line 8
Lines 11 and 17-20
Lines 23-29
1 // Button.java2 // Sends ButtonEvents to ButtonListeners when accessed3 package com.deitel.jhtp5.elevator.model;4 5 // Deitel packages6 import com.deitel.jhtp5.elevator.event.*;7 8 public class Button implements ElevatorMoveListener {9 10 // ButtonListener11 private ButtonListener buttonListener = null;12 13 // represent whether Button is pressed14 private boolean pressed = false;15 16 // set listener17 public void setButtonListener( ButtonListener listener )18 {19 buttonListener = listener;20 }21 22 // press Button and send ButtonEvent23 public void pressButton( Location location )24 {25 pressed = true;26 27 buttonListener.buttonPressed( 28 new ButtonEvent( this, location ) );29 }
Button listens for ElevatorMoveEvents; when Elevator arrives, Button resets
Allow ButtonListener to listen for ButtonEvents
Press Button and notify ButtonListener that Button has been pressed
2003 Prentice Hall, Inc.All rights reserved.
Outline
Button.javaClass Button, which represents a Button in the model, informs listeners when a Button has been pressed or reset.
Lines 32-38
Lines 50-53
30 31 // reset Button and send ButtonEvent32 public void resetButton( Location location )33 {34 pressed = false;35 36 buttonListener.buttonReset( 37 new ButtonEvent( this, location ) );38 }39 40 // return whether button is pressed41 public boolean isButtonPressed()42 {43 return pressed;44 }45 46 // invoked when Elevator has departed47 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {}48 49 // invoked when Elevator has arrived50 public void elevatorArrived( ElevatorMoveEvent moveEvent )51 {52 resetButton( moveEvent.getLocation() );53 }54 }
Reset Button and notify ButtonListener that Button has been reset
When Elevator arrives, reset Button
2003 Prentice Hall, Inc. All rights reserved.
E.6 Class ElevatorShaft
• ElevatorShaft– Represents elevator shaft in which Elevator travels
– Receives events from Elevator– “Bubbles up” events to ElevatorModel
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 11-12
Lines 18-27
1 // ElevatorShaft.java2 // Represents elevator shaft, which contains elevator3 package com.deitel.jhtp5.elevator.model;4 5 // Java core packages6 import java.util.*;7 8 // Deitel packages9 import com.deitel.jhtp5.elevator.event.*;10 11 public class ElevatorShaft implements ElevatorMoveListener,12 LightListener, BellListener {13 14 // Elevator15 private Elevator elevator;16 17 // Buttons on Floors18 private Button firstFloorButton;19 private Button secondFloorButton;20 21 // Doors on Floors22 private Door firstFloorDoor;23 private Door secondFloorDoor;24 25 // Lights on Floors26 private Light firstFloorLight;27 private Light secondFloorLight;28
ElevatorShaft listens for ElevatorMoveEvents
LightEvents and BellEvents
Use class diagram to determine associations of ElevatorShaft
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 30-34
Lines 43-70
Lines 47-53
29 // listeners30 private DoorListener doorListener;31 private ButtonListener buttonListener;32 private LightListener lightListener;33 private BellListener bellListener;34 private Set elevatorMoveListeners;35 36 // constructor initializes aggregated components37 public ElevatorShaft( Floor firstFloor, Floor secondFloor )38 {39 // instantiate Set for ElevatorMoveListeners40 elevatorMoveListeners = new HashSet( 1 );41 42 // anonymous inner class listens for ButtonEvents43 ButtonListener floorButtonListener = 44 new ButtonListener() {45 46 // called when Floor Button has been pressed47 public void buttonPressed( ButtonEvent buttonEvent )48 {49 // request elevator move to location50 Location location = buttonEvent.getLocation();51 buttonListener.buttonPressed( buttonEvent );52 elevator.requestElevator( location );53 }54
Instantiate anonymous inner class to listen for ButtonEvents from
Buttons on floors
When Button on floor is pressed, request Elevator to
move to Floor of request
Declare listeners that receive events from model (and will send these events to ElevatorModel)
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 56-59
Lines 77-100
Lines 80-91
55 // called when Floor Button has been reset56 public void buttonReset( ButtonEvent buttonEvent ) 57 {58 buttonListener.buttonReset( buttonEvent );59 }60 }; // end anonymous inner class61 62 // instantiate Floor Buttons63 firstFloorButton = new Button();64 secondFloorButton = new Button();65 66 // register anonymous ButtonListener with Floor Buttons67 firstFloorButton.setButtonListener( 68 floorButtonListener );69 secondFloorButton.setButtonListener( 70 floorButtonListener );71 72 // Floor Buttons listen for ElevatorMoveEvents73 addElevatorMoveListener( firstFloorButton );74 addElevatorMoveListener( secondFloorButton );75 76 // anonymous inner class listens for DoorEvents77 DoorListener floorDoorListener = new DoorListener() {78 79 // called when Floor Door has opened80 public void doorOpened( DoorEvent doorEvent )81 {82 // forward event to doorListener83 doorListener.doorOpened( doorEvent );84 }
Instantiate anonymous inner class to listen for DoorEvents
from Doors on floors
When Door on floor is opened or closed, forward DoorEvent to
DoorListener
When Button on floor is reset, reset Button
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 103-105
Lines 112-115
85 86 // called when Floor Door has closed87 public void doorClosed( DoorEvent doorEvent )88 {89 // forward event to doorListener90 doorListener.doorClosed( doorEvent );91 }92 }; // end anonymous inner class93 94 // instantiate Floor Doors95 firstFloorDoor = new Door();96 secondFloorDoor = new Door();97 98 // register anonymous DoorListener with Floor Doors99 firstFloorDoor.addDoorListener( floorDoorListener );100 secondFloorDoor.addDoorListener( floorDoorListener );101 102 // instantiate Lights, then listen for LightEvents103 firstFloorLight = new Light();104 addElevatorMoveListener( firstFloorLight );105 firstFloorLight.setLightListener( this );106 107 secondFloorLight = new Light();108 addElevatorMoveListener( secondFloorLight );109 secondFloorLight.setLightListener( this );110 111 // instantiate Elevator object112 elevator = new Elevator( firstFloor, secondFloor );113
Instantiate Lights and listen for LightEvents
Instantiate Elevator and listen for
ElevatorMoveEvents
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Line 118
Lines 122-140
Lines 126-138
114 // register for ElevatorMoveEvents from elevator115 elevator.addElevatorMoveListener( this );116
117 // listen for BellEvents from elevator118 elevator.setBellListener( this );119
120 // anonymous inner class listens for ButtonEvents from121 // elevator122 elevator.setButtonListener( 123 new ButtonListener() {124
125 // invoked when button has been pressed126 public void buttonPressed( ButtonEvent buttonEvent )127 {128 // send event to listener129 buttonListener.buttonPressed( buttonEvent );130 }131
132 // invoked when button has been reset133 public void buttonReset( ButtonEvent buttonEvent )134 {135 // send event to listener136 buttonListener.buttonReset( 137 new ButtonEvent( this, elevator ) );138 }139 } // end anonymous inner class140 );141
Instantiate anonymous inner class to listen for ButtonEvents from Elevator’s Button
When Elevator’s Button is pressed or
reset, forward ButtonEvent to ButtonListener
Listen for BellEvents from elevator
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 144-161
Lines 148-159
Line 164
Lines 169-172
142 // anonymous inner class listens for DoorEvents from143 // elevator144 elevator.setDoorListener( 145 new DoorListener() {146 147 // invoked when door has opened148 public void doorOpened( DoorEvent doorEvent )149 {150 // send event to listener151 doorListener.doorOpened( doorEvent );152 }153 154 // invoked when door has closed155 public void doorClosed( DoorEvent doorEvent )156 {157 // send event to listener158 doorListener.doorClosed( doorEvent );159 }160 } // end anonymous inner class161 );162 163 // start Elevator Thread164 elevator.start();165 166 } // end ElevatorShaft constructor167 168 // get Elevator169 public Elevator getElevator()170 {171 return elevator;172 }
Instantiate anonymous inner class to listen for DoorEvents from Elevator’s Door
When Elevator’s Door is pressed or
reset, forward DoorEvent to DoorListener
Start Elevator’s Thread
Get method for Elevator
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 175-208
173 174 // get Door on first Floor175 public Door getFirstFloorDoor()176 {177 return firstFloorDoor;178 }179 180 // get Door on second Floor181 public Door getSecondFloorDoor()182 {183 return secondFloorDoor;184 }185 186 // get Button on first Floor187 public Button getFirstFloorButton()188 {189 return firstFloorButton;190 }191 192 // get Button on second Floor193 public Button getSecondFloorButton()194 {195 return secondFloorButton;196 }197 198 // get Light on first Floor199 public Light getFirstFloorLight()200 {201 return firstFloorLight;202 }
Get methods for Doors, Buttons and Lights
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 211-214
Lines 217-226
Lines 229-243
203 204 // get Light on second Floor205 public Light getSecondFloorLight()206 {207 return secondFloorLight;208 }209 210 // invoked when Bell rings211 public void bellRang( BellEvent bellEvent )212 {213 bellListener.bellRang( bellEvent );214 }215 216 // invoked when Light turns on217 public void lightTurnedOn( LightEvent lightEvent )218 {219 lightListener.lightTurnedOn( lightEvent );220 }221 222 // invoked when Light turns off223 public void lightTurnedOff( LightEvent lightEvent )224 {225 lightListener.lightTurnedOff( lightEvent );226 }227 228 // invoked when Elevator departs229 public void elevatorDeparted( ElevatorMoveEvent moveEvent )230 {231 Iterator iterator = elevatorMoveListeners.iterator();232
When Bell has rung, forward BellEvent to BellListener
When Lights turn on or off, forward LightEvent to LightListener
When Elevator has departed, notify ElevatorMoveListeners
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 246-262
233 // iterate Set of ElevatorMoveEvent listeners234 while ( iterator.hasNext() ) {235 236 // get respective ElevatorMoveListener from Set237 ElevatorMoveListener listener = 238 ( ElevatorMoveListener ) iterator.next();239 240 // send ElevatorMoveEvent to this listener241 listener.elevatorDeparted( moveEvent );242 }243 } // end method elevatorDeparted244 245 // invoked when Elevator arrives246 public void elevatorArrived( ElevatorMoveEvent moveEvent )247 {248 // obtain iterator from Set249 Iterator iterator = elevatorMoveListeners.iterator();250 251 // get next DoorListener252 while ( iterator.hasNext() ) {253 254 // get next ElevatorMoveListener from Set255 ElevatorMoveListener listener = 256 ( ElevatorMoveListener ) iterator.next();257 258 // send ElevatorMoveEvent to this listener259 listener.elevatorArrived( moveEvent );260 261 } // end while loop262 } // end method elevatorArrived
When Elevator has arrived, notify ElevatorMoveListeners
2003 Prentice Hall, Inc.All rights reserved.
Outline
ElevatorShaft.java Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulation.
Lines 265-268
Lines 271-274
Lines 277-281
Lines 284-287
Lines 290-293
263 264 // set listener to DoorEvents265 public void setDoorListener( DoorListener listener )266 {267 doorListener = listener;268 }269 270 // set listener to ButtonEvents271 public void setButtonListener( ButtonListener listener )272 {273 buttonListener = listener;274 }275 276 // add listener to ElevatorMoveEvents277 public void addElevatorMoveListener( 278 ElevatorMoveListener listener )279 {280 elevatorMoveListeners.add( listener );281 }282 283 // set listener to LightEvents284 public void setLightListener( LightListener listener )285 {286 lightListener = listener;287 }288 289 // set listener to BellEvents290 public void setBellListener( BellListener listener )291 {292 bellListener = listener;293 }294 }
Enable ElevatorMoveListeners to receive ElevatorMoveEvents
from ElevatorShaft
Enable LightListener to receive LightEvents from ElevatorShaft
Enable BellListener to receive BellEvents from ElevatorShaft
Enable DoorListener to receive DoorEvents from ElevatorShaft
Enable ButtonListener to receive ButtonEvents from ElevatorShaft
2003 Prentice Hall, Inc. All rights reserved.
E.7 Classes Light and Bell
• Light and Bell– Help decorate the view
• Send events to ElevatorView via ElevatorShaft and ElevatorSimulation
2003 Prentice Hall, Inc.All rights reserved.
Outline
Light.javaClass Light represents a Light on the Floor in the model.
Line 14
Lines 23-26
1 // Light.java2 // Light turns a light on or off3 package com.deitel.jhtp5.elevator.model;4 5 // Deitel packages6 import com.deitel.jhtp5.elevator.event.*;7 8 public class Light implements ElevatorMoveListener {9 10 // Light state (on/off)11 private boolean lightOn;12 13 // time before Light turns off automatically (3 seconds)14 public static final int AUTOMATIC_TURNOFF_DELAY = 3000;15 16 // LightListener listens for when Light should turn on/off17 private LightListener lightListener;18 19 // location where Light turned on or off20 private Location lightLocation;21 22 // set LightListener23 public void setLightListener( LightListener listener )24 {25 lightListener = listener;26 }
Light turns itself off automatically
after three seconds
Enable LightListener to receive LightEvents
2003 Prentice Hall, Inc.All rights reserved.
Outline
Light.javaClass Light represents a Light on the Floor in the model.
Lines 29-63
Lines 36-37
Lines 42-61
27 28 // turn on Light29 public void turnOnLight( Location location )30 {31 if ( !lightOn ) {32 33 lightOn = true;34 35 // send LightEvent to LightListener36 lightListener.lightTurnedOn( 37 new LightEvent( this, location ) );38 39 lightLocation = location;40 41 // declare Thread that ensures automatic Light turn off42 Thread thread = new Thread( 43 new Runnable() {44 45 public void run()46 {47 // turn off Light if on for more than 3 seconds48 try {49 Thread.sleep( AUTOMATIC_TURNOFF_DELAY );50 turnOffLight( lightLocation );51 }52
Method to turn on Light
Send LightEvent to LightListener
(ElevatorShaft) when Light turns on
Instantiate and start Thread that turns Light off
automatically after three seconds
2003 Prentice Hall, Inc.All rights reserved.
Outline
Light.javaClass Light represents a Light on the Floor in the model.
Lines 66-76
Lines 73-74
53 // handle exception if interrupted54 catch ( InterruptedException exception ) {55 exception.printStackTrace();56 }57 }58 } // end anonymous inner class59 );60 61 thread.start();62 }63 } // end method turnOnLight64 65 // turn off Light66 public void turnOffLight( Location location )67 {68 if ( lightOn ) {69 70 lightOn = false;71 72 // send LightEvent to LightListener73 lightListener.lightTurnedOff( 74 new LightEvent( this, location ) );75 }76 } // end method turnOffLight
Method to turn off Light
Send LightEvent to LightListener
(ElevatorShaft) when Light turns off
2003 Prentice Hall, Inc.All rights reserved.
Outline
Light.javaClass Light represents a Light on the Floor in the model.
Lines 85-89
Lines 92-96
77 78 // return whether Light is on or off79 public boolean isLightOn()80 {81 return lightOn;82 }83 84 // invoked when Elevator has departed85 public void elevatorDeparted( 86 ElevatorMoveEvent moveEvent )87 {88 turnOffLight( moveEvent.getLocation() );89 }90 91 // invoked when Elevator has arrived92 public void elevatorArrived( 93 ElevatorMoveEvent moveEvent )94 {95 turnOnLight( moveEvent.getLocation() );96 }97 }
When Elevator departs from Floor, turn off Light
When Elevator arrives at Floor, turn off Light
2003 Prentice Hall, Inc.All rights reserved.
Outline
Bell.javaClass Bell represents the Bell in the model.
Lines 17-18
Lines 22-25
1 // Bell.java2 // Represents Bell in simulation3 package com.deitel.jhtp5.elevator.model;4 5 // Deitel packages6 import com.deitel.jhtp5.elevator.event.*;7 8 public class Bell implements ElevatorMoveListener {9 10 // BellListener listens for BellEvent object11 private BellListener bellListener;12 13 // ring bell and send BellEvent object to listener14 private void ringBell( Location location )15 {16 if ( bellListener != null )17 bellListener.bellRang( 18 new BellEvent( this, location ) );19 }20 21 // set BellListener22 public void setBellListener( BellListener listener )23 {24 bellListener = listener;25 }
Send BellEvent to BellListener
(ElevatorShaft) when Bell has rung
Enable BellListener to receive BellEvents
2003 Prentice Hall, Inc.All rights reserved.
Outline
Bell.javaClass Bell represents the Bell in the model.
Lines 31-34
26 27 // invoked when Elevator has departed28 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {}29 30 // invoked when Elevator has arrived31 public void elevatorArrived( ElevatorMoveEvent moveEvent )32 {33 ringBell( moveEvent.getLocation() );34 }35 }
When Elevator arrives at Floor, ring Bell
2003 Prentice Hall, Inc. All rights reserved.
E.8 Class Elevator
• Elevator– Travels in ElevatorShaft between Floors
– Carries Person– “Is a” Location
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Line 12
Lines 19-28
1 // Elevator.java2 // Travels between Floors in the ElevatorShaft3 package com.deitel.jhtp5.elevator.model;4 5 // Java core packages6 import java.util.*;7 8 // Deitel packages9 import com.deitel.jhtp5.elevator.event.*;10 import com.deitel.jhtp5.elevator.ElevatorConstants;11 12 public class Elevator extends Location implements Runnable, 13 BellListener, ElevatorConstants {14 15 // manages Elevator thread16 private boolean elevatorRunning = false;17 18 // describes Elevator state (idle or moving)19 private boolean moving = false;20 21 // current Floor22 private Location currentFloorLocation;23 24 // destination Floor25 private Location destinationFloorLocation;26 27 // Elevator needs to service other Floor28 private boolean summoned;29
Use class diagram to determine associations and attributes of Elevator
Class Elevator “is a” Location
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 31-34
Lines 37-39, 44
Lines 55-57
30 // listener objects31 private Set elevatorMoveListeners;32 private BellListener bellListener;33 private ButtonListener elevatorButtonListener;34 private DoorListener elevatorDoorListener;35 36 // ElevatorDoor, Button and Bell on Elevator37 private ElevatorDoor elevatorDoor;38 private Button elevatorButton;39 private Bell bell;40 41 public static final int ONE_SECOND = 1000;42 43 // time needed to travel between Floors (5 seconds)44 private static final int TRAVEL_TIME = 5 * ONE_SECOND;45 46 // Elevator's thread to handle asynchronous movement47 private Thread thread;48 49 // constructor creates variables; registers for ButtonEvents50 public Elevator( Floor firstFloor, Floor secondFloor )51 {52 setLocationName( ELEVATOR_NAME );53 54 // instantiate Elevator's Door, Button and Bell55 elevatorDoor = new ElevatorDoor();56 elevatorButton = new Button();57 bell = new Bell();
Use class diagram to determine associations and attributes of Elevator
Instantiate ElevatorDoor, Button and Bell inside Elevator
Declare listeners that receive events from model (and will send these events to ElevatorShaft)
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Line 60
Lines 81-101
58 59 // register Elevator for BellEvents60 bell.setBellListener( this );61 62 // instantiate listener Set63 elevatorMoveListeners = new HashSet( 1 );64 65 // start Elevator on first Floor66 currentFloorLocation = firstFloor;67 destinationFloorLocation = secondFloor;68 69 // register elevatorButton for ElevatorMoveEvents70 addElevatorMoveListener( elevatorButton );71 72 // register elevatorDoor for ElevatorMoveEvents73 addElevatorMoveListener( elevatorDoor );74 75 // register bell for ElevatorMoveEvents76 addElevatorMoveListener( bell );77 78 // anonymous inner class listens for ButtonEvents from79 // elevatorButton80 elevatorButton.setButtonListener( 81 new ButtonListener() {82
Instantiate anonymous inner class to listen for ButtonEvents from Elevator’s Button
Listen for BellEvents
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 84-92
Lines 95-100
Lines 107-124
83 // invoked when elevatorButton has been pressed84 public void buttonPressed( ButtonEvent buttonEvent )85 {86 // send ButtonEvent to listener87 elevatorButtonListener.buttonPressed( 88 buttonEvent );89 90 // start moving Elevator to destination Floor91 setMoving( true );92 }93 94 // invoked when elevatorButton has been reset95 public void buttonReset( ButtonEvent buttonEvent )96 {97 // send ButtonEvent to listener98 elevatorButtonListener.buttonReset( 99 buttonEvent );100 }101 } // end anonymous inner class102 );103 104 // anonymous inner class listens for DoorEvents from105 // elevatorDoor106 elevatorDoor.addDoorListener( 107 new DoorListener() {108
When Elevator’s Button is pressed, forward
ButtonEvent to ButtonListener and
signal Elevator to move to other Floor
When Elevator’s Button is reset, forward
ButtonEvent to ButtonListener
Instantiate anonymous inner class to listen for DoorEvents from Elevator’s Door
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 110-123
Lines 129-134
109 // invoked when elevatorDoor has opened110 public void doorOpened( DoorEvent doorEvent )111 {112 // send DoorEvent to listener113 elevatorDoorListener.doorOpened( new DoorEvent( 114 doorEvent.getSource(), Elevator.this ));115 }116 117 // invoked when elevatorDoor has closed118 public void doorClosed( DoorEvent doorEvent )119 {120 // send DoorEvent to listener121 elevatorDoorListener.doorClosed( new DoorEvent( 122 doorEvent.getSource(), Elevator.this ));123 }124 } // end anonymous inner class125 );126 } // end Elevator constructor127 128 // swaps current Floor Location with opposite Floor Location129 private void changeFloors()130 {131 Location location = currentFloorLocation;132 currentFloorLocation = destinationFloorLocation;133 destinationFloorLocation = location;‘134 }
When Elevator’s Door is opened or
closed, open or close Door on Floor
(Location), and send DoorEvent to DoorListener
private method for changing destination Location
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 137-144
Lines 147-150
Lines 153-187
Lines 178-179
135 136 // start Elevator thread137 public void start()138 {139 if ( thread == null )140 thread = new Thread( this );141 142 elevatorRunning = true;143 thread.start();144 }145 146 // stop Elevator thread; method run terminates147 public void stopElevator()148 {149 elevatorRunning = false;150 }151 152 // Elevator thread's run method153 public void run()154 {155 while ( isElevatorRunning() ) {156 157 // remain idle until awoken158 while ( !isMoving() )159 pauseThread( 10 );160 161 // pause while passenger exits (if one exists)162 pauseThread( ONE_SECOND );
Start Elevator’s Thread
Stop Elevator’s Thread
Invoked after ElevatorShaft invokes Elevator’s Start method
When Elevator maintains “waiting state,” the Elevator should not move
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Line 165
Line 171
Lines 174-177
Line 180
Line 183
163 164 // close elevatorDoor165 getDoor().closeDoor( currentFloorLocation ); 166 167 // closing Door takes one second168 pauseThread( ONE_SECOND );169 170 // issue elevatorDeparted Event171 sendDepartureEvent( currentFloorLocation );172 173 // Elevator needs 5 seconds to travel174 pauseThread( TRAVEL_TIME );175 176 // stop Elevator177 setMoving( false );178 179 // swap Floor Locations180 changeFloors();181 182 // issue elevatorArrived Event183 sendArrivalEvent( currentFloorLocation );184 185 } // end while loop186 187 } // end method run188
When Elevator maintains “moving state,” close Elevator Door
Notify listeners that Elevator has departed
Travel five seconds, then stop
Change destination Floor
Notify listeners that Elevator has arrived
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 190-200
Lines 203-206
Lines 209-212
189 // pause concurrent thread for number of milliseconds190 private void pauseThread( int milliseconds )191 {192 try {193 Thread.sleep( milliseconds );194 }195 196 // handle if interrupted while sleeping197 catch ( InterruptedException exception ) {198 exception.printStackTrace();199 }200 } // end method pauseThread201 202 // return Button on Elevator203 public Button getButton()204 {205 return elevatorButton;206 }207 208 // return Door on Elevator209 public Door getDoor()210 {211 return elevatorDoor;212 }213 214 // set if Elevator should move215 private void setMoving( boolean elevatorMoving )216 {217 moving = elevatorMoving;218 }
Private method for putting Elevator’s Thread to sleep
Implement Location method getButton to
return Elevator’s Button
Implement Location method getDoor to return
Elevator’s Door
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 233-237
Lines 240-243
219 220 // is Elevator moving?221 public boolean isMoving()222 {223 return moving;224 }225 226 // is Elevator thread running?227 private boolean isElevatorRunning()228 {229 return elevatorRunning;230 }231 232 // register ElevatorMoveListener for ElevatorMoveEvents233 public void addElevatorMoveListener( 234 ElevatorMoveListener listener )235 {236 elevatorMoveListeners.add( listener );237 }238 239 // register BellListener fpr BellEvents240 public void setBellListener( BellListener listener )241 {242 bellListener = listener;243 }244
Enable ElevatorMoveListeners to receive ElevatorMoveEvents
Enable BellListener to receive BellEvents
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 246-249
Lines 252-255
Lines 258-283
245 // register ButtonListener for ButtonEvents246 public void setButtonListener( ButtonListener listener )247 {248 elevatorButtonListener = listener;249 }250 251 // register DoorListener for DoorEvents252 public void setDoorListener( DoorListener listener )253 {254 elevatorDoorListener = listener;255 }256 257 // notify all ElevatorMoveListeners of arrival258 private void sendArrivalEvent( Location location )259 {260 // obtain iterator from Set261 Iterator iterator = elevatorMoveListeners.iterator();262 263 // get next DoorListener264 while ( iterator.hasNext() ) {265 266 // get next ElevatorMoveListener from Set267 ElevatorMoveListener listener = 268 ( ElevatorMoveListener ) iterator.next();269 270 // send event to listener271 listener.elevatorArrived( new 272 ElevatorMoveEvent( this, location ) );273 274 } // end while loop
Enable DoorListener to receive DoorEvents
Private method for notifying all ElevatorMoveListeners that
Elevator has arrived
Enable ButtonListener to receive ButtonEvents
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 277-279
Lines 286-303
275 276 // service queued request, if one exists277 if ( summoned ) {278 setMoving( true ); // start moving Elevator279 }280 281 summoned = false; // request has been serviced282 283 } // end method sendArrivalEvent284 285 // notify all ElevatorMoveListeners of departure286 private void sendDepartureEvent( Location location )287 {288 // obtain iterator from Set289 Iterator iterator = elevatorMoveListeners.iterator();290 291 // get next DoorListener292 while ( iterator.hasNext() ) {293 294 // get next ElevatorMoveListener from Set295 ElevatorMoveListener listener = 296 ( ElevatorMoveListener ) iterator.next();297 298 // send ElevatorMoveEvent to this listener299 listener.elevatorDeparted( new ElevatorMoveEvent( 300 this, currentFloorLocation ) );301 302 } // end while loop303 } // end method sendDepartureEvent
If a queued request exists, service that request (i.e., move to other Floor)
Private method for notifying all ElevatorMoveListeners that
Elevator has departed
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 306-332
Line 315
Line 319
Line 325-326
304 305 // request Elevator306 public void requestElevator( Location location )307 {308 // if Elevator is idle309 if ( !isMoving() ) {310 311 // if Elevator is on same Floor of request312 if ( location == currentFloorLocation )313 314 // Elevator has already arrived; send arrival event315 sendArrivalEvent( currentFloorLocation );316 317 // if Elevator is on opposite Floor of request318 else {319 setMoving( true ); // move to other Floor320 }321 }322 else // if Elevator is moving323 324 // if Elevator departed from same Floor as request325 if ( location == currentFloorLocation )326 summoned = true;327 328 // if Elevator is traveling to Floor of request,329 // simply continue traveling330 331 } // end method requestElevator
If Person requests Elevator just after Elevator has left the Floor on which the Person is waiting, Elevator must
“remember” to return to that Floor
If Elevator is idle and servicing the Floor of the request, send elevatorArrived event
If Elevator is idle and servicing opposite Floor of the request, move to other Floor
Public service to request Elevator (used by Buttons)
2003 Prentice Hall, Inc.All rights reserved.
Outline
Elevator.javaClass Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects.
Lines 334-339
332 333 // invoked when bell has rung334 public void bellRang( BellEvent bellEvent )335 {336 // send event to bellLirdstener337 if ( bellListener != null )338 bellListener.bellRang( bellEvent );339 }340 341 // get the currentFloorLocation of the Elevator 342 public Location getCurrentFloor()343 {344 return currentFloorLocation;345 }346 }
Notify BellListener that Bell has rung
2003 Prentice Hall, Inc. All rights reserved.
E.9 Class Person
• Person– Walks across Floor to Elevator– Rides Elevator– “Has a” Location– Operates asynchronously with other objects
• Extends class Thread
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
Lines 14-20
Line 23
Lines 26-34
1 // Person.java2 // Person riding the elevator3 package com.deitel.jhtp5.elevator.model;4 5 // Java core packages6 import java.util.*;7 8 // Deitel packages9 import com.deitel.jhtp5.elevator.event.*;10 11 public class Person extends Thread {12 13 // identification number14 private int ID = -1;15 16 // represents whether Person is moving or waiting17 private boolean moving;18 19 // reference to Location (either on Floor or in Elevator)20 private Location location;21 22 // listener object for PersonMoveEvents23 private PersonMoveListener personMoveListener;24 25 // time in milliseconds to walk to Button on Floor26 private static final int TIME_TO_WALK = 3000;27
Declare listener that receives
PersonMoveEvent
Use class diagram to determine associations and
attributes of Person
Define constants that indicate each type of event that a Person may send
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
Lines 37-44
Lines 47-51
Lines 54-57
28 // types of messages Person may send29 public static final int PERSON_CREATED = 1;30 public static final int PERSON_ARRIVED = 2;31 public static final int PERSON_ENTERING_ELEVATOR = 3;32 public static final int PERSON_PRESSING_BUTTON = 4;33 public static final int PERSON_EXITING_ELEVATOR = 5;34 public static final int PERSON_EXITED = 6;35 36 // Person constructor set initial location37 public Person( int identifier, Location initialLocation )38 {39 super();40 41 ID = identifier; // assign unique identifier42 location = initialLocation; // set Floor Location43 moving = true; // start moving toward Button on Floor44 }45 46 // set listener for PersonMoveEvents47 public void setPersonMoveListener(48 PersonMoveListener listener )49 {50 personMoveListener = listener;51 }52 53 // set Person Location54 private void setLocation( Location newLocation )55 {56 location = newLocation;57 }
Person constructor assigns unique identifier and sets initial Floor on which Person is located
Enable PersonMoveListener to receive PersonMoveEvents
When Door opens, set Person’s Location to that
of where the Door opened
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
Lines 84-204
Line 87
58 59 // get current Location60 private Location getLocation()61 {62 return location;63 }64 65 // get identifier66 public int getID()67 {68 return ID;69 }70 71 // set if Person should move72 public void setMoving( boolean personMoving )73 {74 moving = personMoving;75 }76 77 // get if Person should move78 public boolean isMoving()79 {80 return moving;81 }82 83 // Person either rides or waits for Elevator84 public void run()85 {86 // indicate that Person thread was created87 sendPersonMoveEvent( PERSON_CREATED );
Invoked when Person’s Thread is started
Notify listeners when Person is created
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
Line 90
Line 96
Lines 109-117
88 89 // walk to Elevator90 pauseThread( TIME_TO_WALK );91 92 // stop walking at Elevator93 setMoving( false );94 95 // Person arrived at Elevator96 sendPersonMoveEvent( PERSON_ARRIVED ); 97 98 // get Door on current Floor99 Door currentFloorDoor = location.getDoor();100 101 // get Elevator102 Elevator elevator = 103 ( (Floor) getLocation() ).getElevatorShaft().getElevator();104 105 // begin exclusive access to currentFloorDoor106 synchronized ( currentFloorDoor ) {107 108 // check whether Floor Door is open109 if ( !currentFloorDoor.isDoorOpen() ) {110 111 sendPersonMoveEvent( PERSON_PRESSING_BUTTON );112 pauseThread( 1000 );113 114 // press Floor's Button to request Elevator115 Button floorButton = getLocation().getButton();116 floorButton.pressButton( getLocation() );117 }
Put Person Thread to sleep for three seconds, simulating a three second walk to the Elevator
Notify listeners when Person arrived at Elevator
If Door is closed, press Button on Floor and wait
for Elevator to arrive
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
Lines 122-123
Lines 135-158
Line 138
Line 141
118 119 // wait for Floor door to open120 try {121 122 while ( !currentFloorDoor.isDoorOpen() )123 currentFloorDoor.wait();124 }125 126 // handle exception waiting for Floor door to open127 catch ( InterruptedException interruptedException ) {128 interruptedException.printStackTrace();129 } 130 131 // Floor Door takes one second to open132 pauseThread( 1000 ); 133 134 // implicitly wait for exclusive access to elevator135 synchronized ( elevator ) { 136 137 // Person enters Elevator138 sendPersonMoveEvent( PERSON_ENTERING_ELEVATOR );139 140 // set Person Location to Elevator141 setLocation( elevator );142 143 // Person takes one second to enter Elevator144 pauseThread( 1000 );
Wait for the currentFloorDoor to
open
Notify listeners when Person entered Elevator
Only one Person is allowed to occupy the Elevator at one time
Set the Person’s location to the Elevator
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
Line 147
Line 154
Lines 173-174
145 146 // pressing Elevator Button takes one second147 sendPersonMoveEvent( PERSON_PRESSING_BUTTON );148 pauseThread( 1000 );149 150 // get Elevator's Button151 Button elevatorButton = getLocation().getButton();152 153 // press Elevator's Button154 elevatorButton.pressButton( location );155 156 // Door closing takes one second157 pauseThread( 1000 );158 } 159 160 } // give up exclusive access to Floor door161 162 // get exclusive access to Elevator163 synchronized( elevator ) {164 165 // get Elevator door166 Door elevatorDoor = getLocation().getDoor();167 168 // wait for Elevator door to open169 synchronized( elevatorDoor ) {170 171 try {172 173 while ( !elevatorDoor.isDoorOpen() )174 elevatorDoor.wait();175 }
Invoke method wait on the elevatorDoor
Press the Elevator’s button to instruct the Elevator to begin
traveling
Notify listeners when Person pressed Button
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
Line 186
Line 189
Line 202
176 177 // handle exception waiting for Elevator door to open178 catch ( InterruptedException interruptedException ) {179 interruptedException.printStackTrace();180 } 181 182 // waiting for Elevator's Door to open takes a second183 pauseThread( 1000 );184 185 // move Person onto Floor186 setLocation( elevator.getCurrentFloor() );187 188 // walk away from Elevator189 setMoving( true );190 191 // Person exiting Elevator192 sendPersonMoveEvent( PERSON_EXITING_ELEVATOR );193 194 } // release elevatorDoor lock, allowing door to close195 196 } // release elevator lock, allowing waiting Person to enter197 198 // walking from elevator takes five seconds199 pauseThread( 2 * TIME_TO_WALK );200 201 // Person exits simulation202 sendPersonMoveEvent( PERSON_EXITED ); 203 204 } // end method run
Set Person’s new location to Floor
Person walks away from the Elevator
Person left the simulation
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
Lines 207-217
Lines 220-262
205 206 // pause thread for desired number of milliseconds207 private void pauseThread( int milliseconds )208 {209 try {210 sleep( milliseconds );211 }212 213 // handle exception if interrupted when paused214 catch ( InterruptedException interruptedException ) {215 interruptedException.printStackTrace();216 }217 } // end method pauseThread218 219 // send PersonMoveEvent to listener, depending on event type220 private void sendPersonMoveEvent( int eventType )221 {222 // create new event223 PersonMoveEvent event = 224 new PersonMoveEvent( this, getLocation(), getID() );225 226 // send Event to this listener, depending on eventType227 switch ( eventType ) {228 229 // Person has been created230 case PERSON_CREATED:231 personMoveListener.personCreated( event );232 break;
Utility method for putting Person’s Thread to sleep
Utility method for determining which
PersonMoveEvent to send to listener, then
sending that event
2003 Prentice Hall, Inc.All rights reserved.
Outline
Person.javaClass Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects.
233 234 // Person arrived at Elevator235 case PERSON_ARRIVED:236 personMoveListener.personArrived( event );237 break;238 239 // Person entered Elevator240 case PERSON_ENTERING_ELEVATOR:241 personMoveListener.personEntered( event );242 break;243 244 // Person pressed Button object245 case PERSON_PRESSING_BUTTON:246 personMoveListener.personPressedButton( event );247 break;248 249 // Person exited Elevator250 case PERSON_EXITING_ELEVATOR:251 personMoveListener.personDeparted( event );252 break;253 254 // Person exited simulation255 case PERSON_EXITED:256 personMoveListener.personExited( event );257 break;258 259 default:260 break;261 }262 } // end method sendPersonMoveEvent263 }
2003 Prentice Hall, Inc. All rights reserved.
E.10Artifacts Revisited
• Artifacts for package model– Each class in model imports package model– Each component in package model maps to distinct file
• Therefore, each component maps to distinct class
– Package model aggregates package event
2003 Prentice Hall, Inc. All rights reserved.
Fig. E.15 Artifacts for package model.
Bell.java
<<file>>
model
Button.java
<<file>>
Door.java
<<file>>
Elevator.java
<<file>>
ElevatorSimulation.java
<<file>>
ElevatorShaft.java
<<file>>
Floor.java
<<file>>
Light.java
<<file>>
Location.java
<<file>>
Person.java
<<file>>
event<<imports>>
ElevatorDoor.java
<<file>>
<<file>>
Bell.java