I have to implement a state machine with very basic requirement that every state pattern should have:
- The state machine can be in any ONE state at a time.
- The transition from X state to Y state will have different parameters than transition from Y to Z or from any other state to another one.
- User program, which will be operating the "state machine" cannot, of-course, transition to a state which is not allowed to go to if you are on a particular state. for e.g.
stateMachine.dispenseCard()
will not work ifstateMachine.currentState()
is notCASHACCEPTED
I tried following this link, but here:
-
The abstract State class needs to define all the possible states of the state machine, thus the concrete state needs to implement all the state methods. Why should the concrete state class be interested in all the other methods which transit to other states? Why not only the ones that this state transits to?
public abstract class DoorState : DomainObject { protected Door _door; public Door Door { get { return _door; } set { _door = value; } } public abstract void Close(); public abstract void Open(); public abstract void Break(); public abstract void Lock(); public abstract void Unlock(); /// <summary> /// Fix simulates a repair to the Door and resets /// the initial state of the door to closed. /// </summary> public void Fix() { _door.DoorState = new DoorClosedState(this); }}
-
Why does the State class "has a" Device which transits to different states? Shouldn't it be other way round? Like the Door should "has a" state.
Aucun commentaire:
Enregistrer un commentaire