526 Object-Oriented Programming Chapter 9 tor, first floor (Web site counters)

526 Object-Oriented Programming Chapter 9 tor, first floor or second floor of that person. Because a superclass reference can hold a subclass reference, the person s Location attribute references the appropriate Floor object when that person is on a floor and references the Elevator object when that person is inside the elevator. The elevator and floors contain buttons. (The elevator s button signals the elevator to move to the other floor, and the floors buttons summon the elevator to the floor of the request.) Because all locations in our simulation contain buttons, class Location provides abstract method getButton. Class Elevator implements method getButton to return a reference to the Button object inside the elevator, and class Floor implements method getButton to return a reference to the Button object on the floor. Using its Locationreference, the person is able to press the correct button i.e., the person will not press a floor s button when inside the elevator and will not press the elevator s button when on a floor. The State design pattern uses an abstract superclass called the State class which contains methods that describe behaviors for states that an object (called the context object) can occupy. In our elevator simulation, the State class is superclass Location, and the context object is the object of class Person. Note that class Location does not describe all states of class Person (e.g., whether that person is walking or waiting for the elevator) class Location describes only the location the Person and contains method getButtonso the Person can access the Button object at various locations. A State subclass, which extends the State class, represents an individual state that the context can occupy. The State subclasses in our simulation are classes Elevator and Floor. Each State subclass contains methods that implement the State class abstract methods. For example, both classes Elevator and Floor implement method get- Button. The context contains exactly one reference to an object of the State class this reference is called the state object. In the simulation, the state object is the object of class Location. When the context changes state, the state object references the State subclass object associated with that new state. For example, when the person walks from the floor into the elevator, the Person object s Location is changed from referencing one of the Floor objects to referencing the Elevator object. When the person walks onto the floor from the elevator, the Person object s Location references the appropriate Floor object. 9.24.4 Conclusion In Discovering Design Patterns Section 9.24, we listed the three types of design patterns introduced in the gang-of-four book, we identified 18 of these design patterns that we discuss in this book and we discussed specific design patterns, including Singleton, Proxy, Memento and State. In Discovering Design Patterns Section 13.18, we introduce some design patterns associated with AWT and Swing GUI components. After reading this section, you should understand better how Java GUI components take advantage of design patterns. 9.24.5 Internet and World-Wide-Web Resources The following URLs provide further information on the nature, importance and applications of design patterns. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply