// @topic S-0314-13-04-50 Java Iterator and Visitor patterns // @brief class Visitor implements IUpdatable package week11; public class Visitor implements IUpdatable { private IVisitable location = null; //-------------------------------------------- // IUpdatable implemention //-------------------------------------------- // A floor object notifying the visitor // that "you are here": @Override public void visitorArrived( Floor floor ) { boolean result = floor.accept(this); if ( result ) { location = floor; System.out.println("visitor (me) "+this+" location is "+floor); } } // Scenario notifying the floor object // that a new visitor has arrived: @Override public void visitorArrived( Visitor visitor ){/*ignore*/} // Generic update from the background thread: @Override public void update() { System.out.println( "visitor (me) " + this + " being updated:" ); if ( location != null ) { if ( location.forget( this ) ) { // only if the location permits the visitor // to leave: System.out.println("visitor (me) "+this+" left location "+location); location = null; } } } // Elevator notifying the floor and visitor objects // about the elevator arrival: @Override public void elevatorArrived( /*Elevator elevator*/ ) {/*TODO*/} // Special update to all objects, // possibly a random event generated // by the scenario or the user pressing // the "red button" during the simulation: @Override public void emergency() {/*TODO*/} }