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.

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

(lines 57–65). Line 64 of method doorOpened interrupts the Person’s thread, which<br />

terminates method sleep of line 135. Method interrupt throws an InterruptedException<br />

received by a catch block (lines 147–155). Line 150 of this<br />

catch block unregisters the Person with the Door on the Floor, and line 154 calls<br />

private method enterAndRideEleva<strong>to</strong>r (lines 175–214)—however, according <strong>to</strong><br />

Fig. 5.29, if the Door on the Floor is already open when the Person arrives, the<br />

Person calls method enterAndRideEleva<strong>to</strong>r immediately.<br />

Line 178 of method enterAndRideEleva<strong>to</strong>r sends a personEntered event<br />

<strong>to</strong> personMoveListener, indicating that the Person is entering the Eleva<strong>to</strong>r.<br />

When the Person enters the Eleva<strong>to</strong>r, the Person’s Location references the Eleva<strong>to</strong>r<br />

(lines 181–183). When the Person has entered the Eleva<strong>to</strong>r, lines 189–190<br />

register the Person as a DoorListener with the Door in the Eleva<strong>to</strong>r. Lines 197–<br />

200 allow the Person <strong>to</strong> press the But<strong>to</strong>n in the Eleva<strong>to</strong>r and <strong>to</strong> send a person-<br />

PressedBut<strong>to</strong>n event <strong>to</strong> the personMoveListener. Lines 206–207 invoke the<br />

synchronized method ride in the Eleva<strong>to</strong>r, ensuring that other Persons cannot<br />

occupy the Eleva<strong>to</strong>r. When the Eleva<strong>to</strong>r arrives, it sends an eleva<strong>to</strong>rArrived<br />

event <strong>to</strong> the Door in the Eleva<strong>to</strong>r, which opens that Door and invokes method<br />

doorOpened of the Person. Method doorOpened, as previously mentioned, interrupts<br />

the Person’s thread—in this case, method interrupt terminates the sleep in<br />

method ride and allows the Person <strong>to</strong> exit the Eleva<strong>to</strong>r (allowing a Person waiting<br />

for the Eleva<strong>to</strong>r <strong>to</strong> enter). Method enterAndRideEleva<strong>to</strong>r returns, and lines 161–<br />

170 of method run make the Person exit the Eleva<strong>to</strong>r and exit the simulation shortly<br />

thereafter.<br />

H.10 Component Diagrams Revisited<br />

In Section 13.17, we introduced the component diagram for the eleva<strong>to</strong>r simulation. In our<br />

simulation, the each class in the model imports package event—we showed the components<br />

of package event in Fig. G.15. Figure H.14 presents the component diagram for<br />

package model. Each component in package model maps <strong>to</strong> a class from the class diagram<br />

of Fig. 15.21—package model aggregates package event.<br />

H.11 Conclusion<br />

This concludes discussion case study model. We hope you have enjoyed the design process<br />

of our eleva<strong>to</strong>r simulation using the UML, along with the presentation of object-oriented<br />

fundamentals and <strong>Java</strong>-specific <strong>to</strong>pics, such as event handling and multithreading. Using<br />

the concepts discussed in this case study, you should now be able <strong>to</strong> tackle even larger systems.<br />

We encourage you <strong>to</strong> read Appendix I, which implements the Eleva<strong>to</strong>rView,<br />

which transforms the Eleva<strong>to</strong>rModel that we have designed in<strong>to</strong> a vibrant and interactive<br />

program abundant with graphics, animation and sound.

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

Saved successfully!

Ooh no, something went wrong!