Java Reference
In-Depth Information
Figure 22.3. This diagram shows a door's states as classes in an arrangement that mirrors
the door's state machine.
The refactoring that Figure 22.3 shows uses the Door_2 class to contain the context of the
state machine. A context is an object that contains information that is environmental and
relevant to a group of other objects. In particular, S TATE uses a context object to record which
instance of DoorState is the current state.
The DoorState class constructor requires a Door_2 object. Subclasses of DoorState use
this object to communicate changes in state back to the door. By giving these classes an
attribute that ties them to a specific Door object, this design requires that a DoorState
object be referenced by a single Door object. In turn, this requires the Door class to define its
states as local variables:
package com.oozinoz.carousel;
public class Door_2 extends Observable
{
public final DoorState CLOSED = new DoorClosed(this);
public final DoorState OPENING = new DoorOpening(this);
Search WWH ::




Custom Search