Você está na página 1de 77

Appendix E – Elevator Model

Outline
E.1 Introduction
E.2 Class ElevatorSimulation
E.3 Classes Location and Floor
E.4 Class Door and ElevatorDoor
E.5 Class Button
E.6 Class ElevatorShaft
E.7 Classes Light and Bell
E.8 Class Elevator
E.9 Class Person
E.10 Artifacts Revisited
E.11 Conclusion

 2003 Prentice Hall, Inc. All rights reserved.


E.1 Introduction

• Classes implement the MVC model

 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.


1 // ElevatorSimulation.java Outline
2 // Elevator simulation model with ElevatorShaft and two Floors
3 package com.deitel.jhtp5.elevator.model;
4 ElevatorSimulat
5 // Java core packages ElevatorSimulation implements
ion.java
6 import java.util.*; ElevatorSimulationListener, which
7 Class
8 // Deitel packages
inherits from all listener interfaces
ElevatorSimulat
9 import com.deitel.jhtp5.elevator.event.*; ion represents the
10 import com.deitel.jhtp5.elevator.ElevatorConstants;
MVC model in our
11
12 public class ElevatorSimulation implements ElevatorSimulationListener,
elevator simulation.
13 ElevatorConstants {
14 Line 12
15 // declare two-Floor architecture in simulation
16 private Floor firstFloor;
Lines 16-20
17 private Floor secondFloor;
18
Use class diagram to determine associations
19 // ElevatorShaft in simulation with Floor and ElevatorShaft
Lines 23-28
20 private ElevatorShaft elevatorShaft;
21
22 // objects listening for events from ElevatorModel Declare listeners that receive
23 private Set personMoveListeners;
events from model (and will send
24 private DoorListener doorListener;
25 private ButtonListener buttonListener;
these events to ElevatorView)
26 private LightListener lightListener;
27 private BellListener bellListener;
28 private ElevatorMoveListener elevatorMoveListener;

 2003 Prentice Hall, Inc.


All rights reserved.
29 Outline
30 // cumulative number of people in simulation
Use class diagram to
31 private int numberOfPeople = 0;
32 determine attributes
ElevatorSimulat
33 // constructor instantiates ElevatorShaft and Floors
34 public ElevatorSimulation()
ion.java
35 { Class
36 // instantiate firstFloor and secondFloor objects Instantiate Floors and
ElevatorSimulat
37 firstFloor = new Floor( FIRST_FLOOR_NAME ); ElevatorShaft, thenthe
ion represents
38 secondFloor = new Floor( SECOND_FLOOR_NAME ); assign the ElevatorShaft
MVC model in our
39
to each Floor
referenceelevator simulation.
40 // instantiate ElevatorShaft object
41 elevatorShaft =
42 new ElevatorShaft( firstFloor, secondFloor ); Line 31
43
44 // give elevatorShaft reference to first and second Floor
Lines 37-46
45 firstFloor.setElevatorShaft( elevatorShaft );
46 secondFloor.setElevatorShaft( elevatorShaft );
47 Lines 49-53
48 // register for events from ElevatorShaft Register ElevatorSimulation
49 elevatorShaft.setDoorListener( this );
for events from ElevatorShaft
50 elevatorShaft.setButtonListener( this );
51 elevatorShaft.addElevatorMoveListener( this );
52 elevatorShaft.setLightListener( this );
53 elevatorShaft.setBellListener( this );
54
55 // instantiate Set for ElevatorMoveListener objects
56 personMoveListeners = new HashSet( 1 );
57
58 } // end ElevatorModel constructor

 2003 Prentice Hall, Inc.


All rights reserved.
59
60 // return Floor with given name Outline
61 private Floor getFloor( String name )
62 {
63 if ( name.equals( FIRST_FLOOR_NAME ) ) ElevatorSimulat
64 return firstFloor; ion.java
65 else
66
Class
67 if ( name.equals( SECOND_FLOOR_NAME ) ) ElevatorSimulat
68 return secondFloor; ion represents the
69 else MVC model in our
70 return null;
elevator simulation.
71
72 } // end method getFloor
73 Lines 75-91
74 // add Person to Elevator Simulator
Method for adding Person
75 public void addPerson( String floorName )
76 { to simulation model Lines 78-86
77 // instantiate new Person and place on Floor
78 Person person =
79 new Person( numberOfPeople, getFloor( floorName ) );
80 person.setName( Integer.toString( numberOfPeople ) ); Instantiate Person, register
81 ElevatorModel to receive
82 // register listener for Person events events from that Person and
83 person.setPersonMoveListener( this );
start Person’s thread
84
85 // start Person thread
86 person.start();

 2003 Prentice Hall, Inc.


All rights reserved.
87
88 // increment number of Person objects in simulation Outline
89 numberOfPeople++;
90
91 } // end method addPerson ElevatorSimulat
92 When Elevator
ion.java has
93 // invoked when Elevator has departed from Floor
departed fromClassFloor, notify
94 public void elevatorDeparted( ElevatorMoveEvent moveEvent )
95 {
Elevator-
listener (i.e.,ElevatorSimulat
96 elevatorMoveListener.elevatorDeparted( moveEvent ); View, in this ionsimulation)
represents the
97 } MVC model in our
98
elevator simulation.
99 // invoked when Elevator has arrived at destination Floor When Elevator has
100 public void elevatorArrived( ElevatorMoveEvent moveEvent ) arrived at Floor, notify
101 { Lines 94-97
listener (ElevatorView)
102 elevatorMoveListener.elevatorArrived( moveEvent );
103 } Lines 100-103
104 When Person performs some action
105 // send PersonMoveEvent to listener, dependingon event type
106 private void sendPersonMoveEvent(
(event) in model, determine Lines
which107-153
event was
107 int eventType, PersonMoveEvent event ) sent, then forward the event to any listeners
108 {
109 Iterator iterator = personMoveListeners.iterator();
110
111 while ( iterator.hasNext() ) {
112
113 PersonMoveListener listener =
114 ( PersonMoveListener ) iterator.next();

 2003 Prentice Hall, Inc.


All rights reserved.
115 Outline
116 // send Event to this listener, depending on eventType
117 switch ( eventType ) {
118 ElevatorSimulat
119 // Person has been created
120 case Person.PERSON_CREATED:
ion.java
121 listener.personCreated( event ); Class
122 break; ElevatorSimulat
When Person performs some
123 ion represents the
124 // Person arrived at Elevator action (event) in model,
MVC model in our
125 case Person.PERSON_ARRIVED: determine which event was
126 listener.personArrived( event );
elevator simulation.
sent, then forward the event to
127 break;
any listeners
128 Lines 108-153
129 // Person entered Elevator
130 case Person.PERSON_ENTERING_ELEVATOR:
131 listener.personEntered( event );
132 break;
133
134 // Person pressed Button object
135 case Person.PERSON_PRESSING_BUTTON:
136 listener.personPressedButton( event );
137 break;
138
139 // Person exited Elevator
140 case Person.PERSON_EXITING_ELEVATOR:
141 listener.personDeparted( event );
142 break;

 2003 Prentice Hall, Inc.


All rights reserved.
143 Outline
144 // Person exited simulation
145 case Person.PERSON_EXITED:
146 listener.personExited( event ); ElevatorSimulat
147 break;
148
ion.java
149 default: Class
150 break; ElevatorSimulat
151 } ion represents the
152 }
MVC model in our
153 } // end method sendPersonMoveEvent
154
elevator simulation.
155 // invoked when Person has been created in model
When Person has been
156 public void personCreated( PersonMoveEvent moveEvent ) Lines 156-159
157 { created, notify listeners
158 sendPersonMoveEvent( Person.PERSON_CREATED, moveEvent );
Lines 162-165
159 }
160
161 // invoked when Person has arrived at Floor's Button Lines 168-171
When Person has
162 public void personArrived( PersonMoveEvent moveEvent ) arrived at Elevator,
163 {
164 sendPersonMoveEvent( Person.PERSON_ARRIVED, moveEvent );
notify listeners
165 }
166
167 // invoked when Person has pressed Button
168 public void personPressedButton( PersonMoveEvent moveEvent )
169 { When Person has pressed
170 sendPersonMoveEvent( Person.PERSON_PRESSING_BUTTON, Button, notify listeners
171 moveEvent );
172 }

 2003 Prentice Hall, Inc.


All rights reserved.
173 Outline
174 // invoked when Person has entered Elevator
When Person has entered
175 public void personEntered( PersonMoveEvent moveEvent )
176 { Elevator, notify listeners
ElevatorSimulat
177 sendPersonMoveEvent( Person.PERSON_ENTERING_ELEVATOR,
178 moveEvent );
ion.java
179 } Class
180 ElevatorSimulat
181 // invoked when Person has departed from Elevator ion represents
When Person has left the
182 public void personDeparted( PersonMoveEvent moveEvent )
183 { Elevator, MVC
notifymodel in our
listeners
184 sendPersonMoveEvent( Person.PERSON_EXITING_ELEVATOR,
elevator simulation.
185 moveEvent );
186 } Lines 175-179
187
188 // invoked when Person has exited Simulation
When Person Lineshas
182-186
exited
189 public void personExited( PersonMoveEvent moveEvent )
190 { simulation, notify listeners
191 sendPersonMoveEvent( Person.PERSON_EXITED, moveEvent ); Lines 189-192
192 }
193 Lines 195-198
194 // invoked when Door has opened
195 public void doorOpened( DoorEvent doorEvent )
196 {
When Door has opened,
197 doorListener.doorOpened( doorEvent );
198 }
notify listeners
199

 2003 Prentice Hall, Inc.


All rights reserved.
200 // invoked when Door has closed Outline
201 public void doorClosed( DoorEvent doorEvent )
202 { When Door has closed,
203 doorListener.doorClosed( doorEvent );
notify listeners ElevatorSimulat
204 }
205
ion.java
206 // invoked when Button has been pressed Class
207 public void buttonPressed( ButtonEvent buttonEvent ) ElevatorSimulat
208 { When Button ion has
represents
been the
209 buttonListener.buttonPressed( buttonEvent );
MVClisteners
pressed, notify model in our
210 }
211
elevator simulation.
212 // invoked when Button has been reset
213 public void buttonReset( ButtonEvent buttonEvent ) Lines 201-204
214 { When Button has been
215 buttonListener.buttonReset( buttonEvent );
216 }
reset, notifyLines 207-210
listeners
217
218 // invoked when Bell has rung Lines 213-216
219 public void bellRang( BellEvent bellEvent )
220 {
When Bell has rung,Lines 219-222
221 bellListener.bellRang( bellEvent );
222 }
notify listeners
223 Lines 225-228
224 // invoked when Light has turned on
225 public void lightTurnedOn( LightEvent lightEvent )
226 {
When Light has been
227 lightListener.lightTurnedOn( lightEvent );
228 } turned on, notify listeners

 2003 Prentice Hall, Inc.


All rights reserved.
229 Outline
230 // invoked when Light has turned off
231 public void lightTurnedOff( LightEvent lightEvent )
232 { When Light ElevatorSimulat
has been
233 lightListener.lightTurnedOff( lightEvent );
234 }
ion.java
turned off, notify listeners
235 Class
236 // set listener for ElevatorModelListener ElevatorSimulat
Allow ElevatorSimulationListener
237 public void setElevatorSimulationListener( ion represents the
238 ElevatorSimulationListener listener ) to listen for all events from model
MVC model in our
239 {
240 // ElevatorModelListener extends all interfaces below
elevator simulation.
241 addPersonMoveListener( listener );
242 setElevatorMoveListener( listener ); Lines 231-234
243 setDoorListener( listener );
244 setButtonListener( listener );
Lines 237-247
245 setLightListener( listener );
246 setBellListener( listener );
247 } Lines 250-254
248
249 // set listener for PersonMoveEvents
Allow PersonListener to listen for
250 public void addPersonMoveListener(
251 PersonMoveListener listener )
PersonMoveEvents from model
252 {
253 personMoveListeners.add( listener );
254 }
255

 2003 Prentice Hall, Inc.


All rights reserved.
256 // set listener for DoorEvents Outline
257 public void setDoorListener( DoorListener listener )
258 { Allow DoorListener to listen
259 doorListener = listener; for DoorEvents from model
ElevatorSimulat
260 }
261
ion.java
262 // set listener for ButtonEvents Class
263 public void setButtonListener( ButtonListener listener ) ElevatorSimulat
264 { ion represents the
265 buttonListener = listener; Allow ButtonListener
MVC modeltoinlisten
our
266 } for ButtonEvents from model
elevator simulation.
267
268 // add listener for ElevatorMoveEvents
Allow ElevatorMoveListener to listen
269 public void setElevatorMoveListener( Lines 257-260
270 ElevatorMoveListener listener )
for ElevatorMoveEvents from model
271 {
Lines 263-266
272 elevatorMoveListener = listener;
273 }
Allow LightListener to listen
274 for LightEvents from model
Lines 269-273
275 // set listener for LightEvents
276 public void setLightListener( LightListener listener ) Lines 276-279
277 {
278 lightListener = listener;
279 } Lines 282-285
280
281 // set listener for BellEvents
282 public void setBellListener( BellListener listener ) Allow BellListener to listen
283 { for BellEvents from model
284 bellListener = listener;
285 }
286 }
 2003 Prentice Hall, Inc.
