0% found this document useful (0 votes)
40 views21 pages

Appendix D - Elevator Events and Listener Interfaces: Outline

The document discusses events and listeners in an elevator simulation case study. It describes how objects can register as listeners for events, and how event classes inherit from the ElevatorSimulationEvent superclass. It then provides examples of different event classes like BellEvent, ButtonEvent, DoorEvent, and ElevatorMoveEvent that are used to indicate state changes in the elevator simulation model.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views21 pages

Appendix D - Elevator Events and Listener Interfaces: Outline

The document discusses events and listeners in an elevator simulation case study. It describes how objects can register as listeners for events, and how event classes inherit from the ElevatorSimulationEvent superclass. It then provides examples of different event classes like BellEvent, ButtonEvent, DoorEvent, and ElevatorMoveEvent that are used to indicate state changes in the elevator simulation model.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

1

Appendix D – Elevator Events and


Listener Interfaces
Outline
D.1 Introduction
D.2 Events
D.3 Listeners
D.4 Artifacts Revisited

 2001 Prentice Hall, Inc. All rights reserved.


2

D.1 Introduction

• Event handling
– Object register as “listeners” for events
• The class of that object must implement a “listener” interface

 2001 Prentice Hall, Inc. All rights reserved.


3

D.2 Events

• Figures D.1-D.7 contain system events


– Each event inherits from class
ElevatorSimulationEvent

 2001 Prentice Hall, Inc. All rights reserved.


1 // ElevatorSimulationEvent.java Outline
2 // Basic event packet in Elevator simulation
3 package com.deitel.jhtp5.elevator.event;
4 Class ElevatorSimulationEvent
ElevatorSimulat
5 // Deitel packages is the superclass for all events in elevator
ionEvent.java
6 import com.deitel.jhtp5.elevator.model.*; simulation case study
ElevatorSimulat
7 ion-Event
8 public class ElevatorSimulationEvent {
superclass for events
9
in the elevator
10 // Location where ElevatorSimulationEvent was generated object represents
Locationsimulation
11 private Location location;
model.
where the event was generated
12
(e.g., FloorLineor 8Elevator)
13 // source Object that generated ElevatorSimulationEvent
14 private Object source;
15 Line 11
16 // ElevatorSimulationEvent constructor sets Location
17 public ElevatorSimulationEvent( Object source, Location location ) Line 14
18 { Object source represents the
19 setSource( source ); actual Object that generated the
20 setLocation( location ); event
21 }
22

 2001 Prentice Hall, Inc.


All rights reserved.
23 // set ElevatorSimulationEvent Location Outline
24 public void setLocation( Location eventLocation )
25 {
26 location = eventLocation; ElevatorSimulat
27 } ionEvent.java
28 ElevatorSimulat
29 // get ElevatorSimulationEvent Location ion-Event
30 public Location getLocation()
superclass for events
31 {
in the elevator
32 return location;
33 }
simulation model.
34
35 // set ElevatorSimulationEvent source
36 private void setSource( Object eventSource )
37 {
38 source = eventSource;
39 }
40
41 // get ElevatorSimulationEvent source
42 public Object getSource()
43 {
44 return source;
45 }
46 }

 2001 Prentice Hall, Inc.


All rights reserved.
1 // BellEvent.java Outline
2 // Indicates that Bell has rung
3 package com.deitel.jhtp5.elevator.event;
4 BellEvent.java
5 // Deitel packages BellEvent
6 import com.deitel.jhtp5.elevator.model.*; ElevatorSimulat
7 ion-Event
BellEvent sent subclass
8 public class BellEvent extends ElevatorSimulationEvent {
indicating
when Bell that the
has rung
9
Bell has rung.
10 // BellEvent constructor
11 public BellEvent( Object source, Location location )
12 { Lines 8-15
13 super( source, location );
14 }
15 }

 2001 Prentice Hall, Inc.


All rights reserved.
1 // ButtonEvent.java Outline
2 // Indicates that a Button has changed state
3 package com.deitel.jhtp5.elevator.event;
4 ButtonEvent.jav
5 // Deitel packages a ButtonEvent
6 import com.deitel.jhtp5.elevator.model.*; ElevatorSimulat
7
sent subclass
ion-Event
ButtonEvent
8 public class ButtonEvent extends ElevatorSimulationEvent {
indicating
when Button as that a
9
10 // ButtonEvent constructor been pressed or resethas changed
Button
11 public ButtonEvent( Object source, Location location )
state.
12 {
13 super( source, location ); Lines 8-15
14 }
15 }

 2001 Prentice Hall, Inc.


