1. Trang chủ
  2. » Thể loại khác

Java - profthinh ď jhtp5_appE

77 108 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 77
Dung lượng 412 KB

Nội dung

Java - profthinh ď jhtp5_appE tài liệu, giáo án, bài giảng , luận văn, luận án, đồ án, bài tập lớn về tất cả các lĩnh vự...

1 Appendix E – Elevator Model Outline E.1 E.2 E.3 E.4 E.5 E.6 E.7 E.8 E.9 E.10 E.11 Introduction Class ElevatorSimulation Classes Location and Floor Class Door and ElevatorDoor Class Button Class ElevatorShaft Classes Light and Bell Class Elevator Class Person Artifacts Revisited Conclusion 2003 Prentice Hall, Inc All rights reserved E.1 Introduction • Classes implement the MVC model  2003 Prentice Hall, Inc All rights reserved E.2 Class ElevatorSimulation • ElevatorSimulation – “Ties together” the objects that comprise the elevator simulation model – Sends events from MVC model to view – Instantiates Person object (as per user request) – Allows Floor to obtain reference to ElevatorShaft  2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // ElevatorSimulation.java // Elevator simulation model with ElevatorShaft and two Floors package com.deitel.jhtp5.elevator.model; // Java core packages import java.util.*; // Deitel packages import com.deitel.jhtp5.elevator.event.*; import com.deitel.jhtp5.elevator.ElevatorConstants; Outline ElevatorSimulat ElevatorSimulation implements ion.java ElevatorSimulationListener, which Class inherits from all listener interfaces ElevatorSimulat public class ElevatorSimulation implements ElevatorSimulationListener, ElevatorConstants { // declare two-Floor architecture in simulation private Floor firstFloor; private Floor secondFloor; // ElevatorShaft in simulation private ElevatorShaft elevatorShaft; // objects listening for events from ElevatorModel private Set personMoveListeners; private DoorListener doorListener; private ButtonListener buttonListener; private LightListener lightListener; private BellListener bellListener; private ElevatorMoveListener elevatorMoveListener; ion represents the MVC model in our elevator simulation Line 12 Lines 16-20 Use class diagram to determine associations with Floor and ElevatorShaft Lines 23-28 Declare listeners that receive events from model (and will send these events to ElevatorView)  2003 Prentice Hall, Inc All rights reserved 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 Outline // cumulative number of people in simulation private int numberOfPeople = 0; Use class diagram to determine attributes ElevatorSimulat ion.java Class Instantiate Floors and ElevatorSimulat ElevatorShaft, thenthe ion represents assign the ElevatorShaft MVC model in our referenceelevator to each Floor simulation // constructor instantiates ElevatorShaft and Floors public ElevatorSimulation() { // instantiate firstFloor and secondFloor objects firstFloor = new Floor( FIRST_FLOOR_NAME ); secondFloor = new Floor( SECOND_FLOOR_NAME ); // instantiate ElevatorShaft object elevatorShaft = new ElevatorShaft( firstFloor, secondFloor ); Line 31 // give elevatorShaft reference to first and second Floor firstFloor.setElevatorShaft( elevatorShaft ); secondFloor.setElevatorShaft( elevatorShaft ); // register for events from ElevatorShaft elevatorShaft.setDoorListener( this ); elevatorShaft.setButtonListener( this ); elevatorShaft.addElevatorMoveListener( this ); elevatorShaft.setLightListener( this ); elevatorShaft.setBellListener( this ); Lines 37-46 Lines 49-53 Register ElevatorSimulation for events from ElevatorShaft // instantiate Set for ElevatorMoveListener objects personMoveListeners = new HashSet( ); } // end ElevatorModel constructor  2003 Prentice Hall, Inc All rights reserved 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 // return Floor with given name private Floor getFloor( String name ) { if ( name.equals( FIRST_FLOOR_NAME ) ) return firstFloor; else if ( name.equals( SECOND_FLOOR_NAME ) ) return secondFloor; else return null; } // end method getFloor Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation Lines 75-91 // add Person to Elevator Simulator Method for adding Person public void addPerson( String floorName ) to simulation model Lines 78-86 { // instantiate new Person and place on Floor Person person = new Person( numberOfPeople, getFloor( floorName ) ); Instantiate Person, register person.setName( Integer.toString( numberOfPeople ) ); // register listener for Person events person.setPersonMoveListener( this ); ElevatorModel to receive events from that Person and start Person’s thread // start Person thread person.start();  2003 Prentice Hall, Inc All rights reserved 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 // increment number of Person objects in simulation numberOfPeople++; } // end method addPerson // invoked when Elevator has departed from Floor public void elevatorDeparted( ElevatorMoveEvent moveEvent ) { elevatorMoveListener.elevatorDeparted( moveEvent ); } // invoked when Elevator has arrived at destination Floor public void elevatorArrived( ElevatorMoveEvent moveEvent ) { elevatorMoveListener.elevatorArrived( moveEvent ); } Outline ElevatorSimulat When Elevator has ion.java departed fromClass Floor, notify listener (i.e.,ElevatorSimulat ElevatorView, in this simulation) ion represents the MVC model in our elevator simulation When Elevator has arrived at Floor, notify Lines 94-97 listener (ElevatorView) Lines 100-103 When Person performs some action (event) on event type in model, determine whichLines event107-153 was sent, then forward the event to any listeners // send PersonMoveEvent to listener, depending private void sendPersonMoveEvent( int eventType, PersonMoveEvent event ) { Iterator iterator = personMoveListeners.iterator(); while ( iterator.hasNext() ) { PersonMoveListener listener = ( PersonMoveListener ) iterator.next();  2003 Prentice Hall, Inc All rights reserved 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 // send Event to this listener, depending on eventType switch ( eventType ) { // Person has been created case Person.PERSON_CREATED: listener.personCreated( event ); break; // Person arrived at Elevator case Person.PERSON_ARRIVED: listener.personArrived( event ); break; Outline ElevatorSimulat ion.java Class ElevatorSimulat When Person performs some ion represents the action (event) in model, MVC model in our determine which event was elevator simulation sent, then forward the event to any listeners Lines 108-153 // Person entered Elevator case Person.PERSON_ENTERING_ELEVATOR: listener.personEntered( event ); break; // Person pressed Button object case Person.PERSON_PRESSING_BUTTON: listener.personPressedButton( event ); break; // Person exited Elevator case Person.PERSON_EXITING_ELEVATOR: listener.personDeparted( event ); break;  2003 Prentice Hall, Inc All rights reserved 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 // Person exited simulation case Person.PERSON_EXITED: listener.personExited( event ); break; default: break; } } } // end method sendPersonMoveEvent // invoked when Person has been created in model public void personCreated( PersonMoveEvent moveEvent ) { sendPersonMoveEvent( Person.PERSON_CREATED, moveEvent ); } // invoked when Person has arrived at Floor's Button public void personArrived( PersonMoveEvent moveEvent ) { sendPersonMoveEvent( Person.PERSON_ARRIVED, moveEvent ); } // invoked when Person has pressed Button public void personPressedButton( PersonMoveEvent moveEvent ) { sendPersonMoveEvent( Person.PERSON_PRESSING_BUTTON, moveEvent ); } Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation When Person has been Lines 156-159 created, notify listeners Lines 162-165 Lines 168-171 When Person has arrived at Elevator, notify listeners When Person has pressed Button, notify listeners  2003 Prentice Hall, Inc All rights reserved 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 // invoked when Person has entered Elevator public void personEntered( PersonMoveEvent moveEvent ) { sendPersonMoveEvent( Person.PERSON_ENTERING_ELEVATOR, moveEvent ); } // invoked when Person has departed from Elevator public void personDeparted( PersonMoveEvent moveEvent ) { sendPersonMoveEvent( Person.PERSON_EXITING_ELEVATOR, moveEvent ); } // invoked when Person has exited Simulation public void personExited( PersonMoveEvent moveEvent ) { sendPersonMoveEvent( Person.PERSON_EXITED, moveEvent ); } // invoked when Door has opened public void doorOpened( DoorEvent doorEvent ) { doorListener.doorOpened( doorEvent ); } Outline When Person has entered Elevator, notify listeners ElevatorSimulat ion.java Class ElevatorSimulat ion represents When Person has left the in our Elevator, MVC notifymodel listeners elevator simulation Lines 175-179 Lines 182-186 When Person has exited simulation, notify listeners Lines 189-192 Lines 195-198 When Door has opened, notify listeners  2003 Prentice Hall, Inc All rights reserved 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 // request Elevator public void requestElevator( Location location ) { // if Elevator is idle if ( !isMoving() ) { Public service to request Outline Elevator (used by Buttons) Elevator.java Elevator If Elevator is idle andClass servicing represents the Floor of the request, send the // if Elevator is on same Floor of request Elevator traveling elevatorArrived event if ( location == currentFloorLocation ) between two Floors, // Elevator has already arrived; send arrival event operating sendArrivalEvent( currentFloorLocation ); asynchronously with If Elevator is idle and other objects // if Elevator is on opposite Floor of request servicing opposite Floor of the else { request, move to other Floor Lines 306-332 setMoving( true ); // move to other Floor } } else // if Elevator is moving Line 315 // if Elevator departed from same Floor as request if ( location == currentFloorLocation ) summoned = true; If Person // if Elevator is traveling to Floor of // simply continue traveling Line 319 Line 325-326 requests Elevator just after Elevator has left the Floor on which request, the Person is waiting, Elevator must “remember” to return to that Floor } // end method requestElevator  2003 Prentice Hall, Inc All rights reserved 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 // invoked when bell has rung public void bellRang( BellEvent bellEvent ) { // send event to bellLirdstener if ( bellListener != null ) bellListener.bellRang( bellEvent ); } // get the currentFloorLocation of the Elevator public Location getCurrentFloor() { return currentFloorLocation; } } Outline Notify BellListener that Bell has rung Elevator.java Class Elevator represents the Elevator traveling between two Floors, operating asynchronously with other objects Lines 334-339  2003 Prentice Hall, Inc All rights reserved 65 E.9 Class Person • Person – – – – Walks across Floor to Elevator Rides Elevator “Has a” Location Operates asynchronously with other objects • Extends class Thread  2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Person.java // Person riding the elevator package com.deitel.jhtp5.elevator.model; // Java core packages import java.util.*; // Deitel packages import com.deitel.jhtp5.elevator.event.*; public class Person extends Thread { // identification number private int ID = -1; // represents whether Person is moving or waiting private boolean moving; // reference to Location (either on Floor or in Elevator) private Location location; // listener object for PersonMoveEvents private PersonMoveListener personMoveListener; // time in milliseconds to walk to Button on Floor private static final int TIME_TO_WALK = 3000; Outline Person.java Class Person represents the Person that rides the Elevator The Person operates asynchronously with other objects Use class diagram to determine associations and Lines 14-20 attributes of Person Line 23 Lines 26-34 Declare listener that receives PersonMoveEvent Define constants that indicate each type of event that a Person may send  2003 Prentice Hall, Inc All rights reserved 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 // types of messages Person may send public static final int PERSON_CREATED = 1; public static final int PERSON_ARRIVED = 2; public static final int PERSON_ENTERING_ELEVATOR = 3; public static final int PERSON_PRESSING_BUTTON = 4; public static final int PERSON_EXITING_ELEVATOR = 5; public static final int PERSON_EXITED = 6; Outline // Person constructor set initial location public Person( int identifier, Location initialLocation ) { super(); ID = identifier; // assign unique identifier location = initialLocation; // set Floor Location moving = true; // start moving toward Button on Floor Person.java Person constructor Person assigns uniqueClass identifier represents and sets initial Floor on the which PersonPerson is locatedthat rides the Elevator The Person operates asynchronously with other objects Lines 37-44 } // set listener for PersonMoveEvents public void setPersonMoveListener( PersonMoveListener listener ) { personMoveListener = listener; } // set Person Location private void setLocation( Location newLocation ) { location = newLocation; } Lines 47-51 Enable PersonMoveListener to receive PersonMoveEvents Lines 54-57 When Door opens, set Person’s Location to that of where the Door opened  2003 Prentice Hall, Inc All rights reserved 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 // get current Location private Location getLocation() { return location; } // get identifier public int getID() { return ID; } // set if Person should move public void setMoving( boolean personMoving ) { moving = personMoving; } // get if Person should move public boolean isMoving() { return moving; } // Person either rides or waits for Elevator public void run() { // indicate that Person thread was created sendPersonMoveEvent( PERSON_CREATED ); Outline Person.java Class Person represents the Person that rides the Elevator The Person operates asynchronously with other objects Lines 84-204 Line 87 Invoked when Person’s Thread is started Notify listeners when Person is created  2003 Prentice Hall, Inc All rights reserved 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 // walk to Elevator pauseThread( TIME_TO_WALK ); // stop walking at Elevator setMoving( false ); // Person arrived at Elevator sendPersonMoveEvent( PERSON_ARRIVED ); // get Door on current Floor Door currentFloorDoor = location.getDoor(); Outline Put Person Thread to sleep for three seconds, simulating a three second walk to the Elevator Person.java Class Person represents the Person that rides the Elevator Notify listeners when PersonThe Person operates arrived at Elevator asynchronously with other objects // get Elevator Elevator elevator = ( (Floor) getLocation() ).getElevatorShaft().getElevator(); // begin exclusive access to currentFloorDoor synchronized ( currentFloorDoor ) { // check whether Floor Door is open if ( !currentFloorDoor.isDoorOpen() ) { Line 90 Line 96 If Door is closed, press Button on Floor and wait Lines 109-117 for Elevator to arrive sendPersonMoveEvent( PERSON_PRESSING_BUTTON ); pauseThread( 1000 ); // press Floor's Button to request Elevator Button floorButton = getLocation().getButton(); floorButton.pressButton( getLocation() ); }  2003 Prentice Hall, Inc All rights reserved 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 Outline // wait for Floor door to open try { Person.java Class Person Wait for the represents the } currentFloorDoor to Person that rides the // handle exception waiting for Floor door to open open Elevator The catch ( InterruptedException interruptedException ) { Person operates interruptedException.printStackTrace(); asynchronously with } other objects while ( !currentFloorDoor.isDoorOpen() ) currentFloorDoor.wait(); // Floor Door takes one second to open pauseThread( 1000 ); Lines 122-123 // implicitly wait for exclusive access to elevator synchronized ( elevator ) { Lines 135-158 Only one Person is allowed to Line 138 // Person enters Elevator occupy the Elevator at one time sendPersonMoveEvent( PERSON_ENTERING_ELEVATOR ); Notify listeners when Person LineElevator 141 entered // set Person Location to Elevator setLocation( elevator ); // Person takes one second to pauseThread( 1000 ); Set the Person’s enter Elevator location to the Elevator  2003 Prentice Hall, Inc All rights reserved 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 Outline Notify listeners when Person pressed Button Person.java // get Elevator's Button Class Person Button elevatorButton = getLocation().getButton(); represents the Person that rides the // press Elevator's Button Elevator The elevatorButton.pressButton( location ); Person operates Press the Elevator’s button to // Door closing takes one second asynchronously with instruct the Elevator to begin pauseThread( 1000 ); other objects traveling // pressing Elevator Button takes one second sendPersonMoveEvent( PERSON_PRESSING_BUTTON ); pauseThread( 1000 ); } } // give up exclusive access to Floor door Line 147 // get exclusive access to Elevator synchronized( elevator ) { Line 154 // get Elevator door Door elevatorDoor = getLocation().getDoor(); Lines 173-174 // wait for Elevator door to open synchronized( elevatorDoor ) { try { Invoke method wait on the elevatorDoor while ( !elevatorDoor.isDoorOpen() ) elevatorDoor.wait(); }  2003 Prentice Hall, Inc All rights reserved 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 // handle exception waiting for Elevator door to open catch ( InterruptedException interruptedException ) { interruptedException.printStackTrace(); } Outline Person.java Class Person // waiting for Elevator's Door to open takes a second represents the pauseThread( 1000 ); Person that rides the Elevator The // move Person onto Floor Person operates setLocation( elevator.getCurrentFloor() ); asynchronously with Set Person’s new location to Floor // walk away from Elevator other objects setMoving( true ); Person walks away from the // Person exiting Elevator Elevator); sendPersonMoveEvent( PERSON_EXITING_ELEVATOR Line 186 Line 189 } // release elevatorDoor lock, allowing door to close } // release elevator lock, allowing waiting Person to enter Line 202 // walking from elevator takes five seconds pauseThread( * TIME_TO_WALK ); // Person exits simulation sendPersonMoveEvent( PERSON_EXITED ); } // end method run Person left the simulation  2003 Prentice Hall, Inc All rights reserved 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 // pause thread for desired number of milliseconds private void pauseThread( int milliseconds ) { try { sleep( milliseconds ); } Outline Utility method for putting Person’s Thread to sleep Person.java Class Person represents the Person that rides the // handle exception if interrupted when paused Elevator The catch ( InterruptedException interruptedException ) { Person operates interruptedException.printStackTrace(); asynchronously with } other objects } // end method pauseThread Utility method for determining which // send PersonMoveEvent to listener, depending on event type Lines 207-217 PersonMoveEvent private void sendPersonMoveEvent( int eventType ) to send to listener, then { Lines 220-262 // create new event sending that event PersonMoveEvent event = new PersonMoveEvent( this, getLocation(), getID() ); // send Event to this listener, depending on eventType switch ( eventType ) { // Person has been created case PERSON_CREATED: personMoveListener.personCreated( event ); break;  2003 Prentice Hall, Inc All rights reserved 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 // Person arrived at Elevator case PERSON_ARRIVED: personMoveListener.personArrived( event ); break; // Person entered Elevator case PERSON_ENTERING_ELEVATOR: personMoveListener.personEntered( event ); break; // Person pressed Button object case PERSON_PRESSING_BUTTON: personMoveListener.personPressedButton( event ); break; Outline Person.java Class Person represents the Person that rides the Elevator The Person operates asynchronously with other objects // Person exited Elevator case PERSON_EXITING_ELEVATOR: personMoveListener.personDeparted( event ); break; // Person exited simulation case PERSON_EXITED: personMoveListener.personExited( event ); break; default: break; } } // end method sendPersonMoveEvent }  2003 Prentice Hall, Inc All rights reserved 75 E.10 Artifacts Revisited • Artifacts for package model – Each class in model imports package model – Each component in package model maps to distinct file • Therefore, each component maps to distinct class – Package model aggregates package event  2003 Prentice Hall, Inc All rights reserved Fig E.15 Artifacts for package model model Bell.java Bell.java Door.java ElevatorSimulation.java Floor.java Location.java Button.java Elevator.java ElevatorShaft.java Light.java Person.java ElevatorDoor.java  2003 Prentice Hall, Inc All rights reserved event 76 77 E.11 Conclusion • Object-oriented fundamentals and Java-specific topics – Event handling – Multithreading • Appendix F – Implements the ElevatorView  2003 Prentice Hall, Inc All rights reserved ... ElevatorSimulat ion .java Class ElevatorSimulat ion represents the MVC model in our elevator simulation When Person has been Lines 15 6-1 59 created, notify listeners Lines 16 2-1 65 Lines 16 8-1 71 When Person... arrived at Floor, notify Lines 9 4-9 7 listener (ElevatorView) Lines 10 0-1 03 When Person performs some action (event) on event type in model, determine whichLines event10 7-1 53 was sent, then forward... 27 28 // ElevatorSimulation .java // Elevator simulation model with ElevatorShaft and two Floors package com.deitel.jhtp5.elevator.model; // Java core packages import java. util.*; // Deitel packages

Ngày đăng: 11/12/2017, 19:48

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w