Basic implementation:
// :: Context :: //
class Context{
private var state:State;
public function request():Void{
state.handle();
}
public function setState(aState:State):Void{
state = aState;
}
public function getState():State{
return state;
}
}
// :: State :: //
interface State{
public function handle():Void;
}
// :: ConcreteStateA :: //
class ConcreteStateA implements State{
public function handle():Void{
trace(”ConcreateStateA: handle“);
}
}
// :: ConcreteStateB :: //
class ConcreteStateB implements State{
public function handle():Void{
trace(”ConcreateStateB: handle“);
}
}
// usage
aContext = new Context();
aContext.setState(new ConcreteStateA());
aContext.request();
aContext.setState(new ConcreteStateB());
aContext.request();
This is only one way to change the state. I have chosen this way for simplicity. You could also change the state dynamically depending on for example a local connection or a mouse press etc. It depends on the context.
