Sei sulla pagina 1di 9

2002 by Prentice-Hall, Inc.

G
Elevator Events
and Listener Interfaces
(on CD)
G.1 Introduction
In Thinking About Objects Section 10.22, we discussed how event handling works in our
elevator simulation. We mentioned that for an object to receive an event, that object must
register as a listener for that event. Therefore, the class of that object must implement an
appropriate listener interface that contains methods that receive an event object as a param-
eter. In this section, we present the events and listener interfaces used in our simulation.
G.2 Events
The next eight figures (Fig. G.1Fig. G.7) contain the events of the system. Each event in-
herits from class ElevatorModelEvent in Fig. G.1. This class contains a Location
reference (line 11) that represent where the event was generatedin our simulation, this
reference is the Elevator object or either Floor object. Class ElevatorModelEv-
ent also contains an Object reference (line 14) representing the source object that gen-
erated the event. Methods getLocation (lines 3033) and getSource (lines 4245)
return the Location and Object references, respectively. Note that each subclass of
ElevatorModelEvent (Fig. G.2Fig. G.7) provides only a constructor calling the con-
structor of class ElevatorModelEvent. As we mentioned in Section 10.22, dividing
class ElevatorModelEvent into several subclass events makes event handling easier
to understand in our simulation.
1 // ElevatorModelEvent.java
2 // Basic event packet in Elevator simulation
3 package com.deitel.jhtp4.elevator.event;
4
Fig. G.1 ElevatorModelEvent superclass for events in the elevator simulation
model (part 1 of 2).
1385 Elevator Events and Listener Interfaces (on CD) Appendix G
5 // Deitel packages
6 import com.deitel.jhtp4.elevator.model.*;
7
8 public class ElevatorModelEvent {
9
10 // Location where ElevatorModelEvent was generated
11 private Location location;
12
13 // source Object that generated ElevatorModelEvent
14 private Object source;
15
16 // ElevatorModelEvent constructor sets Location
17 public ElevatorModelEvent( Object source, Location location )
18 {
19 setSource( source );
20 setLocation( location );
21 }
22
23 // set ElevatorModelEvent Location
24 public void setLocation( Location eventLocation )
25 {
26 location = eventLocation;
27 }
28
29 // get ElevatorModelEvent Location
30 public Location getLocation()
31 {
32 return location;
33 }
34
35 // set ElevatorModelEvent source
36 private void setSource( Object eventSource )
37 {
38 source = eventSource;
39 }
40
41 // get ElevatorModelEvent source
42 public Object getSource()
43 {
44 return source;
45 }
46 }
1 // BellEvent.java
2 // Indicates that Bell has rung
3 package com.deitel.jhtp4.elevator.event;
4
Fig. G.2 BellEvent ElevatorModelEvent subclass indicating that the
Bell has rung (part 1 of 2).
Fig. G.1 ElevatorModelEvent superclass for events in the elevator simulation
model (part 2 of 2).
Appendix G Elevator Events and Listener Interfaces (on CD) 1386
5 // Deitel packages
6 import com.deitel.jhtp4.elevator.model*;
7
8 public class BellEvent extends ElevatorModelEvent {
9
10 // BellEvent constructor
11 public BellEvent( Object source, Location location )
12 {
13 super( source, location );
14 }
15 }
1 // ButtonEvent.java
2 // Indicates that a Button has changed state
3 package com.deitel.jhtp4.elevator.event;
4
5 // Deitel packages
6 import com.deitel.jhtp4.elevator.model.*;
7
8 public class ButtonEvent extends ElevatorModelEvent {
9
10 // ButtonEvent constructor
11 public ButtonEvent( Object source, Location location )
12 {
13 super( source, location );
14 }
15 }
Fig. G.3 ButtonEvent ElevatorModelEvent subclass indicating that a
Button has changed state.
1 // DoorEvent.java
2 // Indicates that a Door has changed state
3 package com.deitel.jhtp4.elevator.event;
4
5 // Deitel packages
6 import com.deitel.jhtp4.elevator.model.*;
7
8 public class DoorEvent extends ElevatorModelEvent {
9
10 // DoorEvent constructor
11 public DoorEvent( Object source, Location location )
12 {
13 super( source, location );
14 }
15 }
Fig. G.4 DoorEvent ElevatorModelEvent subclass indicating that a Door
has changed state.
Fig. G.2 BellEvent ElevatorModelEvent subclass indicating that the
Bell has rung (part 2 of 2).
1387 Elevator Events and Listener Interfaces (on CD) Appendix G
Class PersonMoveEvent (Fig. G.7) has a slightly different structure than that of the
other event classes. Line 11 declares int attribute ID. We will discover in Appendix I that
the ElevatorView obtains this attribute through method getID (lines 2225) to deter-
mine which Person sent the event.
1 // ElevatorMoveEvent.java
2 // Indicates on which Floor the Elevator arrived or departed
3 package com.deitel.jhtp4.elevator.event;
4
5 // Deitel packages
6 import com.deitel.jhtp4.elevator.model.*;
7
8 public class ElevatorMoveEvent extends ElevatorModelEvent {
9
10 // ElevatorMoveEvent constructor
11 public ElevatorMoveEvent( Object source, Location location )
12 {
13 super( source, location );
14 }
15 }
Fig. G.5 ElevatorMoveEvent ElevatorModelEvent subclass indicating
on which Floor the Elevator has either arrived or departed.
1 // LightEvent.java
2 // Indicates on which Floor the Light has changed state
3 package com.deitel.jhtp4.elevator.event;
4
5 // Deitel packages
6 import com.deitel.jhtp4.elevator.model.*;
7
8 public class LightEvent extends ElevatorModelEvent {
9
10 // LightEvent constructor
11 public LightEvent( Object source, Location location )
12 {
13 super( source, location );
14 }
15 }
Fig. G.6 LightEvent ElevatorModelEvent subclass indicating on which
Floor the Light has changed state.
1 // PersonMoveEvent.java
2 // Indicates that a Person has moved
3 package com.deitel.jhtp4.elevator.event;
4
5 // Deitel packages
6 import com.deitel.jhtp4.elevator.model.*;
Fig. G.7 PersonMoveEventElevatorModelEvent subclass indicating that
a Person has moved (part 1 of 2).
Appendix G Elevator Events and Listener Interfaces (on CD) 1388
G.3 Listeners
The next eight figures (Fig. G.8Fig. G.14) contain the listener interfaces for the elevator
simulation. BellListener (Fig. G.8) provides method bellRang (lines 8), which is
invoked when the Bell has rung. ButtonListener (Fig. G.9) provides methods but-
tonPressed (line 8) and buttonReset (line 11), which listen when a Button is
pressed or reset. DoorListener (Fig. G.10) provides methods doorOpened (line 8)
and doorClosed (line 11), which listen for a Door opening or closing. Elevator-
MoveListener (Fig. G.11) provides methods elevatorDeparted (line 8) and el-
evatorArrived (line 11), which listen for Elevator departures and arrivals.
LightListener (Fig. G.12) provides methods lightTurnedOn (line 8) and
lightTurnedOff (line 11) that listen for Light state changes. PersonMoveLis-
tener (Fig. G.13) provides methods personCreated (line 8), personArrived
(line 11), personDeparted (line 14), personPressedButton (line 1718), per-
sonEntered (line 21) and personExited (line 24). These methods listen for when a
Person has been created, has arrived at or departed from the Elevator, pressed a But-
ton, entered the Elevator, or exited the simulation, respectively. Lastly, Elevator-
ModelListener (Fig. G.14) inherits behaviors from all listener interfaces. The
ElevatorView uses interface ElevatorModelListener in Section 13.17 and
Appendix I to receive events from the ElevatorModel.
7
8 public class PersonMoveEvent extends ElevatorModelEvent {
9
10 // identifier of Person sending Event
11 private int ID;
12
13 // PersonMoveEvent constructor
14 public PersonMoveEvent( Object source, Location location,
15 int identifier )
16 {
17 super( source, location );
18 ID = identifier;
19 }
20
21 // return identifier
22 public int getID()
23 {
24 return( ID );
25 }
26 }
1 // BellListener.java
2 // Method invoked when Bell has rung
3 package com.deitel.jhtp4.elevator.event;
4
Fig. G.8 Interface BellListener method when Bell has rung (part 1 of 2).
Fig. G.7 PersonMoveEventElevatorModelEvent subclass indicating that
a Person has moved (part 2 of 2).
1389 Elevator Events and Listener Interfaces (on CD) Appendix G
5 public interface BellListener {
6
7 // invoked when Bell has rungs
8 public void bellRang( BellEvent bellEvent );
9 }
1 // ButtonListener.java
2 // Methods invoked when Button has been either pressed or reset
3 package com.deitel.jhtp4.elevator.event;
4
5 public interface ButtonListener {
6
7 // invoked when Button has been pressed
8 public void buttonPressed( ButtonEvent buttonEvent );
9
10 // invoked when Button has been reset
11 public void buttonReset( ButtonEvent buttonEvent );
12 }
Fig. G.9 Interface ButtonListener methods when Button has been either
pressed or reset.
1 // DoorListener.java
2 // Methods invoked when Door has either opened or closed
3 package com.deitel.jhtp4.elevator.event;
4
5 public interface DoorListener {
6
7 // invoked when Door has opened
8 public void doorOpened( DoorEvent doorEvent );
9
10 // invoked when Door has closed
11 public void doorClosed( DoorEvent doorEvent );
12 }
Fig. G.10 Interface DoorListener methods when Door has either opened or
closed.
1 // ElevatorMoveListener.java
2 // Methods invoked when Elevator has either departed or arrived
3 package com.deitel.jhtp4.elevator.event;
4
5 public interface ElevatorMoveListener {
6
7 // invoked when Elevator has departed
8 public void elevatorDeparted( ElevatorMoveEvent moveEvent );
9
Fig. G.11 Interface ElevatorMoveListener methods when Elevator has
either departed from or arrived on a Floor (part 1 of 2).
Fig. G.8 Interface BellListener method when Bell has rung (part 2 of 2).
Appendix G Elevator Events and Listener Interfaces (on CD) 1390
10 // invoked when Elevator has arrived
11 public void elevatorArrived( ElevatorMoveEvent moveEvent );
12 }
1 // LightListener.java
2 // Methods invoked when Light has either turned on or off
3 package com.deitel.jhtp4.elevator.event;
4
5 public interface LightListener {
6
7 // invoked when Light has turned on
8 public void lightTurnedOn( LightEvent lightEvent );
9
10 // invoked when Light has turned off
11 public void lightTurnedOff( LightEvent lightEvent );
12 }
Fig. G.12 Interface LightListener method for when Light has either
turned on or off.
1 // PersonMoveListener.java
2 // Methods invoked when Person moved
3 package com.deitel.jhtp4.elevator.event;
4
5 public interface PersonMoveListener {
6
7 // invoked when Person has been instantiated in model
8 public void personCreated( PersonMoveEvent moveEvent );
9
10 // invoked when Person arrived at elevator
11 public void personArrived( PersonMoveEvent moveEvent );
12
13 // invoked when Person departed from elevator
14 public void personDeparted( PersonMoveEvent moveEvent );
15
16 // invoked when Person pressed Button
17 public void personPressedButton(
18 PersonMoveEvent moveEvent );
19
20 // invoked when Person entered Elevator
21 public void personEntered( PersonMoveEvent moveEvent );
22
23 // invoked when Person exited simulation
24 public void personExited( PersonMoveEvent moveEvent );
25 }
Fig. G.13 Interface PersonMoveListener methods when Person has
moved.
Fig. G.11 Interface ElevatorMoveListener methods when Elevator has
either departed from or arrived on a Floor (part 2 of 2).
1391 Elevator Events and Listener Interfaces (on CD) Appendix G
G.4 Component Diagrams Revisited
In Section 13.17, we introduced the component diagram for the elevator simulation. In our
simulation, the ElevatorView and every object in the model import package event.
Figure G.15 presents the component diagram for package event. Each component in
package event maps to a class from Fig. G.1Fig. G.14. According to the component di-
agram, ElevatorView.java of package view aggregates package event. In Java,
this aggregation translates to class ElevatorView importing package event. Also ac-
cording to Fig. G.15, package model aggregates package eventi.e., each component
in package model contains an aggregation with all components in pacckage event. (We
show all components of package model in a separate component diagram in Appendix H.)
In Java, this aggregation translates to each class in package model that imports package
event.
This concludes the appendix on the events and listener interfaces of the elevator sim-
ulation. We hope you have found it a useful reference for the material on event handling
discussed in Thinking About Objects Section 10.22. In the next two appendices, we
implement the design for the model and the view, and we provide the component diagrams
for packages model and view.
1 // ElevatorModelListener.java
2 // Listener for ElevatorView from ElevatorModel
3 package com.deitel.jhtp4.elevator.event;
4
5 // ElevatorModelListener inherits all Listener interfaces
6 public interface ElevatorModelListener extends BellListener,
7 ButtonListener, DoorListener, ElevatorMoveListener,
8 LightListener, PersonMoveListener {
9 }
Fig. G.14 Interface ElevatorModelListener allows the model to send all
events to the view.
Appendix G Elevator Events and Listener Interfaces (on CD) 1392
Fig. G.15 Component diagram for package event.
event
BellEvent.java
file
BellListener.java
file
ButtonEvent.java
file
ButtonListener.java
file
DoorEvent.java
file
DoorListener.java
file
ElevatorMoveEvent.java
file
ElevatorMoveListener.java
file
ElevatorModelEvent.java
file
ElevatorModelListener.java
file
LightEvent.java
file
LightListener.java
file
PersonMoveEvent.java
file
PersonMoveListener.java
file
view
ElevatorView.java
file
1
model
1
1
1

Potrebbero piacerti anche