All rights reserved.
1 // DoorEvent.java Outline
2 // Indicates that a Door has changed state
3 package com.deitel.jhtp5.elevator.event;
4 DoorEvent.java
5 // Deitel packages DoorEvent
6 import com.deitel.jhtp5.elevator.model.*; ElevatorSimulat
7
sent subclass
ion-Event
DoorEvent
8 public class DoorEvent extends ElevatorSimulationEvent {
9
when indicating
Door has that a Door
10 // DoorEvent constructor openedhas
or changed
closed state.
11 public DoorEvent( Object source, Location location )
12 { Lines 8-15
13 super( source, location );
14 }
15 }

 2001 Prentice Hall, Inc.


All rights reserved.
1 // ElevatorMoveEvent.java Outline
2 // Indicates on which Floor the Elevator arrived or departed
3 package com.deitel.jhtp5.elevator.event;
4 sent
ElevatorMoveEventElevatorMoveEve
5 // Deitel packages when Elevator arrivesnt.java
at or
6 import com.deitel.jhtp5.elevator.model.*; departs from FloorElevatorMove-
7 Event Elevator-
8 public class ElevatorMoveEvent extends ElevatorSimulationEvent {
ModelEvent
9
subclass indicating on
10 // ElevatorMoveEvent constructor
11 public ElevatorMoveEvent( Object source, Location location )
which Floor the
12 { Elevator has either
13 super( source, location ); arrived or departed.
14 }
15 } Lines 8-15

 2001 Prentice Hall, Inc.


All rights reserved.
1 // LightEvent.java Outline
2 // Indicates on which Floor the Light has changed state
3 package com.deitel.jhtp5.elevator.event;
4 LightEvent.java
5 // Deitel packages LightEvent
6 import com.deitel.jhtp5.elevator.model.*; ElevatorModel-
7
LightEvent sent subclass
Event
8 public class LightEvent extends ElevatorSimulationEvent {
when Lightindicating
has on which
9
10 // LightEvent constructor been turned on or offthe Light
Floor
11 public LightEvent( Object source, Location location )
has changed state.
12 {
13 super( source, location ); Lines 8-15
14 }
15 }

 2001 Prentice Hall, Inc.


All rights reserved.
1 // PersonMoveEvent.java Outline
2 // Indicates that a Person has moved
3 package com.deitel.jhtp5.elevator.event;
4 PersonMoveEvent
5 // Deitel packages .java
6 import com.deitel.jhtp5.elevator.model.*; PersonMoveEvent
7 PersonMoveEvent
ElevatorModel-
8 public class PersonMoveEvent extends ElevatorSimulationEvent { sent whensubclass
Person
Event
9 performs an that
action
indicating a in
10 // identifier of Person sending Event
simulation
Person has moved.
11 private int ID;
12
13 // PersonMoveEvent constructor
ID forLines
identifying
8-26 the
14 public PersonMoveEvent( Object source, Location location, Person that sent the event
15 int identifier ) Line 11
16 {
17 super( source, location );
18 ID = identifier;
19 }
20
21 // return identifier
22 public int getID()
23 {
24 return( ID );
25 }
26 }

 2001 Prentice Hall, Inc.


All rights reserved.
12

D.3 Listeners

• Figures D.8-D.14 contain listener interfaces


– Interface methods receive as arguments various events

 2001 Prentice Hall, Inc. All rights reserved.


1 // BellListener.java Outline
2 // Method invoked when Bell has rung
3 package com.deitel.jhtp5.elevator.event;
4 BellListener.ja
5 public interface BellListener { va
6
7 // invoked when Bell has rungs BellListener
Method invoked
8 public void bellRang( BellEvent bellEvent );
method when Bell
when Bell has rung
9 }
has rung.

Line 8

 2001 Prentice Hall, Inc.


All rights reserved.
1 // ButtonListener.java Outline
2 // Methods invoked when Button has been either pressed or reset
3 package com.deitel.jhtp5.elevator.event;
4 ButtonListener.
5 public interface ButtonListener { java
6
7 // invoked when Button has been pressed ButtonListener
8 public void buttonPressed( ButtonEvent buttonEvent );
methods when
9
Button has been
10 // invoked when Button has been reset
11 public void buttonReset( ButtonEvent buttonEvent );
either pressed or reset.
12 } Methods invoked
Lines 8-11
when Button has
been pressed or reset

 2001 Prentice Hall, Inc.


