Appendix G Elevator Events and Listener Interfaces Outline

  • Slides: 21
Download presentation
Appendix G – Elevator Events and Listener Interfaces Outline G. 1 G. 2 G.

Appendix G – Elevator Events and Listener Interfaces Outline G. 1 G. 2 G. 3 G. 4 Introduction Events Listeners Component Diagrams Revisited 2002 Prentice Hall, Inc. All rights reserved.

G. 1 Introduction • Event handling – Object register as “listeners” for events •

G. 1 Introduction • Event handling – Object register as “listeners” for events • The class of that object must implement a “listener” interface 2002 Prentice Hall, Inc. All rights reserved.

G. 2 Events • Figures G. 1 -G. 7 contain system events – Each

G. 2 Events • Figures G. 1 -G. 7 contain system events – Each event inherits from class Elevator. Model. Event 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // Elevator. Model. Event. java // Basic event packet in Elevator simulation package com. deitel. jhtp 4. elevator. event; // Deitel packages import com. deitel. jhtp 4. elevator. model. *; Outline Class Elevator. Model. Event is the superclass for all events in elevator simulation case study public class Elevator. Model. Event { // Location where Elevator. Model. Event was generated private Location location; // source Object that generated Elevator. Model. Event private Object source; // Elevator. Model. Event constructor sets Location public Elevator. Model. Event( Object source, Location { set. Source( source ); set. Location( location ); } // set Elevator. Model. Event Location public void set. Location( Location event. Location ) { location = event. Location; } Location object represents Fig. G. 1 where the event was generated Elevator. Model(i. e. , Floor or Elevator) Event superclass for events in the elevator simulation location ) model. Object object represents the actual Object that Line generated 8 the event Line 11 Line 14 // get Elevator. Model. Event Location public Location get. Location() { return location; } 2002 Prentice Hall, Inc. All rights reserved.

34 35 36 37 38 39 40 41 42 43 44 45 46 }

34 35 36 37 38 39 40 41 42 43 44 45 46 } // set Elevator. Model. Event source private void set. Source( Object event. Source ) { source = event. Source; } Outline // get Elevator. Model. Event source public Object get. Source() { return source; } Fig. G. 1 Elevator. Model. Event superclass for events in the elevator simulation model (Part 2). 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Button. Event. java // Indicates that a Button has changed state package com. deitel. jhtp 4. elevator. event; Outline // Deitel packages import com. deitel. jhtp 4. elevator. model. *; public class Button. Event extends Elevator. Model. Event { // Button. Event constructor public Button. Event( Object source, Location location ) { super( source, location ); } } Button. Event sent when Button as been pressed or reset Fig. G. 3 Button. Event Elevator. Model. Event subclass indicating that a Button has changed state. Lines 8 -15 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Door. Event. java // Indicates that a Door has changed state package com. deitel. jhtp 4. elevator. event; Outline // Deitel packages import com. deitel. jhtp 4. elevator. model. *; public class Door. Event extends Elevator. Model. Event { // Door. Event constructor public Door. Event( Object source, Location location ) { super( source, location ); } } Door. Event sent when Door has opened or closed Fig. G. 4 Door. Event Elevator. Model. Event subclass indicating that a Door has changed state. Lines 8 -15 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Light. Event. java // Indicates on which Floor the Light has changed state package com. deitel. jhtp 4. elevator. event; Outline // Deitel packages import com. deitel. jhtp 4. elevator. model. *; public class Light. Event extends Elevator. Model. Event { // Light. Event constructor public Light. Event( Object source, Location location ) { super( source, location ); } } Light. Event sent when Light has been turned on or off Fig. G. 6 Light. Event Elevator. Model. Event subclass indicating on which Floor the Light has changed state. Lines 8 -15 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // Person. Move. Event. java // Indicates that a Person has moved package com. deitel. jhtp 4. elevator. event; // Deitel packages import com. deitel. jhtp 4. elevator. model. *; public class Person. Move. Event extends Elevator. Model. Event { // identifier of Person sending Event private int ID; // Person. Move. Event constructor public Person. Move. Event( Object source, Location location, int identifier ) { super( source, location ); ID = identifier; } // return identifier public int get. ID() { return( ID ); } Outline Person. Move. Event sent when Person performs an action in simulation Fig. G. 7 ID for identifying the Person. Move. Event Person that sent the event Elevator. Model. Event subclass indicating that a Person has moved. Lines 8 -26 Line 11 } 2002 Prentice Hall, Inc. All rights reserved.

G. 3 Listeners • Figures G. 8 -G. 14 contain listener interfaces – Interface

G. 3 Listeners • Figures G. 8 -G. 14 contain listener interfaces – Interface methods receive as arguments various events 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 Outline // Bell. Listener. java