All rights reserved.
Fig. E.2 Class diagram showing
realizations in the elevator model (Part 1).
ElevatorSimulationListener ButtonListener DoorListener BellListener

ElevatorSimulation Elevator

ElevatorMove-
LightListener ButtonListener DoorListener BellListener Listener

ElevatorShaft

ElevatorMoveListener

ElevatorDoor Light Bell Button

 2003 Prentice Hall, Inc. All rights reserved.


Fig. E.3 Class diagram showing
realizations in the elevator model (Part 2).
PersonMove- Light- Button- Door- Bell- ElevatorMove-
Listener Listener Listener Listener Listener Listener

ElevatorSimulationListener

ElevatorSimulation

 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, 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.


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 // Location.java Outline
2 // Abstract superclass representing location in simulation
3 package com.deitel.jhtp5.elevator.model;
4 Location.java
5 // Deitel packages
6 import com.deitel.jhtp5.elevator.event.*;
Location superclass
7 that represents a
8 public abstract class Location { location in the
9 simulation.
10 // name of Location
Name of Location can equal “elevator,”
11 private String locationName;
12
“first floor” or “second Linefloor”
11
13 // set name of Location
14 protected void setLocationName( String name ) Lines 26-29
15 {
16 locationName = name;
17 }
18
19 // return name of Location
20 public String getLocationName()
21 {
22 return locationName;
23 }
24
25 // return Button at Location
26 public abstract Button getButton();
Classes Floor and
27 Elevator implement these
28 // return Door object at Location abstract methods to
29 public abstract Door getDoor(); return appropriate objects
30 }

 2003 Prentice Hall, Inc.


All rights reserved.
1 // Floor.java Outline
2 // Represents a Floor located next to an ElevatorShaft
3 package com.deitel.jhtp5.elevator.model;
4 Floor.java
5 // Deitel packages
6 import com.deitel.jhtp5.elevator.ElevatorConstants;
Class Floor—a
7 subclass of
8 public class Floor extends Location Location—
9 implements ElevatorConstants { represents a Floor
10
across which a
11 // reference to ElevatorShaft object
12 private ElevatorShaft elevatorShaft;
Person walks to the
13 Elevator.
14 // Floor constructor sets name of Floor
15 public Floor( String name )
16 {
17 setLocationName( name );
18 }
19

 2003 Prentice Hall, Inc.


All rights reserved.
20 // get first or second Floor Button, using Location name Implement Location’s abstractOutline
21 public Button getButton()
22 {
method getButton to return Button
23 if ( getLocationName().equals( FIRST_FLOOR_NAME ) ) on either first or second Floor
Floor.java
24 return getElevatorShaft().getFirstFloorButton();
25 else
Class Floor—a
26 subclass of
27 if ( getLocationName().equals( SECOND_FLOOR_NAME ) ) Location—
28 return getElevatorShaft().getSecondFloorButton(); represents a Floor
29 else
across which a
30
31 return null;
Person walks to the
32 Elevator.
33 } // end method getButton
34 Lines 21-33
35 // get first or second Floor Door, using Location name Implement Location’s
36 public Door getDoor() abstract method
37 { Lines 36-48
38 if ( getLocationName().equals( FIRST_FLOOR_NAME ) )
getDoor to return
39 return getElevatorShaft().getFirstFloorDoor(); Door on either first or
40 else second Floor
41
42 if ( getLocationName().equals( SECOND_FLOOR_NAME ) )
43 return getElevatorShaft().getSecondFloorDoor();
44 else
45
46 return null;
47
48 } // end method getDoor

 2003 Prentice Hall, Inc.


All rights reserved.
49 Outline
50 // get ElevatorShaft reference
51 public ElevatorShaft getElevatorShaft()
52 { Floor.java
53 return elevatorShaft;
54 }
Class Floor—a
55 subclass of
56 // set ElevatorShaft reference Location—
57 public void setElevatorShaft( ElevatorShaft shaft ) represents a Floor
58 {
across which a
59 elevatorShaft = shaft;
60 }
Person walks to the
61 } Elevator.

 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.


1 // Door.java Outline
2 // Sends DoorEvents to DoorListeners when opened or closed
3 package com.deitel.jhtp5.elevator.model;
4 Door.java
5 Door listens for ElevatorMoveEvents;
// Java core packages
6 import java.util.*; when Elevator arrives, Door opens
Class Door, which
7 represents a Door in
8 // Deitel packages the model, informs
9 import com.deitel.jhtp5.elevator.event.*; listeners when a Door
10
has opened or closed.
11 public class Door {
12
13 // represent whether Door is open or closed Line 11
14 private boolean open = false;
15 Lines 20 and 28
16 // time before Door closes automatically
17 public static final int AUTOMATIC_CLOSE_DELAY = 3000;
18
19 // Set of DoorListeners
20 private Set doorListeners;
21
22 // location where Door opened or closed
23 private Location doorLocation; HashSet stores listener for
24
DoorEvents (i.e., when
25 // Door constructor instantiates Set for DoorListeners
26 public Door() Door opens and closes)
27 {
28 doorListeners = new HashSet( 1 );
29 }

 2003 Prentice Hall, Inc.


All rights reserved.
30 Outline
31 // add Door listener
32 public void addDoorListener( DoorListener listener )
33 { Door.java
34 // prevent other objects from modifying doorListeners
35 synchronized( doorListeners )
Class Door, which
36 { represents a Door in
37 doorListeners.add( listener ); Allow DoorListeners to register
the model, informsand
38 } unregister to receive DoorEvents
listeners when a Door
39 }
has opened or closed.
40
41 // remove Door listener
42 public void removeDoorListener( DoorListener listener ) Lines 32-49
43 {
44 // prevent other objects from modifying doorListeners Line 56
45 synchronized( doorListeners )
46 {
47 doorListeners.remove( listener );
48 }
49 }
50
51 // open Door and send all listeners DoorEvent objects
52 public synchronized void openDoor( Location location )
53 {
54 if ( !open ) {
55
56 open = true;
Open Door if it is closed
57

 2003 Prentice Hall, Inc.


All rights reserved.
58 // obtain iterator from Set Outline
59 Iterator iterator;
60 synchronized( doorListeners )
61 { Door.java
62 iterator = new HashSet( doorListeners ).iterator();
63 }
Class Door, which
64 represents a Door in
65 // get next DoorListener the model, informs
66 while ( iterator.hasNext() ) { listeners when a Door
67 DoorListener doorListener =
has opened or closed.
68 ( DoorListener ) iterator.next();
69 Notify DoorListeners
70 // send doorOpened event to this DoorListener Lines 66-73
that Door has opened
71 doorListener.doorOpened(
72 new DoorEvent( this, location ) ); Lines 78-95
73 }
74
75 doorLocation = location;
76
77 // declare Thread that ensures automatic Door closing Use Thread to enable Door
78 Thread closeThread = new Thread( to close itself automatically
79 new Runnable() { after three seconds
80
81 public void run()
82 {
83 // close Door if open for more than 3 seconds
84 try {
85 Thread.sleep( AUTOMATIC_CLOSE_DELAY );
86 closeDoor( doorLocation );
87 }

 2003 Prentice Hall, Inc.


All rights reserved.
88 Outline
89 // handle exception if interrupted
90 catch ( InterruptedException exception ) {
91 exception.printStackTrace(); Door.java
92 }
93 }
Class Door, which
94 } // end anonymous inner class represents a Door in
95 ); the model, informs
96 listeners when a Door
97 closeThread.start();
has opened or closed.
98 }
99
100 // notify all waiting threads that the door has opened Line 106
101 notifyAll();
102
103 } // end method openDoor
104
105 // close Door and send all listeners DoorEvent objects
106 public synchronized void closeDoor( Location location ) Close Door if it is open
107 {
108 if ( open ) {
109
110 open = false;
111
112 // obtain iterator from Set
113 Iterator iterator;
114 synchronized( doorListeners )
115 {
116 iterator = new HashSet( doorListeners ).iterator();
117 }

 2003 Prentice Hall, Inc.


All rights reserved.
118 Outline
119 // get next DoorListener
120 while ( iterator.hasNext() ) {
121 DoorListener doorListener = Door.java
122 ( DoorListener ) iterator.next();
123
Class Door, which
124 // send doorClosed event to this DoorListener represents a Door in
125 doorListener.doorClosed( Notify DoorListeners
the model, informs
126 new DoorEvent( this, location ) ); that Door has closed
listeners when a Door
127 }
has opened or closed.
128 }
129
130 } // end method closeDoor Lines 125-126
131
132 // return whether Door is open or closed Lines 133-136
133 public synchronized boolean isDoorOpen()
134 { When Elevator
135 return open;
arrives, open Door
136 }
137 }

 2003 Prentice Hall, Inc.


All rights reserved.
1 // ElevatorDoor.java Outline
2 // Opens and closes floor Door when elevator arrives and departs.
3 package com.deitel.jhtp5.elevator.model;
4 ElevatorDoor.ja
5 // Java core packages
6 import java.util.*;
va
7 Class
8 // Deitel packages ElevatorDoor extends Door and
ElevatorDoor
9 import com.deitel.jhtp5.elevator.event.*; implements ElevatorMoveListener
opens in response to
10
Override method elevatorArrived
openDoor
11 public class ElevatorDoor extends Door implements ElevatorMoveListener {
12
messages and opens
13 // open ElevatorDoor and corresponding Floor Door and closes the Floor
14 public synchronized void openDoor( Location location ) Doors.
15 {
16 location.getDoor().openDoor( location );
Line 11
17
18 super.openDoor( location );
19 Lines 14-20
20 } // end method openDoor
21

 2003 Prentice Hall, Inc.


All rights reserved.
22 // close ElevatorDoor and Corresponding Floor Door Outline
23 public synchronized void closeDoor( Location location )
24 {
25 location.getDoor().closeDoor( location ); Override method closeDoor
ElevatorDoor.ja
26
27 super.closeDoor( location );
va
28 Class
29 } // end method closeDoor ElevatorDoor
30 opens in response to
31 // invoked when Elevator has departed
elevatorArrived
32 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {}
33
messages and opens
34 // invoked when Elevator has arrived and closes the Floor
35 public void elevatorArrived( ElevatorMoveEvent moveEvent ) Doors.
36 {
37 openDoor( moveEvent.getLocation() );
Lines 23-29
38 }
39 }

 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.


1 // Button.java Outline
2 // Sends ButtonEvents to ButtonListeners when accessed
3 package com.deitel.jhtp5.elevator.model;
4 Button listens for ElevatorMoveEvents;
Button.java
5 // Deitel packages
6 import com.deitel.jhtp5.elevator.event.*;
when Elevator arrives, ButtonClass Button,
resets which
7 represents a Button
8 public class Button implements ElevatorMoveListener { in the model, informs
9 listeners when a
10 // ButtonListener
Button has been
11 private ButtonListener buttonListener = null;
12
pressed or reset.
13 // represent whether Button is pressed Allow ButtonListener to
14 private boolean pressed = false; listen for Line 8
ButtonEvents
15
16 // set listener
Lines 11 and 17-20
17 public void setButtonListener( ButtonListener listener )
18 {
19 buttonListener = listener; Lines 23-29
20 }
21
22 // press Button and send ButtonEvent
23 public void pressButton( Location location )
24 {
25 pressed = true; Press Button and notify
26 ButtonListener that
27 buttonListener.buttonPressed( Button has been pressed
28 new ButtonEvent( this, location ) );
29 }

 2003 Prentice Hall, Inc.


All rights reserved.
30 Outline
31 // reset Button and send ButtonEvent Reset Button and notify
32 public void resetButton( Location location ) ButtonListener that
33 { Button.java
Button has been reset
34 pressed = false;
35
Class Button, which
36 buttonListener.buttonReset( represents a Button
37 new ButtonEvent( this, location ) ); in the model, informs
38 } listeners when a
39
Button has been
40 // return whether button is pressed
41 public boolean isButtonPressed()
pressed or reset.
42 {
43 return pressed; Lines 32-38
44 }
45
Lines 50-53
46 // invoked when Elevator has departed
47 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {}
48
49 // invoked when Elevator has arrived
50 public void elevatorArrived( ElevatorMoveEvent moveEvent )
51 {
52 resetButton( moveEvent.getLocation() );
When Elevator
53 } arrives, reset Button
54 }

 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.


