K
kk_oop
Hi. I have a question about implementing the state pattern in Java.
I'm implementing the State abstract base class and each specific class
as inner classes of the context class. So I have something like this:
Context: Elevator
State (inner class of Elevator): ElevatorState
Specific States (derived from ElevatorState, inner class of Elevator):
DoorOpenState, DoorClosedState
So Elevator has a currentState attribute of type ElevatorState. This
will be set at any given time to either DoorOpenState or
DoorClosedState.
My question pertains to implementing a way to query the current state.
I see many folks define a series of static final integers to do this,
e.g., public static final int DOOR_OPEN_STATE = 1, etc. Then each
state would have a query that would return the integer value of its
state. Elevator would have a getCurrentState method that delegated to
the state's method.
I think this is unnecessary. I would rather give Elevator a query for
each state, e.g.: isDoorOpen( ) and isDoorClosed( ). I'd implement
isDoorOpen by just saying "return currentState instanceof
DoorOpenState".
I figure I might as well do this, since whoever would want to know the
current state would most likely really be wanting to know if the
Elevator was in a particular state. If I used the int approach, the
client would have to get the state then compare it to one of the
constants. So I figure my alternate approach doesn't add any overhead
beyond the integer approach--it just replaces the integer constants
with the query methods. There is still an if check, but just done with
the query method instead of the integer check.
If you need to add a new state one way, you have to add another
integer. If you add a new state the other way, you add a new query
method.
Taking the "instanceof" also ensures there will be no risk of having
two states accidentally being assigned the same state id.
Any thoughts on this approach?
Thanks!
Ken
I'm implementing the State abstract base class and each specific class
as inner classes of the context class. So I have something like this:
Context: Elevator
State (inner class of Elevator): ElevatorState
Specific States (derived from ElevatorState, inner class of Elevator):
DoorOpenState, DoorClosedState
So Elevator has a currentState attribute of type ElevatorState. This
will be set at any given time to either DoorOpenState or
DoorClosedState.
My question pertains to implementing a way to query the current state.
I see many folks define a series of static final integers to do this,
e.g., public static final int DOOR_OPEN_STATE = 1, etc. Then each
state would have a query that would return the integer value of its
state. Elevator would have a getCurrentState method that delegated to
the state's method.
I think this is unnecessary. I would rather give Elevator a query for
each state, e.g.: isDoorOpen( ) and isDoorClosed( ). I'd implement
isDoorOpen by just saying "return currentState instanceof
DoorOpenState".
I figure I might as well do this, since whoever would want to know the
current state would most likely really be wanting to know if the
Elevator was in a particular state. If I used the int approach, the
client would have to get the state then compare it to one of the
constants. So I figure my alternate approach doesn't add any overhead
beyond the integer approach--it just replaces the integer constants
with the query methods. There is still an if check, but just done with
the query method instead of the integer check.
If you need to add a new state one way, you have to add another
integer. If you add a new state the other way, you add a new query
method.
Taking the "instanceof" also ensures there will be no risk of having
two states accidentally being assigned the same state id.
Any thoughts on this approach?
Thanks!
Ken