1 2 3 4 5 6 7 8 9 Outline // Bell. Listener. java // Method invoked when Bell has rung package com. deitel. jhtp 4. elevator. event; public interface Bell. Listener { // invoked when Bell has rung public void bell. Rang( Bell. Event bell. Event ); } Method invoked when Bell has rung Fig. G. 8 Interface Bell. Listener method when Bell has rung. Line 8 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 // Button.

1 2 3 4 5 6 7 8 9 10 11 12 // Button. Listener. java // Methods invoked when Button has been either pressed or reset package com. deitel. jhtp 4. elevator. event; Outline public interface Button. Listener { // invoked when Button has been pressed public void button. Pressed( Button. Event button. Event ); // invoked when Button has been reset public void button. Reset( Button. Event button. Event ); } Methods invoked Fig. G. 9 Interface when Button has Button. Listener been pressed or reset methods when Button has been either pressed or reset. Lines 8 -11 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 // Door.

1 2 3 4 5 6 7 8 9 10 11 12 // Door. Listener. java // Methods invoked when Door has either opened or closed package com. deitel. jhtp 4. elevator. event; Outline public interface Door. Listener { // invoked when Door has opened public void door. Opened( Door. Event door. Event ); // invoked when Door has closed public void door. Closed( Door. Event door. Event ); } Methods invoked when Door been Interface Fig. has G. 10 opened. Door. Listener or closed methods when Door has either opened or closed. Lines 8 -11 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 // Elevator.

1 2 3 4 5 6 7 8 9 10 11 12 // Elevator. Move. Listener. java // Methods invoked when Elevator has either departed or arrived package com. deitel. jhtp 4. elevator. event; Outline public interface Elevator. Move. Listener { // invoked when Elevator has departed public void elevator. Departed( Elevator. Move. Event move. Event ); // invoked when Elevator has arrived public void elevator. Arrived( Elevator. Move. Event move. Event ); } Fig. G. 11 Interface Elevator. Move. Methods invoked when methods Listener Elevator has arrived at when Elevator has or departed from a Floor either departed from or arrived on a Floor. Lines 8 -11 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 // Light.

1 2 3 4 5 6 7 8 9 10 11 12 // Light. Listener. java // Methods invoked when Light has either turned on or off package com. deitel. jhtp 4. elevator. event; Outline public interface Light. Listener { // invoked when Light has turned on public void light. Turned. On( Light. Event light. Event ); // invoked when Light has turned off public void light. Turned. Off( Light. Event light. Event ); } Methods invoked when Light has been turned on or off Fig. G. 12 Interface Light. Listener method for when Light has either turned on or off. Lines 8 -11 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Person. Move. Listener. java // Methods invoked when Person moved package com. deitel. jhtp 4. elevator. event; Outline public interface Person. Move. Listener { // invoked when Person has been instantiated in model public void person. Created( Person. Move. Event move. Event ); // invoked when Person arrived at elevator public void person. Arrived( Person. Move. Event move. Event ); // invoked when Person departed from elevator public void person. Departed( Person. Move. Event move. Event ); // invoked when Person pressed Button public void person. Pressed. Button( Person. Move. Event move. Event ); // invoked when Person entered Elevator public void person. Entered( Person. Move. Event move. Event ); // invoked when Person exited simulation public void person. Exited( Person. Move. Event move. Event ); Fig. G. 13 Interface Methods invoked Person. Movewhen Person Listener methods performs an action when Person has moved. Lines 8 -24 } 2002 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 // Elevator. Model. Listener. java

1 2 3 4 5 6 7 8 9 // Elevator. Model. Listener. java // Listener for Elevator. View from Elevator. Model package com. deitel. jhtp 4. elevator. event; Outline // Elevator. Model. Listener inherits all Listener interfaces public interface Elevator. Model. Listener extends Bell. Listener, Button. Listener, Door. Listener, Elevator. Move. Listener, Light. Listener, Person. Move. Listener { } Create interface that extends all listener interfaces Fig. G. 14 Interface Elevator. Model. Listener allows the model to send all events to the view. Lines 6 -9 2002 Prentice Hall, Inc. All rights reserved.

G. 4 Component Diagrams Revisited • Component diagram for events – Components in package

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 – Elevator. View. java aggregates package event • In Java, class Elevator. View imports package event – Package model aggregates package event • In Java, each class in model imports package event 2002 Prentice Hall, Inc. All rights reserved.

Fig. G. 15 Component Diagram for package event. 2002 Prentice Hall, Inc. All rights

Fig. G. 15 Component Diagram for package event. 2002 Prentice Hall, Inc. All rights reserved.