1
Appendix E – Elevator Model
Outline
E.1
E.2
E.3
E.4
E.5
E.6
E.7
E.8
E.9
E.10
E.11
Introduction
Class ElevatorSimulation
Classes Location and Floor
Class Door and ElevatorDoor
Class Button
Class ElevatorShaft
Classes Light and Bell
Class Elevator
Class Person
Artifacts Revisited
Conclusion
2003 Prentice Hall, Inc. All rights reserved.
2
E.1
Introduction
• Classes implement the MVC model
2003 Prentice Hall, Inc. All rights reserved.
3
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// ElevatorSimulation.java
// Elevator simulation model with ElevatorShaft and two Floors
package com.deitel.jhtp5.elevator.model;
// Java core packages
import java.util.*;
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
import com.deitel.jhtp5.elevator.ElevatorConstants;
Outline
ElevatorSimulat
ElevatorSimulation
implements
ion.java
ElevatorSimulationListener,
which
Class
inherits from all listener
interfaces
ElevatorSimulat
public class ElevatorSimulation implements ElevatorSimulationListener,
ElevatorConstants {
ion represents the
MVC model in our
elevator simulation.
Line 12
// declare two-Floor architecture in simulation
private Floor firstFloor;
private Floor secondFloor;
// ElevatorShaft in simulation
private ElevatorShaft elevatorShaft;
// objects listening for events from ElevatorModel
private Set personMoveListeners;
private DoorListener doorListener;
private ButtonListener buttonListener;
private LightListener lightListener;
private BellListener bellListener;
private ElevatorMoveListener elevatorMoveListener;
Lines 16-20
Use class diagram to determine associations
with Floor and ElevatorShaft
Lines 23-28
Declare listeners that receive
events from model (and will send
these events to ElevatorView)
2003 Prentice Hall, Inc.
All rights reserved.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Outline
// cumulative number of people in simulation
private int numberOfPeople = 0;
Use class diagram to
determine attributes
ElevatorSimulat
ion.java
Class
Instantiate
Floors and
ElevatorSimulat
ElevatorShaft,
thenthe
ion represents
assign the ElevatorShaft
MVC model in our
referenceelevator
to each Floor
simulation.
// constructor instantiates ElevatorShaft and Floors
public ElevatorSimulation()
{
// instantiate firstFloor and secondFloor objects
firstFloor = new Floor( FIRST_FLOOR_NAME );
secondFloor = new Floor( SECOND_FLOOR_NAME );
// instantiate ElevatorShaft object
elevatorShaft =
new ElevatorShaft( firstFloor, secondFloor );
Line 31
// give elevatorShaft reference to first and second Floor
firstFloor.setElevatorShaft( elevatorShaft );
secondFloor.setElevatorShaft( elevatorShaft );
// register for events from ElevatorShaft
elevatorShaft.setDoorListener( this );
elevatorShaft.setButtonListener( this );
elevatorShaft.addElevatorMoveListener( this );
elevatorShaft.setLightListener( this );
elevatorShaft.setBellListener( this );
Lines 37-46
Lines 49-53
Register ElevatorSimulation
for events from ElevatorShaft
// instantiate Set for ElevatorMoveListener objects
personMoveListeners = new HashSet( 1 );
} // end ElevatorModel constructor
2003 Prentice Hall, Inc.
All rights reserved.
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// return Floor with given name
private Floor getFloor( String name )
{
if ( name.equals( FIRST_FLOOR_NAME ) )
return firstFloor;
else
if ( name.equals( SECOND_FLOOR_NAME ) )
return secondFloor;
else
return null;
} // end method getFloor
Outline
ElevatorSimulat
ion.java
Class
ElevatorSimulat
ion represents the
MVC model in our
elevator simulation.
Lines 75-91
// add Person to Elevator Simulator
Method for adding Person
public void addPerson( String floorName )
to simulation model Lines 78-86
{
// instantiate new Person and place on Floor
Person person =
new Person( numberOfPeople, getFloor( floorName ) );
Instantiate Person, register
person.setName( Integer.toString( numberOfPeople ) );
// register listener for Person events
person.setPersonMoveListener( this );
ElevatorModel to receive
events from that Person and
start Person’s thread
// start Person thread
person.start();
2003 Prentice Hall, Inc.
All rights reserved.
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// increment number of Person objects in simulation
numberOfPeople++;
} // end method addPerson
// invoked when Elevator has departed from Floor
public void elevatorDeparted( ElevatorMoveEvent moveEvent )
{
elevatorMoveListener.elevatorDeparted( moveEvent );
}
// invoked when Elevator has arrived at destination Floor
public void elevatorArrived( ElevatorMoveEvent moveEvent )
{
elevatorMoveListener.elevatorArrived( moveEvent );
}
Outline
ElevatorSimulat
When Elevator
has
ion.java
departed fromClass
Floor, notify
listener (i.e.,ElevatorSimulat
ElevatorView, in this
simulation)
ion
represents the
MVC model in our
elevator simulation.
When Elevator has
arrived at Floor, notify
Lines 94-97
listener (ElevatorView)
Lines 100-103
When Person performs some action
on event type
(event) in model, determine Lines
which107-153
event was
sent, then forward the event to any listeners
// send PersonMoveEvent to listener, depending
private void sendPersonMoveEvent(
int eventType, PersonMoveEvent event )
{
Iterator iterator = personMoveListeners.iterator();
while ( iterator.hasNext() ) {
PersonMoveListener listener =
( PersonMoveListener ) iterator.next();
2003 Prentice Hall, Inc.
All rights reserved.
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// send Event to this listener, depending on eventType
switch ( eventType ) {
// Person has been created
case Person.PERSON_CREATED:
listener.personCreated( event );
break;
// Person arrived at Elevator
case Person.PERSON_ARRIVED:
listener.personArrived( event );
break;
Outline
ElevatorSimulat
ion.java
Class
ElevatorSimulat
When Person performs some
ion represents the
action (event) in model,
MVC model in our
determine which event was
elevator simulation.
sent, then forward the event to
any listeners
Lines 108-153
// Person entered Elevator
case Person.PERSON_ENTERING_ELEVATOR:
listener.personEntered( event );
break;
// Person pressed Button object
case Person.PERSON_PRESSING_BUTTON:
listener.personPressedButton( event );
break;
// Person exited Elevator
case Person.PERSON_EXITING_ELEVATOR:
listener.personDeparted( event );
break;
2003 Prentice Hall, Inc.
All rights reserved.
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Person exited simulation
case Person.PERSON_EXITED:
listener.personExited( event );
break;
default:
break;
}
}
} // end method sendPersonMoveEvent
Outline
ElevatorSimulat
ion.java
Class
ElevatorSimulat
ion represents the
MVC model in our
elevator simulation.
// invoked when Person has been created in model
public void personCreated( PersonMoveEvent moveEvent )
{
sendPersonMoveEvent( Person.PERSON_CREATED, moveEvent );
}
When Person has been
Lines 156-159
created, notify listeners
// invoked when Person has arrived at Floor's Button
public void personArrived( PersonMoveEvent moveEvent )
{
sendPersonMoveEvent( Person.PERSON_ARRIVED, moveEvent );
}
Lines 168-171
When Person
has
arrived at Elevator,
notify listeners
// invoked when Person has pressed Button
public void personPressedButton( PersonMoveEvent moveEvent )
{
sendPersonMoveEvent( Person.PERSON_PRESSING_BUTTON,
moveEvent );
}
Lines 162-165
When Person has pressed
Button, notify listeners
2003 Prentice Hall, Inc.
All rights reserved.
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// invoked when Person has entered Elevator
public void personEntered( PersonMoveEvent moveEvent )
{
sendPersonMoveEvent( Person.PERSON_ENTERING_ELEVATOR,
moveEvent );
}
// invoked when Person has departed from Elevator
public void personDeparted( PersonMoveEvent moveEvent )
{
sendPersonMoveEvent( Person.PERSON_EXITING_ELEVATOR,
moveEvent );
}
// invoked when Person has exited Simulation
public void personExited( PersonMoveEvent moveEvent )
{
sendPersonMoveEvent( Person.PERSON_EXITED, moveEvent );
}
// invoked when Door has opened
public void doorOpened( DoorEvent doorEvent )
{
doorListener.doorOpened( doorEvent );
}
Outline
When Person has entered
Elevator, notify listeners
ElevatorSimulat
ion.java
Class
ElevatorSimulat
ion represents
When Person
has left the
in our
Elevator, MVC
notifymodel
listeners
elevator simulation.
Lines 175-179
Lineshas
182-186
When Person
exited
simulation, notify listeners
Lines 189-192
Lines 195-198
When Door has opened,
notify listeners
2003 Prentice Hall, Inc.
All rights reserved.
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// invoked when Door has closed
public void doorClosed( DoorEvent doorEvent )
{
doorListener.doorClosed( doorEvent );
}
// invoked when Button has been pressed
public void buttonPressed( ButtonEvent buttonEvent )
{
buttonListener.buttonPressed( buttonEvent );
}
// invoked when Button has been reset
public void buttonReset( ButtonEvent buttonEvent )
{
buttonListener.buttonReset( buttonEvent );
}
// invoked when Bell has rung
public void bellRang( BellEvent bellEvent )
{
bellListener.bellRang( bellEvent );
}
// invoked when Light has turned on
public void lightTurnedOn( LightEvent lightEvent )
{
lightListener.lightTurnedOn( lightEvent );
}
Outline
When Door has closed,
notify listeners ElevatorSimulat
ion.java
Class
ElevatorSimulat
ion has
represents
When Button
been the
MVClisteners
model in our
pressed, notify
elevator simulation.
Lines 201-204
When Button has been
207-210
reset, notifyLines
listeners
Lines 213-216
Lines 219-222
When Bell has rung,
notify listeners
Lines 225-228
When Light has been
turned on, notify listeners
2003 Prentice Hall, Inc.
All rights reserved.
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// invoked when Light has turned off
public void lightTurnedOff( LightEvent lightEvent )
{
lightListener.lightTurnedOff( lightEvent );
}
// set listener for ElevatorModelListener
public void setElevatorSimulationListener(
ElevatorSimulationListener listener )
{
// ElevatorModelListener extends all interfaces
addPersonMoveListener( listener );
setElevatorMoveListener( listener );
setDoorListener( listener );
setButtonListener( listener );
setLightListener( listener );
setBellListener( listener );
}
// set listener for PersonMoveEvents
public void addPersonMoveListener(
PersonMoveListener listener )
{
personMoveListeners.add( listener );
}
Outline
ElevatorSimulat
When Light
has been
ion.java
turned off, notify
listeners
Class
ElevatorSimulat
Allow ElevatorSimulationListener
ion represents the
to listen for all events from model
MVC model in our
elevator simulation.
below
Lines 231-234
Lines 237-247
Lines 250-254
Allow PersonListener to listen for
PersonMoveEvents from model
2003 Prentice Hall, Inc.
All rights reserved.
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// set listener for DoorEvents
public void setDoorListener( DoorListener listener )
{
doorListener = listener;
}
// set listener for ButtonEvents
public void setButtonListener( ButtonListener listener )
{
buttonListener = listener;
}
// add listener for ElevatorMoveEvents
public void setElevatorMoveListener(
ElevatorMoveListener listener )
{
elevatorMoveListener = listener;
}
Allow DoorListener to listen
for DoorEvents
from model
ElevatorSimulat
ion.java
Class
ElevatorSimulat
ion represents the
Allow ButtonListener
MVC modeltoinlisten
our
for ButtonEvents
from
model
elevator simulation.
Allow ElevatorMoveListener to listen
Lines 257-260
for ElevatorMoveEvents from model
Lines 263-266
Allow LightListener to listen
for LightEvents from model
Lines 269-273
// set listener for LightEvents
public void setLightListener( LightListener listener )
{
lightListener = listener;
}
// set listener for BellEvents
public void setBellListener( BellListener listener )
{
bellListener = listener;
}
}
Outline
Lines 276-279
Lines 282-285
Allow BellListener to listen
for BellEvents from model
2003 Prentice Hall, Inc.
All rights reserved.
Fig. E.2 Class diagram showing
realizations in the elevator model (Part 1).
ElevatorSimulationListener
ButtonListener
DoorListener
ElevatorSimulation
LightListener
BellListener
Elevator
ButtonListener
DoorListener
BellListener
ElevatorMoveListener
ElevatorShaft
ElevatorMoveListener
ElevatorDoor
2003 Prentice Hall, Inc. All rights reserved.
Light
Bell
Button
14
Fig. E.3 Class diagram showing
realizations in the elevator model (Part 2).
PersonMoveListener
LightListener
ButtonListener
DoorListener
BellListener
ElevatorSimulationListener
ElevatorSimulation
2003 Prentice Hall, Inc. All rights reserved.
ElevatorMoveListener
15
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, ElevatorMoveListener
Button
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.
16
17
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Location.java
// Abstract superclass representing location in simulation
package com.deitel.jhtp5.elevator.model;
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
public abstract class Location {
// name of Location
private String locationName;
// set name of Location
protected void setLocationName( String name )
{
locationName = name;
}
Outline
Location.java
Location superclass
that represents a
location in the
simulation.
Name of Location can equal “elevator,”
“first floor” or “second
Linefloor”
11
Lines 26-29
// return name of Location
public String getLocationName()
{
return locationName;
}
// return Button at Location
public abstract Button getButton();
// return Door object at Location
public abstract Door getDoor();
Classes Floor and
Elevator implement these
abstract methods to
return appropriate objects
}
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Floor.java
// Represents a Floor located next to an ElevatorShaft
package com.deitel.jhtp5.elevator.model;
// Deitel packages
import com.deitel.jhtp5.elevator.ElevatorConstants;
public class Floor extends Location
implements ElevatorConstants {
// reference to ElevatorShaft object
private ElevatorShaft elevatorShaft;
Outline
Floor.java
Class Floor—a
subclass of
Location—
represents a Floor
across which a
Person walks to the
Elevator.
// Floor constructor sets name of Floor
public Floor( String name )
{
setLocationName( name );
}
2003 Prentice Hall, Inc.
All rights reserved.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Outline
// get first or second Floor Button, using Location name Implement Location’s abstract
public Button getButton()
method getButton to return Button
{
on either first or second Floor
if ( getLocationName().equals( FIRST_FLOOR_NAME ) )
Floor.java
return getElevatorShaft().getFirstFloorButton();
Class Floor—a
else
if ( getLocationName().equals( SECOND_FLOOR_NAME ) )
return getElevatorShaft().getSecondFloorButton();
else
return null;
subclass of
Location—
represents a Floor
across which a
Person walks to the
Elevator.
} // end method getButton
// get first or second Floor Door, using Location name
public Door getDoor()
{
if ( getLocationName().equals( FIRST_FLOOR_NAME ) )
return getElevatorShaft().getFirstFloorDoor();
else
Lines 21-33
Implement Location’s
abstract
method
Lines 36-48
getDoor to return
Door on either first or
second Floor
if ( getLocationName().equals( SECOND_FLOOR_NAME ) )
return getElevatorShaft().getSecondFloorDoor();
else
return null;
} // end method getDoor
2003 Prentice Hall, Inc.
All rights reserved.
49
50
51
52
53
54
55
56
57
58
59
60
61
// get ElevatorShaft reference
public ElevatorShaft getElevatorShaft()
{
return elevatorShaft;
}
// set ElevatorShaft reference
public void setElevatorShaft( ElevatorShaft shaft )
{
elevatorShaft = shaft;
}
}
Outline
Floor.java
Class Floor—a
subclass of
Location—
represents a Floor
across which a
Person walks to the
Elevator.
2003 Prentice Hall, Inc.
All rights reserved.
22
E.4
Class Door and ElevatorDoor
• Door
– Signals Person when to enter and exit Elevator
– Subclass ElevatorDoor
2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Door.java
// Sends DoorEvents to DoorListeners when opened or closed
package com.deitel.jhtp5.elevator.model;
Door listens for ElevatorMoveEvents;
// Java core packages
import java.util.*; when Elevator arrives, Door opens
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
public class Door
{
// represent whether Door is open or closed
private boolean open = false;
// time before Door closes automatically
public static final int AUTOMATIC_CLOSE_DELAY = 3000;
Outline
Door.java
Class Door, which
represents a Door in
the model, informs
listeners when a Door
has opened or closed.
Line 11
Lines 20 and 28
// Set of DoorListeners
private Set doorListeners;
// location where Door opened or closed
private Location doorLocation;
// Door constructor instantiates Set for DoorListeners
public Door()
{
doorListeners = new HashSet( 1 );
}
HashSet stores listener for
DoorEvents (i.e., when
Door opens and closes)
2003 Prentice Hall, Inc.
All rights reserved.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Outline
// add Door listener
public void addDoorListener( DoorListener listener )
{
Door.java
// prevent other objects from modifying doorListeners
Class Door, which
synchronized( doorListeners )
represents a Door in
{
Allow DoorListeners
to register
and
the model,
informs
doorListeners.add( listener );
unregister to receive
DoorEvents
}
listeners
when a Door
}
has opened or closed.
// remove Door listener
public void removeDoorListener( DoorListener listener )
{
// prevent other objects from modifying doorListeners
synchronized( doorListeners )
{
doorListeners.remove( listener );
}
}
Lines 32-49
Line 56
// open Door and send all listeners DoorEvent objects
public synchronized void openDoor( Location location )
{
if ( !open ) {
open = true;
Open Door if it is closed
2003 Prentice Hall, Inc.
All rights reserved.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// obtain iterator from Set
Iterator iterator;
synchronized( doorListeners )
{
iterator = new HashSet( doorListeners ).iterator();
}
// get next DoorListener
while ( iterator.hasNext() ) {
DoorListener doorListener =
( DoorListener ) iterator.next();
// send doorOpened event to this DoorListener
doorListener.doorOpened(
new DoorEvent( this, location ) );
Outline
Door.java
Class Door, which
represents a Door in
the model, informs
listeners when a Door
has opened or closed.
Notify DoorListeners
Lines 66-73
that Door has opened
}
Lines 78-95
doorLocation = location;
// declare Thread that ensures automatic Door closing
Thread closeThread = new Thread(
new Runnable() {
Use Thread to enable Door
to close itself automatically
after three seconds
public void run()
{
// close Door if open for more than 3 seconds
try {
Thread.sleep( AUTOMATIC_CLOSE_DELAY );
closeDoor( doorLocation );
}
2003 Prentice Hall, Inc.
All rights reserved.
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// handle exception if interrupted
catch ( InterruptedException exception ) {
exception.printStackTrace();
}
}
} // end anonymous inner class
);
closeThread.start();
}
// notify all waiting threads that the door has opened
notifyAll();
Outline
Door.java
Class Door, which
represents a Door in
the model, informs
listeners when a Door
has opened or closed.
Line 106
} // end method openDoor
// close Door and send all listeners DoorEvent objects
public synchronized void closeDoor( Location location )
{
if ( open ) {
Close Door if it is open
open = false;
// obtain iterator from Set
Iterator iterator;
synchronized( doorListeners )
{
iterator = new HashSet( doorListeners ).iterator();
}
2003 Prentice Hall, Inc.
All rights reserved.
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Outline
// get next DoorListener
while ( iterator.hasNext() ) {
DoorListener doorListener =
( DoorListener ) iterator.next();
// send doorClosed event to this DoorListener
doorListener.doorClosed(
new DoorEvent( this, location ) );
}
}
Door.java
Class Door, which
represents a Door in
Notify DoorListeners
the model, informs
that Door has closed
listeners when a Door
has opened or closed.
} // end method closeDoor
Lines 125-126
// return whether Door is open or closed
public synchronized boolean isDoorOpen()
{
return open;
}
Lines 133-136
When Elevator
arrives, open Door
}
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ElevatorDoor.java
// Opens and closes floor Door when elevator arrives and departs.
package com.deitel.jhtp5.elevator.model;
Outline
ElevatorDoor.ja
va
Class
ElevatorDoor extends
Door and
ElevatorDoor
// Deitel packages
implements ElevatorMoveListener
import com.deitel.jhtp5.elevator.event.*;
opens in response to
elevatorArrived
Override method
openDoor
public class ElevatorDoor extends Door implements ElevatorMoveListener
{
messages and opens
and closes the Floor
// open ElevatorDoor and corresponding Floor Door
public synchronized void openDoor( Location location )
Doors.
// Java core packages
import java.util.*;
{
location.getDoor().openDoor( location );
Line 11
super.openDoor( location );
Lines 14-20
} // end method openDoor
2003 Prentice Hall, Inc.
All rights reserved.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// close ElevatorDoor and Corresponding Floor Door
public synchronized void closeDoor( Location location )
{
location.getDoor().closeDoor( location );
Outline
Override method closeDoor
ElevatorDoor.ja
super.closeDoor( location );
} // end method closeDoor
// invoked when Elevator has departed
public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {}
// invoked when Elevator has arrived
public void elevatorArrived( ElevatorMoveEvent moveEvent )
{
openDoor( moveEvent.getLocation() );
}
va
Class
ElevatorDoor
opens in response to
elevatorArrived
messages and opens
and closes the Floor
Doors.
Lines 23-29
}
2003 Prentice Hall, Inc.
All rights reserved.
30
E.5
Class Button
• Button
– Signals Elevator to move between Floors
2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Button.java
// Sends ButtonEvents to ButtonListeners when accessed
package com.deitel.jhtp5.elevator.model;
Outline
Button listens for ElevatorMoveEvents;
Button.java
when Elevator arrives, Button
Class Button,
resets which
represents a Button
in the model, informs
public class Button implements ElevatorMoveListener {
listeners when a
// ButtonListener
Button has been
private ButtonListener buttonListener = null;
pressed or reset.
// represent whether Button is pressed
Allow ButtonListener to
private boolean pressed = false;
8
listen for Line
ButtonEvents
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
// set listener
public void setButtonListener( ButtonListener listener )
{
buttonListener = listener;
}
// press Button and send ButtonEvent
public void pressButton( Location location )
{
pressed = true;
buttonListener.buttonPressed(
new ButtonEvent( this, location ) );
Lines 11 and 17-20
Lines 23-29
Press Button and notify
ButtonListener that
Button has been pressed
}
2003 Prentice Hall, Inc.
All rights reserved.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// reset Button and send ButtonEvent
public void resetButton( Location location )
{
pressed = false;
buttonListener.buttonReset(
new ButtonEvent( this, location ) );
}
// return whether button is pressed
public boolean isButtonPressed()
{
return pressed;
}
// invoked when Elevator has departed
public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {}
// invoked when Elevator has arrived
public void elevatorArrived( ElevatorMoveEvent moveEvent )
{
resetButton( moveEvent.getLocation() );
}
Outline
Reset Button and notify
ButtonListener that
Button.java
Button
has been reset
Class Button, which
represents a Button
in the model, informs
listeners when a
Button has been
pressed or reset.
Lines 32-38
Lines 50-53
When Elevator
arrives, reset Button
}
2003 Prentice Hall, Inc.
All rights reserved.
33
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// ElevatorShaft.java
// Represents elevator shaft, which contains elevator
package com.deitel.jhtp5.elevator.model;
Outline
ElevatorShaft.j
ElevatorShaft listens
ava for
ElevatorMoveEvents
Class
LightEvents and BellEvents
ElevatorShaft,
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
which represents the
ElevatorShaft,
public class ElevatorShaft implements ElevatorMoveListener,
which sends events
LightListener, BellListener {
from the Elevator
// Elevator
to the
private Elevator elevator;
ElevatorSimulat
ion.
// Java core packages
import java.util.*;
// Buttons on Floors
private Button firstFloorButton;
private Button secondFloorButton;
// Doors on Floors
private Door firstFloorDoor;
private Door secondFloorDoor;
Lines 11-12
Lines 18-27
Use class diagram to determine
associations of ElevatorShaft
// Lights on Floors
private Light firstFloorLight;
private Light secondFloorLight;
2003 Prentice Hall, Inc.
All rights reserved.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Declare listeners that receive events
Outline
from model (and will send these
events to ElevatorModel)
ElevatorShaft.j
ava
Class
ElevatorShaft,
// constructor initializes aggregated components
public ElevatorShaft( Floor firstFloor, Floor secondFloor )
which represents the
{
ElevatorShaft,
// instantiate Set for ElevatorMoveListeners
which sends events
elevatorMoveListeners = new HashSet( 1 );
from the Elevator
// anonymous inner class listens for ButtonEvents
to the
Instantiate anonymous inner
class
ButtonListener floorButtonListener =
ElevatorSimulat
new ButtonListener() {
to listen for ButtonEvents
from
ion.
Buttons on floors
// listeners
private DoorListener doorListener;
private ButtonListener buttonListener;
private LightListener lightListener;
private BellListener bellListener;
private Set elevatorMoveListeners;
// called when Floor Button has been pressed
public void buttonPressed( ButtonEvent buttonEvent )
{
// request elevator move to location
When Button on floor is
Location location = buttonEvent.getLocation();
pressed, request Elevator to
buttonListener.buttonPressed( buttonEvent );
move to Floor of request
elevator.requestElevator( location );
}
Lines 30-34
Lines 43-70
Lines 47-53
2003 Prentice Hall, Inc.
All rights reserved.
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// called when Floor Button has been reset
public void buttonReset( ButtonEvent buttonEvent )
{
When Button
on floor
buttonListener.buttonReset( buttonEvent
);
}
is reset, reset Button
}; // end anonymous inner class
Outline
ElevatorShaft.j
ava
Class
ElevatorShaft,
// instantiate Floor Buttons
firstFloorButton = new Button();
which represents the
secondFloorButton = new Button();
ElevatorShaft,
which sends events
// register anonymous ButtonListener with Floor Buttons
from the Elevator
firstFloorButton.setButtonListener(
floorButtonListener );
to the
secondFloorButton.setButtonListener(
ElevatorSimulat
floorButtonListener );
ion.
Instantiate anonymous inner
// Floor Buttons listen for ElevatorMoveEvents
class to listen for DoorEvents
Lines 56-59
addElevatorMoveListener( firstFloorButton );
from
Doors
on
floors
addElevatorMoveListener( secondFloorButton );
// anonymous inner class listens for DoorEvents
DoorListener floorDoorListener = new DoorListener() {
// called when Floor Door has opened
public void doorOpened( DoorEvent doorEvent )
{
// forward event to doorListener
doorListener.doorOpened( doorEvent );
}
Lines 77-100
Lines 80-91
When Door on floor is opened or
closed, forward DoorEvent to
DoorListener
2003 Prentice Hall, Inc.
All rights reserved.
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Outline
// called when Floor Door has closed
public void doorClosed( DoorEvent doorEvent )
{
// forward event to doorListener
doorListener.doorClosed( doorEvent );
}
}; // end anonymous inner class
ElevatorShaft.j
ava
Class
ElevatorShaft,
which represents the
// instantiate Floor Doors
ElevatorShaft,
firstFloorDoor = new Door();
which sends events
secondFloorDoor = new Door();
from the Elevator
// register anonymous DoorListener with Floor Doors
to the
firstFloorDoor.addDoorListener( floorDoorListener );
ElevatorSimulat
secondFloorDoor.addDoorListener( floorDoorListener );Instantiate Lights and
ion.
listen for LightEvents
// instantiate Lights, then listen for LightEvents
firstFloorLight = new Light();
addElevatorMoveListener( firstFloorLight );
firstFloorLight.setLightListener( this );
secondFloorLight = new Light();
addElevatorMoveListener( secondFloorLight );
secondFloorLight.setLightListener( this );
// instantiate Elevator object
elevator = new Elevator( firstFloor, secondFloor );
Lines 103-105
Lines 112-115
Instantiate Elevator
and listen for
ElevatorMoveEvents
2003 Prentice Hall, Inc.
All rights reserved.
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// register for ElevatorMoveEvents from elevator
elevator.addElevatorMoveListener( this );
// listen for BellEvents from elevator
elevator.setBellListener( this );
Outline
Listen for BellEvents
from elevator ElevatorShaft.j
// anonymous inner class listens for ButtonEvents from
// elevator
elevator.setButtonListener(
new ButtonListener() {
// invoked when button has been pressed
public void buttonPressed( ButtonEvent buttonEvent )
{
// send event to listener
buttonListener.buttonPressed( buttonEvent );
}
// invoked when button has been reset
public void buttonReset( ButtonEvent buttonEvent )
{
// send event to listener
buttonListener.buttonReset(
new ButtonEvent( this, elevator ) );
}
} // end anonymous inner class
ava
Class
ElevatorShaft,
Instantiate
anonymous
which
represents
inner class
to listen
for the
ElevatorShaft,
ButtonEvents
from
whichButton
sends events
Elevator’s
from the Elevator
to the
When Elevator’s
ElevatorSimulat
Buttonion.
is pressed
or reset, forward
ButtonEvent
Line 118 to
ButtonListener
Lines 122-140
Lines 126-138
);
2003 Prentice Hall, Inc.
All rights reserved.
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// anonymous inner class listens for DoorEvents from
// elevator
elevator.setDoorListener(
new DoorListener() {
// invoked when door has opened
public void doorOpened( DoorEvent doorEvent )
{
// send event to listener
doorListener.doorOpened( doorEvent );
}
// invoked when door has closed
public void doorClosed( DoorEvent doorEvent )
{
// send event to listener
doorListener.doorClosed( doorEvent );
}
} // end anonymous inner class
Outline
Instantiate anonymous
inner class to listen for
ElevatorShaft.j
DoorEvents
from
ava Door
Elevator’s
Class
ElevatorShaft,
which represents the
When Elevator’s
ElevatorShaft,
Door is pressed
or
which sends events
reset, forward
from
DoorEvent
to the Elevator
to the
DoorListener
ElevatorSimulat
ion.
Lines 144-161
);
// start Elevator Thread
elevator.start();
Lines 148-159
Start Elevator’s Thread
Line 164
} // end ElevatorShaft constructor
Lines 169-172
// get Elevator
public Elevator getElevator()
{
return elevator;
}
Get method for Elevator
2003 Prentice Hall, Inc.
All rights reserved.
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// get Door on first Floor
public Door getFirstFloorDoor()
{
return firstFloorDoor;
}
// get Door on second Floor
public Door getSecondFloorDoor()
{
return secondFloorDoor;
}
// get Button on first Floor
public Button getFirstFloorButton()
{
return firstFloorButton;
}
Outline
ElevatorShaft.j
ava
Class
ElevatorShaft,
which represents the
ElevatorShaft,
which sends events
from the Elevator
to the
ElevatorSimulat
ion.
Get methods for
Doors,
Buttons and Lights
Lines 175-208
// get Button on second Floor
public Button getSecondFloorButton()
{
return secondFloorButton;
}
// get Light on first Floor
public Light getFirstFloorLight()
{
return firstFloorLight;
}
2003 Prentice Hall, Inc.
All rights reserved.
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// get Light on second Floor
public Light getSecondFloorLight()
{
return secondFloorLight;
}
// invoked when Bell rings
public void bellRang( BellEvent bellEvent )
{
bellListener.bellRang( bellEvent );
}
// invoked when Light turns on
public void lightTurnedOn( LightEvent lightEvent )
{
lightListener.lightTurnedOn( lightEvent );
}
// invoked when Light turns off
public void lightTurnedOff( LightEvent lightEvent )
{
lightListener.lightTurnedOff( lightEvent );
}
Outline
ElevatorShaft.j
ava
Class
ElevatorShaft,
When Bell has rung, forward
which represents the
BellEvent to BellListener
ElevatorShaft,
which sends events
from the Elevator
to the
ElevatorSimulat
ion.
When Lights
turn
on or off, forward
Lines
LightEvent
to 211-214
LightListener
Lines 217-226
When Elevator has departed, notify
ElevatorMoveListeners
Lines 229-243
// invoked when Elevator departs
public void elevatorDeparted( ElevatorMoveEvent moveEvent )
{
Iterator iterator = elevatorMoveListeners.iterator();
2003 Prentice Hall, Inc.
All rights reserved.
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
Outline
// iterate Set of ElevatorMoveEvent listeners
while ( iterator.hasNext() ) {
// get respective ElevatorMoveListener from Set
ElevatorMoveListener listener =
( ElevatorMoveListener ) iterator.next();
// send ElevatorMoveEvent to this listener
listener.elevatorDeparted( moveEvent );
}
} // end method elevatorDeparted
// invoked when Elevator arrives
public void elevatorArrived( ElevatorMoveEvent moveEvent )
{
// obtain iterator from Set
Iterator iterator = elevatorMoveListeners.iterator();
// get next DoorListener
while ( iterator.hasNext() ) {
ElevatorShaft.j
ava
Class
ElevatorShaft,
which represents the
ElevatorShaft,
which sends events
from the Elevator
to the
ElevatorSimulat
ion.
When Elevator has arrived, notify
Lines 246-262
ElevatorMoveListeners
// get next ElevatorMoveListener from Set
ElevatorMoveListener listener =
( ElevatorMoveListener ) iterator.next();
// send ElevatorMoveEvent to this listener
listener.elevatorArrived( moveEvent );
} // end while loop
} // end method elevatorArrived
2003 Prentice Hall, Inc.
All rights reserved.
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
Outline
// set listener to DoorEvents
public void setDoorListener( DoorListener listener )
{
ElevatorShaft.j
doorListener = listener;
Enable DoorListener to receive
ava
}
DoorEvents from ElevatorShaft
Class
// set listener to ButtonEvents
ElevatorShaft,
public void setButtonListener( ButtonListener listener )
which represents the
{
ElevatorShaft,
buttonListener = listener;
Enable ButtonListener to receive
}
which sends events
ButtonEvents from ElevatorShaft
from the Elevator
// add listener to ElevatorMoveEvents
Enable ElevatorMoveListeners
to the
public void addElevatorMoveListener(
to receive ElevatorMoveEvents
ElevatorSimulat
ElevatorMoveListener listener )
from ElevatorShaft
ion.
{
elevatorMoveListeners.add( listener );
}
Lines 265-268
// set listener to LightEvents
Lines 271-274
public void setLightListener( LightListener listener )
{
lightListener = listener;
Enable BellListener to receive
Lines 277-281
Enable
LightListener
to
receive
}
BellEvents from ElevatorShaft
LightEvents from ElevatorShaft
Lines 284-287
// set listener to BellEvents
public void setBellListener( BellListener listener )
{
bellListener = listener;
}
}
Lines 290-293
2003 Prentice Hall, Inc.
All rights reserved.
44
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Light.java
// Light turns a light on or off
package com.deitel.jhtp5.elevator.model;
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
public class Light implements ElevatorMoveListener {
// Light state (on/off)
private boolean lightOn;
// time before Light turns off automatically (3 seconds)
public static final int AUTOMATIC_TURNOFF_DELAY = 3000;
Outline
Light.java
Class Light
represents a Light
on the Floor in the
model.
Light turns itself
offLine
automatically
14
after three seconds
Lines 23-26
// LightListener listens for when Light should turn on/off
private LightListener lightListener;
// location where Light turned on or off
private Location lightLocation;
Enable LightListener
to receive LightEvents
// set LightListener
public void setLightListener( LightListener listener )
{
lightListener = listener;
}
2003 Prentice Hall, Inc.
All rights reserved.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// turn on Light
public void turnOnLight( Location location )
{
if ( !lightOn ) {
lightOn = true;
// send LightEvent to LightListener
lightListener.lightTurnedOn(
new LightEvent( this, location ) );
Outline
Method to turn on Light
Light.java
Class Light
represents a Light
on the Floor in the
Send LightEvent to
model.
LightListener
(ElevatorShaft)
Lines 29-63
when Light turns on
lightLocation = location;
Lines 36-37
// declare Thread that ensures automatic Light turn off
Thread thread = new Thread(
Instantiate
new Runnable() {
and start Lines
Thread
42-61
that turns Light off
automatically after three seconds
public void run()
{
// turn off Light if on for more than 3 seconds
try {
Thread.sleep( AUTOMATIC_TURNOFF_DELAY );
turnOffLight( lightLocation );
}
2003 Prentice Hall, Inc.
All rights reserved.
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Outline
// handle exception if interrupted
catch ( InterruptedException exception ) {
exception.printStackTrace();
}
Light.java
Class Light
represents a Light
on the Floor in the
model.
}
} // end anonymous inner class
);
thread.start();
}
} // end method turnOnLight
// turn off Light
public void turnOffLight( Location location )
{
if ( lightOn ) {
Lines 66-76
Lines
73-74
Method to turn
off Light
lightOn = false;
// send LightEvent to LightListener
lightListener.lightTurnedOff(
new LightEvent( this, location ) );
}
} // end method turnOffLight
Send LightEvent to
LightListener
(ElevatorShaft)
when Light turns off
2003 Prentice Hall, Inc.
All rights reserved.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// return whether Light is on or off
public boolean isLightOn()
{
return lightOn;
}
// invoked when Elevator has departed
public void elevatorDeparted(
ElevatorMoveEvent moveEvent )
{
turnOffLight( moveEvent.getLocation() );
}
// invoked when Elevator has arrived
public void elevatorArrived(
ElevatorMoveEvent moveEvent )
{
turnOnLight( moveEvent.getLocation() );
}
Outline
Light.java
Class Light
represents a Light
on the Floor in the
When Elevator departs
model.
from Floor, turn off Light
Lines 85-89
Lines 92-96
When Elevator arrives at
Floor, turn off Light
}
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Bell.java
// Represents Bell in simulation
package com.deitel.jhtp5.elevator.model;
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
public class Bell implements ElevatorMoveListener {
// BellListener listens for BellEvent object
private BellListener bellListener;
// ring bell and send BellEvent object to listener
private void ringBell( Location location )
{
if ( bellListener != null )
bellListener.bellRang(
new BellEvent( this, location ) );
}
// set BellListener
public void setBellListener( BellListener listener )
{
bellListener = listener;
}
Outline
Bell.java
Class Bell represents
the Bell in the
model.
Lines 17-18
Send BellEvent to
BellListener
Lines 22-25
(ElevatorShaft)
when Bell has rung
Enable BellListener to
receive BellEvents
2003 Prentice Hall, Inc.
All rights reserved.
26
27
28
29
30
31
32
33
34
35
// invoked when Elevator has departed
public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {}
// invoked when Elevator has arrived
public void elevatorArrived( ElevatorMoveEvent moveEvent )
{
ringBell( moveEvent.getLocation() );
}
}
Outline
When Elevator arrives
at Floor,Bell.java
ring Bell
Class Bell represents
the Bell in the
model.
Lines 31-34
2003 Prentice Hall, Inc.
All rights reserved.
51
E.8
Class Elevator
• Elevator
– Travels in ElevatorShaft between Floors
– Carries Person
– “Is a” Location
2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Elevator.java
// Travels between Floors in the ElevatorShaft
package com.deitel.jhtp5.elevator.model;
Outline
Elevator.java
Class Elevator
represents the
traveling
// Deitel packages
Class Elevator “is Elevator
a” Location
import com.deitel.jhtp5.elevator.event.*;
between two Floors,
import com.deitel.jhtp5.elevator.ElevatorConstants;
operating
asynchronously with
public class Elevator extends Location implements Runnable,
other objects.
BellListener, ElevatorConstants {
// Java core packages
import java.util.*;
// manages Elevator thread
private boolean elevatorRunning = false;
// describes Elevator state (idle or moving)
private boolean moving = false;
// current Floor
private Location currentFloorLocation;
// destination Floor
private Location destinationFloorLocation;
Line 12
Lines 19-28
Use class diagram to
determine associations and
attributes of Elevator
// Elevator needs to service other Floor
private boolean summoned;
2003 Prentice Hall, Inc.
All rights reserved.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// listener objects
private Set elevatorMoveListeners;
private BellListener bellListener;
private ButtonListener elevatorButtonListener;
private DoorListener elevatorDoorListener;
// ElevatorDoor, Button and Bell on Elevator
private ElevatorDoor elevatorDoor;
private Button elevatorButton;
private Bell bell;
public static final int ONE_SECOND = 1000;
Outline
Declare listeners that receive events
from model (and will send these
events to ElevatorShaft)
Elevator.java
Class Elevator
represents the
Use class diagram to
Elevator traveling
determine associations and
between two Floors,
attributes of Elevator
operating
asynchronously with
other objects.
// time needed to travel between Floors (5 seconds)
private static final int TRAVEL_TIME = 5 * ONE_SECOND;
Lines 31-34
// Elevator's thread to handle asynchronous movement
private Thread thread;
Lines 37-39, 44
// constructor creates variables; registers for ButtonEvents
public Elevator( Floor firstFloor, Floor secondFloor )
{
setLocationName( ELEVATOR_NAME );
// instantiate Elevator's Door, Button and Bell
elevatorDoor = new ElevatorDoor();
elevatorButton = new Button();
bell = new Bell();
Lines 55-57
Instantiate ElevatorDoor, Button
and Bell inside Elevator
2003 Prentice Hall, Inc.
All rights reserved.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Outline
// register Elevator for BellEvents
bell.setBellListener( this );
// instantiate listener Set
elevatorMoveListeners = new HashSet( 1 );
// start Elevator on first Floor
currentFloorLocation = firstFloor;
destinationFloorLocation = secondFloor;
// register elevatorButton for ElevatorMoveEvents
addElevatorMoveListener( elevatorButton );
// register elevatorDoor for ElevatorMoveEvents
addElevatorMoveListener( elevatorDoor );
// register bell for ElevatorMoveEvents
addElevatorMoveListener( bell );
Listen for BellEvents
Elevator.java
Class Elevator
represents the
Elevator traveling
between two Floors,
operating
asynchronously with
other objects.
Line 60
Lines 81-101
// anonymous inner class listens for ButtonEvents from
// elevatorButton
Instantiate anonymous inner class
elevatorButton.setButtonListener(
to listen for ButtonEvents
new ButtonListener() {
from Elevator’s Button
2003 Prentice Hall, Inc.
All rights reserved.
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Outline
// invoked when elevatorButton has been pressed
public void buttonPressed( ButtonEvent buttonEvent )
{
// send ButtonEvent to listener
When Elevator’s Elevator.java
elevatorButtonListener.buttonPressed(
Button is pressed, forward
Class Elevator
buttonEvent );
ButtonEvent to represents the
andElevator traveling
// start moving Elevator to destination ButtonListener
Floor
setMoving( true );
signal Elevator to move
between two Floors,
}
to other Floor
operating
asynchronously with
// invoked when elevatorButton has been reset
other objects.
public void buttonReset( ButtonEvent buttonEvent )
When Elevator’s
{
Button is reset,
// send ButtonEvent to listener
Lines 84-92
forward ButtonEvent
elevatorButtonListener.buttonReset(
buttonEvent );
to ButtonListener
Lines 95-100
}
} // end anonymous inner class
);
// anonymous inner class listens for DoorEvents from
// elevatorDoor
elevatorDoor.addDoorListener(
Instantiate anonymous
new DoorListener() {
DoorEvents from
Lines 107-124
inner class to listen for
Elevator’s Door
2003 Prentice Hall, Inc.
All rights reserved.
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// invoked when elevatorDoor has opened
public void doorOpened( DoorEvent doorEvent )
{
// send DoorEvent to listener
elevatorDoorListener.doorOpened( new DoorEvent(
doorEvent.getSource(), Elevator.this ));
}
// invoked when elevatorDoor has closed
public void doorClosed( DoorEvent doorEvent )
{
// send DoorEvent to listener
elevatorDoorListener.doorClosed( new DoorEvent(
doorEvent.getSource(), Elevator.this ));
}
} // end anonymous inner class
);
} // end Elevator constructor
// swaps current Floor Location with opposite Floor Location
private void changeFloors()
{
Location location = currentFloorLocation;
currentFloorLocation = destinationFloorLocation;
destinationFloorLocation = location;‘
}
Outline
When Elevator’s
Door
is opened or
Elevator.java
closed,
or close
Classopen
Elevator
Door
on Floor
represents
the
(Location),
and
Elevator traveling
sendbetween
DoorEvent
to
two Floors,
DoorListener
operating
asynchronously with
other objects.
Lines 110-123
Lines 129-134
private method for changing
destination Location
2003 Prentice Hall, Inc.
All rights reserved.
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// start Elevator thread
public void start()
{
if ( thread == null )
thread = new Thread( this );
elevatorRunning = true;
thread.start();
}
// stop Elevator thread; method run terminates
public void stopElevator()
{
elevatorRunning = false;
}
// Elevator thread's run method
public void run()
{
while ( isElevatorRunning() ) {
// remain idle until awoken
while ( !isMoving() )
pauseThread( 10 );
Outline
Start Elevator’s Thread
Elevator.java
Class Elevator
represents the
Elevator traveling
between two Floors,
operating
asynchronously with
otherThread
objects.
Stop Elevator’s
Lines 137-144
Lines 147-150
Invoked after ElevatorShaft
invokes Elevator’s Start
Lines method
153-187
Lines 178-179
When Elevator maintains “waiting
state,” the Elevator should not move
// pause while passenger exits (if one exists)
pauseThread( ONE_SECOND );
2003 Prentice Hall, Inc.
All rights reserved.
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// close elevatorDoor
getDoor().closeDoor( currentFloorLocation );
// closing Door takes one second
pauseThread( ONE_SECOND );
// issue elevatorDeparted Event
sendDepartureEvent( currentFloorLocation );
// Elevator needs 5 seconds to travel
pauseThread( TRAVEL_TIME );
// stop Elevator
setMoving( false );
// swap Floor Locations
changeFloors();
// issue elevatorArrived Event
sendArrivalEvent( currentFloorLocation );
} // end while loop
} // end method run
Outline
When ElevatorElevator.java
maintains “moving
Class
Elevator
state,” close Elevator
Door
represents the
Elevator traveling
between
Notify listeners
that two Floors,
operating
Elevator has
departed
asynchronously with
other objects.
Travel five seconds, then stop
Line 165
Line 171
Change destination Floor
Lines 174-177
Notify listeners that
180
Elevator hasLine
arrived
Line 183
2003 Prentice Hall, Inc.
All rights reserved.
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// pause concurrent thread for number of milliseconds
private void pauseThread( int milliseconds )
{
try {
Thread.sleep( milliseconds );
}
// handle if interrupted while sleeping
catch ( InterruptedException exception ) {
exception.printStackTrace();
}
} // end method pauseThread
// return Button on Elevator
public Button getButton()
{
return elevatorButton;
}
// return Door on Elevator
public Door getDoor()
{
return elevatorDoor;
}
Private method for putting
Outline
Elevator’s Thread to sleep
Elevator.java
Class Elevator
represents the
Elevator traveling
between two Floors,
operating
asynchronously with
other objects.
Implement Location
method getButton to
Lines
190-200
return Elevator’s
Button
Lines 203-206
Implement Location
Lines 209-212
method getDoor to return
Elevator’s Door
// set if Elevator should move
private void setMoving( boolean elevatorMoving )
{
moving = elevatorMoving;
}
2003 Prentice Hall, Inc.
All rights reserved.
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// is Elevator moving?
public boolean isMoving()
{
return moving;
}
// is Elevator thread running?
private boolean isElevatorRunning()
{
return elevatorRunning;
}
Outline
Elevator.java
Class Elevator
represents the
Elevator traveling
between two Floors,
operating
asynchronously with
other objects.
// register ElevatorMoveListener for ElevatorMoveEvents
public void addElevatorMoveListener(
Lines 233-237
ElevatorMoveListener listener )
Enable ElevatorMoveListeners
{
to receive ElevatorMoveEvents
Lines 240-243
elevatorMoveListeners.add( listener );
}
// register BellListener fpr BellEvents
public void setBellListener( BellListener listener )
{
bellListener = listener;
}
Enable BellListener
to receive BellEvents
2003 Prentice Hall, Inc.
All rights reserved.
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// register ButtonListener for ButtonEvents
public void setButtonListener( ButtonListener listener )
{
Enable ButtonListener
elevatorButtonListener = listener;
receive ButtonEvents
}
// register DoorListener for DoorEvents
public void setDoorListener( DoorListener listener )
{
elevatorDoorListener = listener;
}
// notify all ElevatorMoveListeners of arrival
private void sendArrivalEvent( Location location )
{
// obtain iterator from Set
Iterator iterator = elevatorMoveListeners.iterator();
// get next DoorListener
while ( iterator.hasNext() ) {
Outline
to
Elevator.java
Class Elevator
Enable DoorListener
represents the
to receive DoorEvents
Elevator traveling
between two Floors,
operating
Private method asynchronously
for notifying all with
ElevatorMoveListeners
other objects.that
Elevator has arrived
Lines 246-249
Lines 252-255
Lines 258-283
// get next ElevatorMoveListener from Set
ElevatorMoveListener listener =
( ElevatorMoveListener ) iterator.next();
// send event to listener
listener.elevatorArrived( new
ElevatorMoveEvent( this, location ) );
} // end while loop
2003 Prentice Hall, Inc.
All rights reserved.
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// service queued request, if one exists
if ( summoned ) {
setMoving( true ); // start moving Elevator
}
summoned = false; // request has been serviced
} // end method sendArrivalEvent
If a queued request exists,
Outline
service that request (i.e.,
move to other Floor)
Elevator.java
Class Elevator
Private method for notifying
allthe
represents
ElevatorMoveListeners
that traveling
Elevator
Elevator has departed
between two Floors,
operating
asynchronously with
other objects.
// notify all ElevatorMoveListeners of departure
private void sendDepartureEvent( Location location )
{
// obtain iterator from Set
Iterator iterator = elevatorMoveListeners.iterator();
Lines 277-279
// get next DoorListener
while ( iterator.hasNext() ) {
Lines 286-303
// get next ElevatorMoveListener from Set
ElevatorMoveListener listener =
( ElevatorMoveListener ) iterator.next();
// send ElevatorMoveEvent to this listener
listener.elevatorDeparted( new ElevatorMoveEvent(
this, currentFloorLocation ) );
} // end while loop
} // end method sendDepartureEvent
2003 Prentice Hall, Inc.
All rights reserved.
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// request Elevator
public void requestElevator( Location location )
{
// if Elevator is idle
if ( !isMoving() ) {
Public service to request
Outline
Elevator (used by Buttons)
Elevator.java
Elevator
If Elevator is idle andClass
servicing
represents
the Floor of the request,
send the
// if Elevator is on same Floor of request
Elevator
traveling
if ( location == currentFloorLocation )
elevatorArrived
event
between two Floors,
// Elevator has already arrived; send arrival event
operating
sendArrivalEvent( currentFloorLocation );
asynchronously with
If Elevator is idle and
other objects.
// if Elevator is on opposite Floor of request
servicing opposite Floor of the
else {
request, move to other Floor
setMoving( true ); // move to other Floor
Lines 306-332
}
}
else // if Elevator is moving
Line 315
// if Elevator departed from same Floor as request
if ( location == currentFloorLocation )
summoned = true;
If Person
// if Elevator is traveling to Floor of
// simply continue traveling
Line 319
requests Elevator
just after
Line 325-326
Elevator has left the Floor on which
request,
the Person is waiting, Elevator must
“remember” to return to that Floor
} // end method requestElevator
2003 Prentice Hall, Inc.
All rights reserved.
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// invoked when bell has rung
public void bellRang( BellEvent bellEvent )
{
// send event to bellLirdstener
if ( bellListener != null )
bellListener.bellRang( bellEvent );
}
// get the currentFloorLocation of the Elevator
public Location getCurrentFloor()
{
return currentFloorLocation;
}
}
Outline
Notify BellListener
that Bell has rung
Elevator.java
Class Elevator
represents the
Elevator traveling
between two Floors,
operating
asynchronously with
other objects.
Lines 334-339
2003 Prentice Hall, Inc.
All rights reserved.
65
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Person.java
// Person riding the elevator
package com.deitel.jhtp5.elevator.model;
// Java core packages
import java.util.*;
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
public class Person extends Thread {
// identification number
private int ID = -1;
// represents whether Person is moving or waiting
private boolean moving;
// reference to Location (either on Floor or in Elevator)
private Location location;
// listener object for PersonMoveEvents
private PersonMoveListener personMoveListener;
// time in milliseconds to walk to Button on Floor
private static final int TIME_TO_WALK = 3000;
Outline
Person.java
Class Person
represents the
Person that rides the
Elevator. The
Person operates
asynchronously with
other objects.
Use class diagram to
determine associations and
Lines 14-20
attributes of Person
Line 23
Lines 26-34
Declare listener that
receives
PersonMoveEvent
Define constants that
indicate each type of event
that a Person may send
2003 Prentice Hall, Inc.
All rights reserved.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// types of messages Person may send
public static final int PERSON_CREATED = 1;
public static final int PERSON_ARRIVED = 2;
public static final int PERSON_ENTERING_ELEVATOR = 3;
public static final int PERSON_PRESSING_BUTTON = 4;
public static final int PERSON_EXITING_ELEVATOR = 5;
public static final int PERSON_EXITED = 6;
Outline
// Person constructor set initial location
public Person( int identifier, Location initialLocation )
{
super();
ID = identifier; // assign unique identifier
location = initialLocation; // set Floor Location
moving = true; // start moving toward Button on Floor
Person.java
Person constructor
Person
assigns uniqueClass
identifier
represents
and sets initial Floor
on the
which PersonPerson
is locatedthat rides the
Elevator. The
Person operates
asynchronously with
other objects.
Lines 37-44
}
// set listener for PersonMoveEvents
public void setPersonMoveListener(
PersonMoveListener listener )
{
personMoveListener = listener;
}
// set Person Location
private void setLocation( Location newLocation )
{
location = newLocation;
}
Lines 47-51
Enable PersonMoveListener
to receive PersonMoveEvents
Lines 54-57
When Door opens, set
Person’s Location to that
of where the Door opened
2003 Prentice Hall, Inc.
All rights reserved.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// get current Location
private Location getLocation()
{
return location;
}
// get identifier
public int getID()
{
return ID;
}
// set if Person should move
public void setMoving( boolean personMoving )
{
moving = personMoving;
}
// get if Person should move
public boolean isMoving()
{
return moving;
}
// Person either rides or waits for Elevator
public void run()
{
// indicate that Person thread was created
sendPersonMoveEvent( PERSON_CREATED );
Outline
Person.java
Class Person
represents the
Person that rides the
Elevator. The
Person operates
asynchronously with
other objects.
Lines 84-204
Line 87
Invoked when Person’s
Thread is started
Notify listeners when
Person is created
2003 Prentice Hall, Inc.
All rights reserved.
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// walk to Elevator
pauseThread( TIME_TO_WALK );
// stop walking at Elevator
setMoving( false );
// Person arrived at Elevator
sendPersonMoveEvent( PERSON_ARRIVED );
// get Door on current Floor
Door currentFloorDoor = location.getDoor();
Outline
Put Person Thread to sleep
for
three seconds, simulating a three
second walk to the Elevator
Person.java
Class Person
represents the
Person that rides the
Elevator.
Notify listeners when
PersonThe
Person operates
arrived at Elevator
asynchronously with
other objects.
// get Elevator
Elevator elevator =
( (Floor) getLocation() ).getElevatorShaft().getElevator();
// begin exclusive access to currentFloorDoor
synchronized ( currentFloorDoor ) {
// check whether Floor Door is open
if ( !currentFloorDoor.isDoorOpen() ) {
Line 90
Line 96
If Door is closed, press
Button on Floor and wait
Lines 109-117
for Elevator to arrive
sendPersonMoveEvent( PERSON_PRESSING_BUTTON );
pauseThread( 1000 );
// press Floor's Button to request Elevator
Button floorButton = getLocation().getButton();
floorButton.pressButton( getLocation() );
}
2003 Prentice Hall, Inc.
All rights reserved.
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
Outline
// wait for Floor door to open
try {
Person.java
Class Person
Wait for the
represents the
}
currentFloorDoor to
Person that rides the
// handle exception waiting for Floor door to open
open
Elevator. The
catch ( InterruptedException interruptedException ) {
Person operates
interruptedException.printStackTrace();
asynchronously with
}
other objects.
while ( !currentFloorDoor.isDoorOpen() )
currentFloorDoor.wait();
// Floor Door takes one second to open
pauseThread( 1000 );
Lines 122-123
// implicitly wait for exclusive access to elevator
synchronized ( elevator ) {
Lines 135-158
Only one Person is allowed to
// Person enters Elevator
Line 138
occupy the Elevator at one time
sendPersonMoveEvent( PERSON_ENTERING_ELEVATOR );
Notify listeners when Person
LineElevator
141
entered
// set Person Location to Elevator
setLocation( elevator );
Set the Person’s
to the
Elevator
// Person takes one second to enter Elevator
location
pauseThread( 1000 );
2003 Prentice Hall, Inc.
All rights reserved.
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Outline
Notify listeners when
Person
pressed Button
Person.java
// get Elevator's Button
Class Person
Button elevatorButton = getLocation().getButton();
represents the
Person that rides the
// press Elevator's Button
Elevator. The
elevatorButton.pressButton( location );
Person
operates
Press the Elevator’s button
to
// Door closing takes one second
asynchronously with
instruct the Elevator to begin
pauseThread( 1000 );
other objects.
traveling
// pressing Elevator Button takes one second
sendPersonMoveEvent( PERSON_PRESSING_BUTTON );
pauseThread( 1000 );
}
} // give up exclusive access to Floor door
Line 147
// get exclusive access to Elevator
synchronized( elevator ) {
Line 154
// get Elevator door
Door elevatorDoor = getLocation().getDoor();
Lines 173-174
// wait for Elevator door to open
synchronized( elevatorDoor ) {
try {
while ( !elevatorDoor.isDoorOpen() )
elevatorDoor.wait();
Invoke method wait on the
elevatorDoor
}
2003 Prentice Hall, Inc.
All rights reserved.
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// handle exception waiting for Elevator door to open
catch ( InterruptedException interruptedException ) {
interruptedException.printStackTrace();
}
Outline
Person.java
Class Person
// waiting for Elevator's Door to open takes a second
represents the
pauseThread( 1000 );
Person that rides the
Elevator. The
// move Person onto Floor
Person operates
setLocation( elevator.getCurrentFloor() );
asynchronously with
Set
Person’s
new
location
to
Floor
// walk away from Elevator
other objects.
setMoving( true );
// Person exiting ElevatorPerson walks away from
Elevator);
sendPersonMoveEvent( PERSON_EXITING_ELEVATOR
the
Line 186
Line 189
} // release elevatorDoor lock, allowing door to close
} // release elevator lock, allowing waiting Person to enter
Line 202
// walking from elevator takes five seconds
pauseThread( 2 * TIME_TO_WALK );
// Person exits simulation
sendPersonMoveEvent( PERSON_EXITED );
} // end method run
Person left the simulation
2003 Prentice Hall, Inc.
All rights reserved.
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// pause thread for desired number of milliseconds
private void pauseThread( int milliseconds )
{
try {
sleep( milliseconds );
}
Outline
Utility method for putting
Person’s Thread to sleep
Person.java
Class Person
represents the
Person that rides the
// handle exception if interrupted when paused
Elevator. The
catch ( InterruptedException interruptedException ) {
Person operates
interruptedException.printStackTrace();
asynchronously with
}
other objects.
} // end method pauseThread
Utility method for
determining which
// send PersonMoveEvent to listener, depending on event type
Lines 207-217
PersonMoveEvent
private void sendPersonMoveEvent( int eventType )
{
to send to listener, then
Lines 220-262
// create new event
sending that event
PersonMoveEvent event =
new PersonMoveEvent( this, getLocation(), getID() );
// send Event to this listener, depending on eventType
switch ( eventType ) {
// Person has been created
case PERSON_CREATED:
personMoveListener.personCreated( event );
break;
2003 Prentice Hall, Inc.
All rights reserved.
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Person arrived at Elevator
case PERSON_ARRIVED:
personMoveListener.personArrived( event );
break;
// Person entered Elevator
case PERSON_ENTERING_ELEVATOR:
personMoveListener.personEntered( event );
break;
// Person pressed Button object
case PERSON_PRESSING_BUTTON:
personMoveListener.personPressedButton( event );
break;
Outline
Person.java
Class Person
represents the
Person that rides the
Elevator. The
Person operates
asynchronously with
other objects.
// Person exited Elevator
case PERSON_EXITING_ELEVATOR:
personMoveListener.personDeparted( event );
break;
// Person exited simulation
case PERSON_EXITED:
personMoveListener.personExited( event );
break;
default:
break;
}
} // end method sendPersonMoveEvent
}
2003 Prentice Hall, Inc.
All rights reserved.
75
E.10 Artifacts 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.
76
Fig. E.15 Artifacts for package model.
model
<<file>>
<<file>>
Bell.java
Bell.java
<<file>>
Door.java
<<file>>
ElevatorSimulation.java
<<file>>
Floor.java
<<file>>
Location.java
<<file>>
Button.java
<<file>>
Elevator.java
<<file>>
ElevatorShaft.java
<<file>>
Light.java
<<file>>
Person.java
<<file>>
ElevatorDoor.java
<<imports>>
2003 Prentice Hall, Inc. All rights reserved.
event
77
E.11 Conclusion
• Object-oriented fundamentals and Java-specific
topics
– Event handling
– Multithreading
• Appendix F
– Implements the ElevatorView
2003 Prentice Hall, Inc. All rights reserved.
© Copyright 2026 Paperzz