26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1426 Eleva<strong>to</strong>r Model (on CD) Appendix H<br />

268 // register But<strong>to</strong>nListener for But<strong>to</strong>nEvents<br />

269 public void setBut<strong>to</strong>nListener( But<strong>to</strong>nListener listener )<br />

270 {<br />

271 eleva<strong>to</strong>rBut<strong>to</strong>nListener = listener;<br />

272 }<br />

273<br />

274 // register DoorListener for DoorEvents<br />

275 public void setDoorListener( DoorListener listener )<br />

276 {<br />

277 eleva<strong>to</strong>rDoorListener = listener;<br />

278 }<br />

279<br />

280 // register BellListener fpr BellEvents<br />

281 public void setBellListener( BellListener listener )<br />

282 {<br />

283 bellListener = listener;<br />

284 }<br />

285<br />

286 // notify all Eleva<strong>to</strong>rMoveListeners of arrival<br />

287 private void sendArrivalEvent( Location location )<br />

288 {<br />

289 // obtain itera<strong>to</strong>r from Set<br />

290 Itera<strong>to</strong>r itera<strong>to</strong>r = eleva<strong>to</strong>rMoveListeners.itera<strong>to</strong>r();<br />

291<br />

292 // get next DoorListener<br />

293 while ( itera<strong>to</strong>r.hasNext() ) {<br />

294<br />

295 // get next Eleva<strong>to</strong>rMoveListener from Set<br />

296 Eleva<strong>to</strong>rMoveListener listener =<br />

297 ( Eleva<strong>to</strong>rMoveListener ) itera<strong>to</strong>r.next();<br />

298<br />

299 // send event <strong>to</strong> listener<br />

300 listener.eleva<strong>to</strong>rArrived( new<br />

301 Eleva<strong>to</strong>rMoveEvent( this, location ) );<br />

302<br />

303 } // end while loop<br />

304<br />

305 // service queued request, if one exists<br />

306 if ( summoned ) {<br />

307 pauseThread( Door.AUTOMATIC_CLOSE_DELAY );<br />

308 setMoving( true ); // start moving Eleva<strong>to</strong>r<br />

309 }<br />

310<br />

311 summoned = false; // request has been serviced<br />

312<br />

313 } // end method sendArrivalEvent<br />

314<br />

315 // notify all Eleva<strong>to</strong>rMoveListeners of departure<br />

316 private void sendDepartureEvent( Location location )<br />

317 {<br />

318 // obtain itera<strong>to</strong>r from Set<br />

319 Itera<strong>to</strong>r itera<strong>to</strong>r = eleva<strong>to</strong>rMoveListeners.itera<strong>to</strong>r();<br />

Fig. Fig. H.12 H.12 Class Eleva<strong>to</strong>r represents the Eleva<strong>to</strong>r traveling between two<br />

Floors, operating asynchronously with other objects (part 7 of 9).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!