All rights reserved.
1 // DoorListener.java Outline
2 // Methods invoked when Door has either opened or closed
3 package com.deitel.jhtp5.elevator.event;
4 DoorListener.ja
5 public interface DoorListener { va
6 DoorListener
7 // invoked when Door has opened methods when Door
8 public void doorOpened( DoorEvent doorEvent );
has either opened or
9
closed.
10 // invoked when Door has closed
11 public void doorClosed( DoorEvent doorEvent );
12 } Lines 8-11
Methods invoked
when Door has been
opened or closed

 2001 Prentice Hall, Inc.


All rights reserved.
1 // ElevatorMoveListener.java Outline
2 // Methods invoked when Elevator has either departed or arrived
3 package com.deitel.jhtp5.elevator.event;
4 ElevatorMoveLis
5 public interface ElevatorMoveListener { tener.java
6
7 // invoked when Elevator has departed ElevatorMove-
8 public void elevatorDeparted( ElevatorMoveEvent moveEvent );
Listener methods
9
when Elevator has
10 // invoked when Elevator has arrived
11 public void elevatorArrived( ElevatorMoveEvent moveEvent );
either departed from or
12 } arrived on a Floor.

Lines when
Methods invoked 8-11
Elevator has arrived at
or departed from a Floor

 2001 Prentice Hall, Inc.


All rights reserved.
1 // LightListener.java Outline
2 // Methods invoked when Light has either turned on or off
3 package com.deitel.jhtp5.elevator.event;
4 LightListener.j
5 public interface LightListener { ava
6
7 // invoked when Light has turned on LightListener
8 public void lightTurnedOn( LightEvent lightEvent );
methodinvoked
Methods for when
9
10 // invoked when Light has turned off
Lighthas
when Light haseither
been
11 public void lightTurnedOff( LightEvent lightEvent );
turned
turned onon
oror
offoff.
12 }
Lines 8-11

 2001 Prentice Hall, Inc.


All rights reserved.
1 // PersonMoveListener.java Outline
2 // Methods invoked when Person moved
3 package com.deitel.jhtp5.elevator.event;
4 PersonMoveListe
5 public interface PersonMoveListener { ner.java
6
7 // invoked when Person has been instantiated in model PersonMove-
8 public void personCreated( PersonMoveEvent moveEvent );
Listener methods
9
when Person has
10 // invoked when Person arrived at elevator
11 public void personArrived( PersonMoveEvent moveEvent );
moved.
12
13 // invoked when Person departed from elevator Lines 8-24 invoked
Methods
14 public void personDeparted( PersonMoveEvent moveEvent ); when Person
15 performs an action
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 }

 2001 Prentice Hall, Inc.


All rights reserved.
1 // ElevatorSimulationListener.java Outline
2 // Listener for ElevatorView from ElevatorModel
3 package com.deitel.jhtp5.elevator.event;
4 ElevatorSimulat
5 // ElevatorSimulationListener inherits all Listener interfaces ionListener.jav
6 public interface ElevatorSimulationListener extends BellListener, a
7 ButtonListener, DoorListener, ElevatorMoveListener,
8 LightListener, PersonMoveListener {
ElevatorSimulat
9 }
ion-Listener
allows the model to
Convenience interface
send that to the
all events
extends all listener
view. interfaces

Lines 6-9

 2001 Prentice Hall, Inc.


All rights reserved.
20

D.4 Artifacts Revisited

• Artifacts diagram for events


– Artifacts in package event
• Each artifact maps to a class in Figure D.1-D.14
– ElevatorView.java aggregates package event
• In Java, class ElevatorView imports package event
– Package model aggregates package event
• In Java, each class in model imports package event

 2001 Prentice Hall, Inc. All rights reserved.


21

Fig. D.15 Artifacts for package event.


event

<< file
file>> << file>>
BellEvent.java
BellEvent.java BellListener.java

<< file>> << file>>


ButtonEvent.java ButtonListener.java

<< file>> << file>>


DoorEvent.java DoorListener.java

<< file>> << file>>


ElevatorMoveEvent.java ElevatorMoveListener.java

<< file>> << file>>


ElevatorSimulationEvent.java ElevatorSimulationListener.java

<< file>> << file>>


LightEvent.java LightListener.java

<< file>> << file>>


PersonMoveEvent.java PersonMoveListener.java

1 1

view model

<< file>> 1 1
ElevatorView.java

 2001 Prentice Hall, Inc. All rights reserved.

You might also like