1 // ElevatorShaft.java Outline
2 // Represents elevator shaft, which contains elevator
3 package com.deitel.jhtp5.elevator.model;
4 ElevatorShaft.j
5 // Java core packages
ElevatorShaft listens
ava for
6 import java.util.*;
7 ElevatorMoveEvents
Class
8 // Deitel packages LightEvents and BellEvents
ElevatorShaft,
9 import com.deitel.jhtp5.elevator.event.*; which represents the
10
ElevatorShaft,
11 public class ElevatorShaft implements ElevatorMoveListener,
12 LightListener, BellListener {
which sends events
13 from the Elevator
14 // Elevator to the
15 private Elevator elevator; ElevatorSimulat
16
ion.
17 // Buttons on Floors
18 private Button firstFloorButton;
19 private Button secondFloorButton; Lines 11-12
20
21 // Doors on Floors Lines 18-27
22 private Door firstFloorDoor; Use class diagram to determine
23 private Door secondFloorDoor; associations of ElevatorShaft
24
25 // Lights on Floors
26 private Light firstFloorLight;
27 private Light secondFloorLight;
28

 2003 Prentice Hall, Inc.


All rights reserved.
29 // listeners Declare listeners that receive events
Outline
30 private DoorListener doorListener; from model (and will send these
31 private ButtonListener buttonListener;
events to ElevatorModel)
32 private LightListener lightListener; ElevatorShaft.j
33 private BellListener bellListener;
34 private Set elevatorMoveListeners;
ava
35 Class
36 // constructor initializes aggregated components ElevatorShaft,
37 public ElevatorShaft( Floor firstFloor, Floor secondFloor ) which represents the
38 {
ElevatorShaft,
39 // instantiate Set for ElevatorMoveListeners
40 elevatorMoveListeners = new HashSet( 1 );
which sends events
41 from the Elevator
42 // anonymous inner class listens for ButtonEvents to the
43 ButtonListener floorButtonListener = Instantiate anonymous inner class
ElevatorSimulat
44 new ButtonListener() { to listen for ButtonEvents ion.from
45
Buttons on floors
46 // called when Floor Button has been pressed
47 public void buttonPressed( ButtonEvent buttonEvent ) Lines 30-34
48 {
49 When Button on floor is
// request elevator move to location Lines 43-70
50 pressed, request Elevator to
Location location = buttonEvent.getLocation();
51 buttonListener.buttonPressed( buttonEvent );
move to Floor of request Lines 47-53
52 elevator.requestElevator( location );
53 }
54

 2003 Prentice Hall, Inc.


All rights reserved.
55 // called when Floor Button has been reset Outline
56 public void buttonReset( ButtonEvent buttonEvent )
57 {
58 When Button
buttonListener.buttonReset( buttonEvent ); on floor ElevatorShaft.j
59 } is reset, reset Button ava
60 }; // end anonymous inner class
61 Class
62 // instantiate Floor Buttons ElevatorShaft,
63 firstFloorButton = new Button(); which represents the
64 secondFloorButton = new Button();
ElevatorShaft,
65
66 // register anonymous ButtonListener with Floor Buttons
which sends events
67 firstFloorButton.setButtonListener( from the Elevator
68 floorButtonListener ); to the
69 secondFloorButton.setButtonListener( ElevatorSimulat
70 floorButtonListener );
ion.
71 Instantiate anonymous inner
72 // Floor Buttons listen for ElevatorMoveEvents
class to listen for DoorEventsLines 56-59
73 addElevatorMoveListener( firstFloorButton );
74 addElevatorMoveListener( secondFloorButton );
from Doors on floors
75 Lines 77-100
76 // anonymous inner class listens for DoorEvents
77 DoorListener floorDoorListener = new DoorListener() {
78 Lines 80-91
79 // called when Floor Door has opened
80 public void doorOpened( DoorEvent doorEvent )
81 {
When Door on floor is opened or
82 // forward event to doorListener
83 doorListener.doorOpened( doorEvent ); closed, forward DoorEvent to
84 } DoorListener
 2003 Prentice Hall, Inc.
All rights reserved.
85 Outline
86 // called when Floor Door has closed
87 public void doorClosed( DoorEvent doorEvent )
88 { ElevatorShaft.j
89 // forward event to doorListener
90 doorListener.doorClosed( doorEvent );
ava
91 } Class
92 }; // end anonymous inner class ElevatorShaft,
93 which represents the
94 // instantiate Floor Doors
ElevatorShaft,
95 firstFloorDoor = new Door();
96 secondFloorDoor = new Door();
which sends events
97 from the Elevator
98 // register anonymous DoorListener with Floor Doors to the
99 firstFloorDoor.addDoorListener( floorDoorListener ); ElevatorSimulat
100 secondFloorDoor.addDoorListener( floorDoorListener );Instantiate Lights and
ion.
101 listen for LightEvents
102 // instantiate Lights, then listen for LightEvents
103 firstFloorLight = new Light(); Lines 103-105
104 addElevatorMoveListener( firstFloorLight );
105 firstFloorLight.setLightListener( this ); Lines 112-115
106
107 secondFloorLight = new Light();
108 addElevatorMoveListener( secondFloorLight );
109 secondFloorLight.setLightListener( this ); Instantiate Elevator
110 and listen for
111 // instantiate Elevator object ElevatorMoveEvents
112 elevator = new Elevator( firstFloor, secondFloor );
113

 2003 Prentice Hall, Inc.


All rights reserved.
114 // register for ElevatorMoveEvents from elevator Outline
115 elevator.addElevatorMoveListener( this );
116 Listen for BellEvents
117 // listen for BellEvents from elevator from elevator ElevatorShaft.j
118 elevator.setBellListener( this );
119
ava
120 // anonymous inner class listens for ButtonEvents from Class
121 // elevator ElevatorShaft,
Instantiate anonymous
122 elevator.setButtonListener( which represents
inner class to listen for the
123 new ButtonListener() {
ElevatorShaft,
ButtonEvents from
124
125 // invoked when button has been pressed
whichButton
Elevator’s sends events
126 public void buttonPressed( ButtonEvent buttonEvent ) from the Elevator
127 { to the
128 // send event to listener When Elevator’s
ElevatorSimulat
129 buttonListener.buttonPressed( buttonEvent ); Buttonion.is pressed
130 }
or reset, forward
131
132 // invoked when button has been reset
ButtonEvent
Line 118 to
133 public void buttonReset( ButtonEvent buttonEvent ) ButtonListener
134 { Lines 122-140
135 // send event to listener
136 buttonListener.buttonReset(
137 new ButtonEvent( this, elevator ) ); Lines 126-138
138 }
139 } // end anonymous inner class
140 );
141

 2003 Prentice Hall, Inc.


All rights reserved.
142 // anonymous inner class listens for DoorEvents from Outline
143 // elevator Instantiate anonymous
144 elevator.setDoorListener( inner class to listen for
145 new DoorListener() { ElevatorShaft.j
DoorEvents from
146
147 // invoked when door has opened
ava Door
Elevator’s
148 public void doorOpened( DoorEvent doorEvent ) Class
149 { ElevatorShaft,
150 // send event to listener When Elevator’s
which represents the
151 doorListener.doorOpened( doorEvent );
ElevatorShaft,
Door is pressed or
152 }
153
which sends events
reset, forward
154 // invoked when door has closed DoorEvent fromto the Elevator
155 public void doorClosed( DoorEvent doorEvent ) DoorListenerto the
156 { ElevatorSimulat
157 // send event to listener
ion.
158 doorListener.doorClosed( doorEvent );
159 }
160 } // end anonymous inner class Lines 144-161
161 );
162 Lines 148-159
163 // start Elevator Thread
164 elevator.start(); Start Elevator’s Thread
165 Line 164
166 } // end ElevatorShaft constructor
167 Lines 169-172
168 // get Elevator
169 public Elevator getElevator() Get method for Elevator
170 {
171 return elevator;
172 }
 2003 Prentice Hall, Inc.
All rights reserved.
173 Outline
174 // get Door on first Floor
175 public Door getFirstFloorDoor()
176 { ElevatorShaft.j
177 return firstFloorDoor;
178 }
ava
179 Class
180 // get Door on second Floor ElevatorShaft,
181 public Door getSecondFloorDoor() which represents the
182 {
ElevatorShaft,
183 return secondFloorDoor;
184 }
which sends events
185 from the Elevator
186 // get Button on first Floor to the
187 public Button getFirstFloorButton() ElevatorSimulat
188 {
ion.
Get methods for Doors,
189 return firstFloorButton;
190 } Buttons and Lights
191 Lines 175-208
192 // get Button on second Floor
193 public Button getSecondFloorButton()
194 {
195 return secondFloorButton;
196 }
197
198 // get Light on first Floor
199 public Light getFirstFloorLight()
200 {
201 return firstFloorLight;
202 }

 2003 Prentice Hall, Inc.


All rights reserved.
203 Outline
204 // get Light on second Floor
205 public Light getSecondFloorLight()
206 { ElevatorShaft.j
207 return secondFloorLight;
208 }
ava
209 Class
210 // invoked when Bell rings ElevatorShaft,
211 public void bellRang( BellEvent bellEvent )
When Bell has rung, forward
which represents the
212 { BellEvent to BellListener
ElevatorShaft,
213 bellListener.bellRang( bellEvent );
214 }
which sends events
215 from the Elevator
216 // invoked when Light turns on to the
217 public void lightTurnedOn( LightEvent lightEvent ) ElevatorSimulat
218 {
When Lightsion.
turn
219 lightListener.lightTurnedOn( lightEvent );
220 } on or off, forward
221 LightEvent Lines
to 211-214
222 // invoked when Light turns off
LightListener
223 public void lightTurnedOff( LightEvent lightEvent ) Lines 217-226
224 { When Elevator has departed, notify
225 lightListener.lightTurnedOff( lightEvent );
ElevatorMoveListeners
Lines 229-243
226 }
227
228 // invoked when Elevator departs
229 public void elevatorDeparted( ElevatorMoveEvent moveEvent )
230 {
231 Iterator iterator = elevatorMoveListeners.iterator();
232

 2003 Prentice Hall, Inc.


All rights reserved.
233 // iterate Set of ElevatorMoveEvent listeners Outline
234 while ( iterator.hasNext() ) {
235
236 // get respective ElevatorMoveListener from Set ElevatorShaft.j
237 ElevatorMoveListener listener =
238 ( ElevatorMoveListener ) iterator.next();
ava
239 Class
240 // send ElevatorMoveEvent to this listener ElevatorShaft,
241 listener.elevatorDeparted( moveEvent ); which represents the
242 }
ElevatorShaft,
243 } // end method elevatorDeparted
244
which sends events
245 // invoked when Elevator arrives from the Elevator
246 public void elevatorArrived( ElevatorMoveEvent moveEvent ) to the
247 { ElevatorSimulat
248 // obtain iterator from Set
ion.
249 Iterator iterator = elevatorMoveListeners.iterator();
250 When Elevator has arrived, notify
251 // get next DoorListener Lines 246-262
ElevatorMoveListeners
252 while ( iterator.hasNext() ) {
253
254 // get next ElevatorMoveListener from Set
255 ElevatorMoveListener listener =
256 ( ElevatorMoveListener ) iterator.next();
257
258 // send ElevatorMoveEvent to this listener
259 listener.elevatorArrived( moveEvent );
260
261 } // end while loop
262 } // end method elevatorArrived

 2003 Prentice Hall, Inc.


All rights reserved.
263
264 // set listener to DoorEvents
Outline
265 public void setDoorListener( DoorListener listener )
266 {
267 doorListener = listener; ElevatorShaft.j
Enable DoorListener to receive
268 } ava
DoorEvents from ElevatorShaft
269 Class
270 // set listener to ButtonEvents ElevatorShaft,
271 public void setButtonListener( ButtonListener listener )
272 {
which represents the
273 buttonListener = listener; ElevatorShaft,
274 } Enable ButtonListener to receive which sends events
275 ButtonEvents from ElevatorShaft from the Elevator
276 // add listener to ElevatorMoveEvents Enable ElevatorMoveListeners
to the
277 public void addElevatorMoveListener(
to receive ElevatorMoveEvents
ElevatorSimulat
278 ElevatorMoveListener listener )
279 {
from ElevatorShaft
ion.
280 elevatorMoveListeners.add( listener );
281 } Lines 265-268
282
283 // set listener to LightEvents
284 public void setLightListener( LightListener listener ) Lines 271-274
285 {
286 lightListener = listener; Enable BellListener to receive
Lines 277-281
287 } Enable LightListener to receive
BellEvents from ElevatorShaft
288 LightEvents from ElevatorShaft
289 // set listener to BellEvents
Lines 284-287
290 public void setBellListener( BellListener listener )
291 { Lines 290-293
292 bellListener = listener;
293 }
294 }  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.


