Outline
G.1 Introduction
G.2 Events
G.3 Listeners
G.4 Component Diagrams Revisited
G.1 (Introduction)
Event handling
Object register as “listeners” for events
The class of that object must implement a “listener” interface
G.2 (Events)
Figures G.1-G.7 contain system events
Each event inherits from class ElevatorModelEvent
// ElevatorModelEvent.java
2 // Basic event packet in Elevator simulation
3 package com.deitel.jhtp4.elevator.event;
4
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 }
// 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 }
// BellEvent.java
2 // Indicates that Bell has rung
3 package com.deitel.jhtp4.elevator.event;
4
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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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.*;
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 }
G.3 (Listeners)
// BellListener.java
2 // Method invoked when Bell has rung
3 package com.deitel.jhtp4.elevator.event;
4
5 public interface BellListener {
6
7 // invoked when Bell has rung
8 public void bellRang( BellEvent bellEvent );
9 }
// 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 }
// 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 }
// 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
10 // invoked when Elevator has arrived
11 public void elevatorArrived( ElevatorMoveEvent moveEvent );
12 }
// 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 }
// 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 }
// 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 }
G.4 (Component Diagrams Revisited)
Component diagram for events
Components in package event
Each component maps to a class in Figure G.1-G.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
Wednesday, March 31, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment