Appendix B. Solutions
package com.oozinoz.carousel_sol;
public interface DoorConstants
{
DoorState CLOSED = new DoorClosed();
DoorState OPENING = new DoorOpening();
DoorState OPEN = new DoorOpen();
DoorState CLOSING = new DoorClosing();
DoorState STAYOPEN = new DoorStayOpen();
}
The Door class now retains a single state object:
package com.oozinoz.carousel_sol;
import java.util.*;
public class Door extends Observable
implements DoorConstants
{
private DoorState state = CLOSED;
//....
}
The Door object passes itself as an argument when it forwards state transition calls to its
state variable. This lets the receiving state update the Door object's state with
setState(). For example, Door.click() is:
public void click()
{
state.click(this);
}
And DoorClosing.click(door:Door) is:
public void click(Door door)
{
door.setState(OPENING);
}