1 // Light.java Outline
2 // Light turns a light on or off
3 package com.deitel.jhtp5.elevator.model;
4 Light.java
5 // Deitel packages
6 import com.deitel.jhtp5.elevator.event.*;
Class Light
7 represents a Light
8 public class Light implements ElevatorMoveListener { on the Floor in the
9 model.
10 // Light state (on/off) Light turns itself
11 private boolean lightOn; offLine
automatically
12
14
after three seconds
13 // time before Light turns off automatically (3 seconds)
14 public static final int AUTOMATIC_TURNOFF_DELAY = 3000; Lines 23-26
15
16 // LightListener listens for when Light should turn on/off
17 private LightListener lightListener;
18
19 // location where Light turned on or off
Enable LightListener
20 private Location lightLocation;
21 to receive LightEvents
22 // set LightListener
23 public void setLightListener( LightListener listener )
24 {
25 lightListener = listener;
26 }

 2003 Prentice Hall, Inc.


All rights reserved.
27 Outline
28 // turn on Light
29 public void turnOnLight( Location location ) Method to turn on Light
30 { Light.java
31 if ( !lightOn ) {
32
Class Light
33 lightOn = true; represents a Light
34 on the Floor in the
35 // send LightEvent to LightListener
Send LightEvent to
model.
36 lightListener.lightTurnedOn( LightListener
37 new LightEvent( this, location ) ); (ElevatorShaft)
Lines 29-63
38 when Light turns on
39 lightLocation = location;
40 Lines 36-37
41 // declare Thread that ensures automatic Light turn off
42 Thread thread = new Thread(
Instantiate Thread
and start Lines 42-61
43 new Runnable() {
44 that turns Light off
45 public void run() automatically after three seconds
46 {
47 // turn off Light if on for more than 3 seconds
48 try {
49 Thread.sleep( AUTOMATIC_TURNOFF_DELAY );
50 turnOffLight( lightLocation );
51 }
52

 2003 Prentice Hall, Inc.


All rights reserved.
53 // handle exception if interrupted Outline
54 catch ( InterruptedException exception ) {
55 exception.printStackTrace();
56 } Light.java
57 }
58 } // end anonymous inner class
Class Light
59 ); represents a Light
60 on the Floor in the
61 thread.start(); model.
62 }
63 } // end method turnOnLight
64
Lines 66-76
65 // turn off Light
66 public void turnOffLight( Location location ) Lines
Method to turn 73-74
off Light
67 {
68 if ( lightOn ) {
69
70 lightOn = false;
71
Send LightEvent to
72 // send LightEvent to LightListener
73 lightListener.lightTurnedOff( LightListener
74 new LightEvent( this, location ) ); (ElevatorShaft)
75 } when Light turns off
76 } // end method turnOffLight

 2003 Prentice Hall, Inc.


All rights reserved.
77 Outline
78 // return whether Light is on or off
79 public boolean isLightOn()
80 { Light.java
81 return lightOn;
82 }
Class Light
83 represents a Light
84 // invoked when Elevator has departed on the Floor in the
85 public void elevatorDeparted(
When Elevator departs
model.
86 ElevatorMoveEvent moveEvent ) from Floor, turn off Light
87 {
88 turnOffLight( moveEvent.getLocation() );
Lines 85-89
89 }
90 Lines 92-96
91 // invoked when Elevator has arrived When Elevator arrives at
92 public void elevatorArrived(
93 ElevatorMoveEvent moveEvent )
Floor, turn off Light
94 {
95 turnOnLight( moveEvent.getLocation() );
96 }
97 }

 2003 Prentice Hall, Inc.


All rights reserved.
1 // Bell.java Outline
2 // Represents Bell in simulation
3 package com.deitel.jhtp5.elevator.model;
4 Bell.java
5 // Deitel packages
6 import com.deitel.jhtp5.elevator.event.*;
Class Bell represents
7 the Bell in the
8 public class Bell implements ElevatorMoveListener { model.
9
10 // BellListener listens for BellEvent object
Lines 17-18
11 private BellListener bellListener; Send BellEvent to
12
BellListener
Lines 22-25
13 // ring bell and send BellEvent object to listener
14 private void ringBell( Location location )
(ElevatorShaft)
15 { when Bell has rung
16 if ( bellListener != null )
17 bellListener.bellRang(
18 new BellEvent( this, location ) );
19 } Enable BellListener to
20 receive BellEvents
21 // set BellListener
22 public void setBellListener( BellListener listener )
23 {
24 bellListener = listener;
25 }

 2003 Prentice Hall, Inc.


All rights reserved.
26 Outline
27 // invoked when Elevator has departed
28 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {} When Elevator arrives
29 at Floor,Bell.java
ring Bell
30 // invoked when Elevator has arrived
31 public void elevatorArrived( ElevatorMoveEvent moveEvent )
Class Bell represents
32 { the Bell in the
33 ringBell( moveEvent.getLocation() ); model.
34 }
35 }
Lines 31-34

 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.


1 // Elevator.java Outline
2 // Travels between Floors in the ElevatorShaft
3 package com.deitel.jhtp5.elevator.model;
4 Elevator.java
5 // Java core packages
6 import java.util.*;
Class Elevator
7 represents the
8 // Deitel packages Class Elevator “is Elevator
a” Location traveling
9 import com.deitel.jhtp5.elevator.event.*; between two Floors,
10 import com.deitel.jhtp5.elevator.ElevatorConstants;
operating
11
12 public class Elevator extends Location implements Runnable,
asynchronously with
13 BellListener, ElevatorConstants { other objects.
14
15 // manages Elevator thread Line 12
16 private boolean elevatorRunning = false;
17
18 // describes Elevator state (idle or moving) Lines 19-28
19 private boolean moving = false;
20
21 // current Floor
22 private Location currentFloorLocation;
23
Use class diagram to
24 // destination Floor determine associations and
25 private Location destinationFloorLocation; attributes of Elevator
26
27 // Elevator needs to service other Floor
28 private boolean summoned;
29

 2003 Prentice Hall, Inc.


All rights reserved.
30 // listener objects Outline
Declare listeners that receive events
31 private Set elevatorMoveListeners;
32 private BellListener bellListener; from model (and will send these
33 private ButtonListener elevatorButtonListener; events to ElevatorShaft) Elevator.java
34 private DoorListener elevatorDoorListener;
35
Class Elevator
36 // ElevatorDoor, Button and Bell on Elevator represents the
Use class diagram to
37 private ElevatorDoor elevatorDoor; Elevator traveling
determine associations and
38 private Button elevatorButton; between two Floors,
39 private Bell bell; attributes of Elevator
operating
40
41 public static final int ONE_SECOND = 1000;
asynchronously with
42 other objects.
43 // time needed to travel between Floors (5 seconds)
44 private static final int TRAVEL_TIME = 5 * ONE_SECOND; Lines 31-34
45
46 // Elevator's thread to handle asynchronous movement
47 private Thread thread; Lines 37-39, 44
48
49 // constructor creates variables; registers for ButtonEvents Lines 55-57
50 public Elevator( Floor firstFloor, Floor secondFloor )
51 {
52 setLocationName( ELEVATOR_NAME );
53
54 // instantiate Elevator's Door, Button and Bell
55 elevatorDoor = new ElevatorDoor();
56 elevatorButton = new Button(); Instantiate ElevatorDoor, Button
57 bell = new Bell(); and Bell inside Elevator

 2003 Prentice Hall, Inc.


All rights reserved.
58 Outline
59 // register Elevator for BellEvents
60 bell.setBellListener( this ); Listen for BellEvents
61 Elevator.java
62 // instantiate listener Set
63 elevatorMoveListeners = new HashSet( 1 );
Class Elevator
64 represents the
65 // start Elevator on first Floor Elevator traveling
66 currentFloorLocation = firstFloor; between two Floors,
67 destinationFloorLocation = secondFloor;
operating
68
69 // register elevatorButton for ElevatorMoveEvents
asynchronously with
70 addElevatorMoveListener( elevatorButton ); other objects.
71
72 // register elevatorDoor for ElevatorMoveEvents Line 60
73 addElevatorMoveListener( elevatorDoor );
74
75 // register bell for ElevatorMoveEvents Lines 81-101
76 addElevatorMoveListener( bell );
77
78 // anonymous inner class listens for ButtonEvents from
79 // elevatorButton
80 elevatorButton.setButtonListener(
Instantiate anonymous inner class
81 new ButtonListener() { to listen for ButtonEvents
82 from Elevator’s Button

 2003 Prentice Hall, Inc.


All rights reserved.
83 // invoked when elevatorButton has been pressed Outline
84 public void buttonPressed( ButtonEvent buttonEvent )
85 {
86 // send ButtonEvent to listener When Elevator’s Elevator.java
87 elevatorButtonListener.buttonPressed(
88 buttonEvent );
Button is pressed, forward
Class Elevator
89 ButtonEvent to represents the
90 // start moving Elevator to destination ButtonListener
Floor andElevator traveling
91 setMoving( true ); signal Elevator to move between two Floors,
92 }
to other Floor operating
93
94 // invoked when elevatorButton has been reset
asynchronously with
95 public void buttonReset( ButtonEvent buttonEvent ) other objects.
When Elevator’s
96 {
97 // send ButtonEvent to listener
Button is reset,
Lines 84-92
98 elevatorButtonListener.buttonReset( forward ButtonEvent
99 buttonEvent ); to ButtonListener
100 } Lines 95-100
101 } // end anonymous inner class
102 ); Lines 107-124
103
104 // anonymous inner class listens for DoorEvents from
105 // elevatorDoor
106 elevatorDoor.addDoorListener( Instantiate anonymous inner class to listen for
107 new DoorListener() { DoorEvents from Elevator’s Door
108

 2003 Prentice Hall, Inc.


All rights reserved.
109 // invoked when elevatorDoor has opened Outline
110 public void doorOpened( DoorEvent doorEvent )
111 { When Elevator’s
112 // send DoorEvent to listener Door is opened or
Elevator.java
113 elevatorDoorListener.doorOpened( new DoorEvent( closed,
114 doorEvent.getSource(), Elevator.this ));
Classopen or close
Elevator
Door on Floor
represents the
115 }
116 (Location), and
Elevator traveling
117 // invoked when elevatorDoor has closed DoorEvent
sendbetween to
two Floors,
118 public void doorClosed( DoorEvent doorEvent ) DoorListener
operating
119 {
120 // send DoorEvent to listener
asynchronously with
121 elevatorDoorListener.doorClosed( new DoorEvent( other objects.
122 doorEvent.getSource(), Elevator.this ));
123 } Lines 110-123
124 } // end anonymous inner class
125 );
126 } // end Elevator constructor Lines 129-134
127
128 // swaps current Floor Location with opposite Floor Location
129 private void changeFloors()
private method for changing
130 { destination Location
131 Location location = currentFloorLocation;
132 currentFloorLocation = destinationFloorLocation;
133 destinationFloorLocation = location;‘
134 }

 2003 Prentice Hall, Inc.


All rights reserved.
135 Outline
136 // start Elevator thread
137 public void start() Start Elevator’s Thread
138 { Elevator.java
139 if ( thread == null )
140 thread = new Thread( this );
Class Elevator
141 represents the
142 elevatorRunning = true; Elevator traveling
143 thread.start(); between two Floors,
144 }
operating
145
146 // stop Elevator thread; method run terminates
asynchronously with
147 public void stopElevator() otherThread
Stop Elevator’s objects.
148 {
149 elevatorRunning = false; Lines 137-144
150 }
151
152 // Elevator thread's run method Lines 147-150
153 public void run() Invoked after ElevatorShaft
154 { invokes Elevator’s Start
Lines method
153-187
155 while ( isElevatorRunning() ) {
156
157 // remain idle until awoken
Lines 178-179
158 while ( !isMoving() ) When Elevator maintains “waiting
159 pauseThread( 10 ); state,” the Elevator should not move
160
161 // pause while passenger exits (if one exists)
162 pauseThread( ONE_SECOND );

 2003 Prentice Hall, Inc.


All rights reserved.
163 Outline
164 // close elevatorDoor
165 getDoor().closeDoor( currentFloorLocation );
166 When ElevatorElevator.java
maintains “moving
167 // closing Door takes one second Class Elevator
168 pauseThread( ONE_SECOND );
state,” close Elevator Door
represents the
169
170 // issue elevatorDeparted Event Elevator traveling
171 sendDepartureEvent( currentFloorLocation ); Notify listeners that two Floors,
between
172
Elevator has operating
departed
173 // Elevator needs 5 seconds to travel asynchronously with
174 pauseThread( TRAVEL_TIME );
175
other objects.
176 // stop Elevator Travel five seconds, then stop
177 setMoving( false ); Line 165
178
179 // swap Floor Locations
Line 171
180 changeFloors();
Change destination Floor
181
182 // issue elevatorArrived Event Lines 174-177
183 sendArrivalEvent( currentFloorLocation );
Notify listeners that
184
185 } // end while loop
Elevator hasLine 180
arrived
186
187 } // end method run Line 183
188

 2003 Prentice Hall, Inc.


All rights reserved.
189 // pause concurrent thread for number of milliseconds Private method for putting
190 private void pauseThread( int milliseconds ) Outline
Elevator’s Thread to sleep
191 {
192 try {
193 Thread.sleep( milliseconds ); Elevator.java
194 } Class Elevator
195
196 // handle if interrupted while sleeping
represents the
197 catch ( InterruptedException exception ) { Elevator traveling
198 exception.printStackTrace(); between two Floors,
199 } operating
200 } // end method pauseThread
asynchronously with
201
202 // return Button on Elevator
other objects.
Implement Location
203 public Button getButton() method getButton to
204 { Lines
return Elevator’s 190-200
Button
205 return elevatorButton;
206 }
Lines 203-206
207
208 // return Door on Elevator Implement Location
209 public Door getDoor() Lines 209-212
method getDoor to return
210 { Elevator’s Door
211 return elevatorDoor;
212 }
213
214 // set if Elevator should move
215 private void setMoving( boolean elevatorMoving )
216 {
217 moving = elevatorMoving;
218 }

 2003 Prentice Hall, Inc.


All rights reserved.
219 Outline
220 // is Elevator moving?
221 public boolean isMoving()
222 { Elevator.java
223 return moving;
224 }
Class Elevator
225 represents the
226 // is Elevator thread running? Elevator traveling
227 private boolean isElevatorRunning() between two Floors,
228 {
operating
229 return elevatorRunning;
230 }
asynchronously with
231 other objects.
232 // register ElevatorMoveListener for ElevatorMoveEvents
233 public void addElevatorMoveListener( Lines 233-237
234 ElevatorMoveListener listener ) Enable ElevatorMoveListeners
235 { to receive ElevatorMoveEvents
236 elevatorMoveListeners.add( listener ); Lines 240-243
237 }
238
239 // register BellListener fpr BellEvents
Enable BellListener
240 public void setBellListener( BellListener listener )
241 {
to receive BellEvents
242 bellListener = listener;
243 }
244

 2003 Prentice Hall, Inc.


All rights reserved.
245 // register ButtonListener for ButtonEvents
246 public void setButtonListener( ButtonListener listener ) Outline
247 {
248 elevatorButtonListener = listener;
Enable ButtonListener to
249 } receive ButtonEvents Elevator.java
250 Class Elevator
251 // register DoorListener for DoorEvents
Enable DoorListener
represents the
252 public void setDoorListener( DoorListener listener )
253 {
to receive DoorEvents
Elevator traveling
254 elevatorDoorListener = listener; between two Floors,
255 } operating
256 Private method asynchronously
for notifying all with
257 // notify all ElevatorMoveListeners of arrival ElevatorMoveListeners
other objects.that
258 private void sendArrivalEvent( Location location ) Elevator has arrived
259 {
260 // obtain iterator from Set Lines 246-249
261 Iterator iterator = elevatorMoveListeners.iterator();
262
Lines 252-255
263 // get next DoorListener
264 while ( iterator.hasNext() ) {
265 Lines 258-283
266 // get next ElevatorMoveListener from Set
267 ElevatorMoveListener listener =
268 ( ElevatorMoveListener ) iterator.next();
269
270 // send event to listener
271 listener.elevatorArrived( new
272 ElevatorMoveEvent( this, location ) );
273
274 } // end while loop

 2003 Prentice Hall, Inc.


All rights reserved.
275
276 // service queued request, if one exists If a queued request exists, Outline
277 if ( summoned ) { service that request (i.e.,
278 setMoving( true ); // start moving Elevator move to other Floor)
279 } Elevator.java
280 Class Elevator
281 summoned = false; // request has been serviced
Private method for notifying allthe
represents
282
283 } // end method sendArrivalEvent
ElevatorMoveListeners Elevatorthat traveling
284 Elevator has departed
between two Floors,
285 // notify all ElevatorMoveListeners of departure operating
286 private void sendDepartureEvent( Location location )
asynchronously with
287 {
288 // obtain iterator from Set
other objects.
289 Iterator iterator = elevatorMoveListeners.iterator();
290 Lines 277-279
291 // get next DoorListener
292 while ( iterator.hasNext() ) {
Lines 286-303
293
294 // get next ElevatorMoveListener from Set
295 ElevatorMoveListener listener =
296 ( ElevatorMoveListener ) iterator.next();
297
298 // send ElevatorMoveEvent to this listener
299 listener.elevatorDeparted( new ElevatorMoveEvent(
300 this, currentFloorLocation ) );
301
302 } // end while loop
303 } // end method sendDepartureEvent

 2003 Prentice Hall, Inc.


All rights reserved.
304 Public service to request
305 // request Elevator Outline
Elevator (used by Buttons)
306 public void requestElevator( Location location )
307 {
308 // if Elevator is idle Elevator.java
309 if ( !isMoving() ) { Elevator
310
If Elevator is idle andClass
servicing
311 // if Elevator is on same Floor of request
represents
the Floor of the request, send the
312 if ( location == currentFloorLocation ) elevatorArrived Elevator
event traveling
313 between two Floors,
314 // Elevator has already arrived; send arrival event operating
315 sendArrivalEvent( currentFloorLocation );
asynchronously with
316 If Elevator is idle and
317 // if Elevator is on opposite Floor of request
other objects.
servicing opposite Floor of the
318 else {
request, move to other Floor
319 setMoving( true ); // move to other Floor Lines 306-332
320 }
321 }
Line 315
322 else // if Elevator is moving
323
324 // if Elevator departed from same Floor as request Line 319
325 if ( location == currentFloorLocation )
326 summoned = true; If Person requests Elevator just after
Line 325-326
327
Elevator has left the Floor on which
328 // if Elevator is traveling to Floor of request,
329 // simply continue traveling the Person is waiting, Elevator must
330 “remember” to return to that Floor
331 } // end method requestElevator

 2003 Prentice Hall, Inc.


All rights reserved.
332 Outline
333 // invoked when bell has rung
334 public void bellRang( BellEvent bellEvent ) Notify BellListener
335 {
that Bell has rungElevator.java
336 // send event to bellLirdstener
337 if ( bellListener != null )
Class Elevator
338 bellListener.bellRang( bellEvent ); represents the
339 } Elevator traveling
340 between two Floors,
341 // get the currentFloorLocation of the Elevator
operating
342 public Location getCurrentFloor()
343 {
asynchronously with
344 return currentFloorLocation; other objects.
345 }
346 } Lines 334-339

 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.


1 // Person.java Outline
2 // Person riding the elevator
3 package com.deitel.jhtp5.elevator.model;
4 Person.java
5 // Java core packages
6 import java.util.*;
Class Person
7 represents the
8 // Deitel packages Person that rides the
9 import com.deitel.jhtp5.elevator.event.*; Elevator. The
10
Person operates
11 public class Person extends Thread {
12
asynchronously with
13 // identification number other objects.
Use class diagram to
14 private int ID = -1;
15 determine associations and
Lines 14-20
16 // represents whether Person is moving or waiting attributes of Person
17 private boolean moving;
18 Line 23
19 // reference to Location (either on Floor or in Elevator)
20 private Location location; Lines 26-34
21
22 // listener object for PersonMoveEvents
23 private PersonMoveListener personMoveListener;
Declare listener that
24 receives
25 // time in milliseconds to walk to Button on Floor PersonMoveEvent
Define constants that
26 private static final int TIME_TO_WALK = 3000;
indicate each type of event
27
that a Person may send

 2003 Prentice Hall, Inc.


All rights reserved.
28 // types of messages Person may send
29 public static final int PERSON_CREATED = 1; Outline
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; Person.java
Person constructor
33 public static final int PERSON_EXITING_ELEVATOR = 5; Person
34 public static final int PERSON_EXITED = 6; assigns uniqueClass
identifier
35
represents
and sets initial Floor on the
36 // Person constructor set initial location which PersonPerson is locatedthat rides the
37 public Person( int identifier, Location initialLocation ) Elevator. The
38 { Person operates
39 super();
asynchronously with
40
41 ID = identifier; // assign unique identifier
other objects.
42 location = initialLocation; // set Floor Location
43 moving = true; // start moving toward Button on Floor Lines 37-44
44 }
45
Lines 47-51
46 // set listener for PersonMoveEvents
Enable PersonMoveListener
47 public void setPersonMoveListener(
48 PersonMoveListener listener ) to receive PersonMoveEvents
Lines 54-57
49 {
50 personMoveListener = listener;
51 }
When Door opens, set
52 Person’s Location to that
53 // set Person Location of where the Door opened
54 private void setLocation( Location newLocation )
55 {
56 location = newLocation;
57 }

 2003 Prentice Hall, Inc.


All rights reserved.
58
59 // get current Location Outline
60 private Location getLocation()
61 {
62 return location; Person.java
63 } Class Person
64
65 // get identifier
represents the
66 public int getID() Person that rides the
67 { Elevator. The
68 return ID; Person operates
69 }
asynchronously with
70
71 // set if Person should move
other objects.
72 public void setMoving( boolean personMoving )
73 { Lines 84-204
74 moving = personMoving;
75 }
Line 87
76
77 // get if Person should move
78 public boolean isMoving()
Invoked when Person’s
79 { Thread is started
80 return moving;
81 }
82
83 // Person either rides or waits for Elevator
84 public void run() Notify listeners when
85 { Person is created
86 // indicate that Person thread was created
87 sendPersonMoveEvent( PERSON_CREATED );

 2003 Prentice Hall, Inc.


All rights reserved.
88
89 // walk to Elevator Put Person Thread to sleep Outline
for
90 pauseThread( TIME_TO_WALK ); three seconds, simulating a three
91
second walk to the Elevator
92 // stop walking at Elevator Person.java
93 setMoving( false ); Class Person
94
95 // Person arrived at Elevator
represents the
96 sendPersonMoveEvent( PERSON_ARRIVED ); Person that rides the
97 Elevator.
Notify listeners when PersonThe
98 // get Door on current Floor Person operates
arrived at Elevator
99 Door currentFloorDoor = location.getDoor();
asynchronously with
100
101 // get Elevator
other objects.
102 Elevator elevator =
103 ( (Floor) getLocation() ).getElevatorShaft().getElevator(); Line 90
104
105 // begin exclusive access to currentFloorDoor
Line 96
106 synchronized ( currentFloorDoor ) { If Door is closed, press
107 Button on Floor and wait
108 // check whether Floor Door is open Lines 109-117
109 if ( !currentFloorDoor.isDoorOpen() ) {
for Elevator to arrive
110
111 sendPersonMoveEvent( PERSON_PRESSING_BUTTON );
112 pauseThread( 1000 );
113
114 // press Floor's Button to request Elevator
115 Button floorButton = getLocation().getButton();
116 floorButton.pressButton( getLocation() );
117 }

 2003 Prentice Hall, Inc.


All rights reserved.
118 Outline
119 // wait for Floor door to open
120 try {
121 Person.java
122 while ( !currentFloorDoor.isDoorOpen() )
123 currentFloorDoor.wait();
Class Person
124 }
Wait for the represents the
125 currentFloorDoor to Person that rides the
126 // handle exception waiting for Floor door to open open Elevator. The
127 catch ( InterruptedException interruptedException ) {
Person operates
128 interruptedException.printStackTrace();
129 }
asynchronously with
130 other objects.
131 // Floor Door takes one second to open
132 pauseThread( 1000 ); Lines 122-123
133
134 // implicitly wait for exclusive access to elevator
135 synchronized ( elevator ) { Lines 135-158
136 Only one Person is allowed to
137 // Person enters Elevator occupy the Elevator at one time Line 138
138 sendPersonMoveEvent( PERSON_ENTERING_ELEVATOR );
139 Notify listeners when Person
140 // set Person Location to Elevator
LineElevator
entered 141
141 setLocation( elevator );
142 Set the Person’s
143 // Person takes one second to enter Elevator
location to the
144 pauseThread( 1000 );
Elevator

 2003 Prentice Hall, Inc.


All rights reserved.
145
146 // pressing Elevator Button takes one second Outline
Notify listeners when Person
147 sendPersonMoveEvent( PERSON_PRESSING_BUTTON );
148 pauseThread( 1000 );
pressed Button
149 Person.java
150 // get Elevator's Button Class Person
151 Button elevatorButton = getLocation().getButton();
152
represents the
153 // press Elevator's Button Person that rides the
154 elevatorButton.pressButton( location ); Elevator. The
155
Press the Elevator’s button Person
to operates
156 // Door closing takes one second
instruct the Elevator to beginasynchronously with
157 pauseThread( 1000 );
158 } traveling other objects.
159
160 } // give up exclusive access to Floor door Line 147
161
162 // get exclusive access to Elevator
Line 154
163 synchronized( elevator ) {
164
165 // get Elevator door Lines 173-174
166 Door elevatorDoor = getLocation().getDoor();
167
168 // wait for Elevator door to open
169 synchronized( elevatorDoor ) {
170
171 try { Invoke method wait on the
172 elevatorDoor
173 while ( !elevatorDoor.isDoorOpen() )
174 elevatorDoor.wait();
175 }
 2003 Prentice Hall, Inc.
All rights reserved.
176
177 // handle exception waiting for Elevator door to open
Outline
178 catch ( InterruptedException interruptedException ) {
179 interruptedException.printStackTrace();
180 } Person.java
181 Class Person
182 // waiting for Elevator's Door to open takes a second represents the
183 pauseThread( 1000 ); Person that rides the
184
185 // move Person onto Floor
Elevator. The
186 setLocation( elevator.getCurrentFloor() ); Person operates
187 asynchronously with
188 // walk away from Elevator Set Person’s new location to Floor
other objects.
189 setMoving( true );
190
// Person exiting ElevatorPerson walks away from
191 the Line 186
192 Elevator);
sendPersonMoveEvent( PERSON_EXITING_ELEVATOR
193 Line 189
194 } // release elevatorDoor lock, allowing door to close
195
Line 202
196 } // release elevator lock, allowing waiting Person to enter
197
198 // walking from elevator takes five seconds
199 pauseThread( 2 * TIME_TO_WALK );
200
201 // Person exits simulation
202 sendPersonMoveEvent( PERSON_EXITED );
203
204 } // end method run
Person left the simulation

 2003 Prentice Hall, Inc.


All rights reserved.
205 Outline
206 // pause thread for desired number of milliseconds
Utility method for putting
207 private void pauseThread( int milliseconds )
208 { Person’s Thread to sleep
Person.java
209 try {
210 sleep( milliseconds );
Class Person
211 } represents the
212 Person that rides the
213 // handle exception if interrupted when paused Elevator. The
214 catch ( InterruptedException interruptedException ) {
Person operates
215 interruptedException.printStackTrace();
216 }
asynchronously with
217 } // end method pauseThread other objects.
Utility method for
218
219 // send PersonMoveEvent to listener, depending on event type determining which
Lines 207-217
220 private void sendPersonMoveEvent( int eventType ) PersonMoveEvent
221 { to send to listener, then
222 // create new event Lines 220-262
sending that event
223 PersonMoveEvent event =
224 new PersonMoveEvent( this, getLocation(), getID() );
225
226 // send Event to this listener, depending on eventType
227 switch ( eventType ) {
228
229 // Person has been created
230 case PERSON_CREATED:
231 personMoveListener.personCreated( event );
232 break;

 2003 Prentice Hall, Inc.


All rights reserved.
233
234 // Person arrived at Elevator Outline
235 case PERSON_ARRIVED:
236 personMoveListener.personArrived( event );
237 break; Person.java
238 Class Person
239 // Person entered Elevator
240 case PERSON_ENTERING_ELEVATOR:
represents the
241 personMoveListener.personEntered( event ); Person that rides the
242 break; Elevator. The
243 Person operates
244 // Person pressed Button object
asynchronously with
245 case PERSON_PRESSING_BUTTON:
246 personMoveListener.personPressedButton( event );
other objects.
247 break;
248
249 // Person exited Elevator
250 case PERSON_EXITING_ELEVATOR:
251 personMoveListener.personDeparted( event );
252 break;
253
254 // Person exited simulation
255 case PERSON_EXITED:
256 personMoveListener.personExited( event );
257 break;
258
259 default:
260 break;
261 }
262 } // end method sendPersonMoveEvent
263 }
 2003 Prentice Hall, Inc.
All rights reserved.
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.


Fig. E.15 Artifacts for package model.
model
<<file>>
<<file>> <<file>>
Bell.java
Bell.java Button.java

<<file>> <<file>>

Door.java Elevator.java

<<file>> <<file>>

ElevatorSimulation.java ElevatorShaft.java

<<file>> <<file>>

Floor.java Light.java

<<file>> <<file>>

Location.java Person.java

<<file>>

ElevatorDoor.java

event
<<imports>>

 2003 Prentice Hall, Inc. All rights reserved.


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.

Você também pode gostar