ステート マシン: http://en.wikipedia.org/wiki/State_pattern。
基本的に、ステート リングがあります (すべてのステートが 1 つのインターフェイスを実装します)。次に、次のように、状態シフター クラスに「参加者」インターフェイスを実装させます。
// All stages should implement this
interface IStateRing
{
}
// Everything participating in the state ring should implement this
interface IStateRingParticipant
{
void SetStage(IStateRing stage);
}
あなたの参加者クラスの唯一の仕事は、次のように、状態が指示したときに状態の変化に応答することです。
class MyStageParticipant : IStateRingParticipant
{
// Keep track of the current state.
IStateRing currentStage;
// This is called by the state when it's time to change
public void SetStage(IStateRing stage)
{
throw new NotImplementedException();
}
}
どの状態にあるかを追跡し、現在の状態によって呼び出される関数 SetStage を持っていることに注意してください。次に状態があります。
// The state handles the actual functionality, and when it's good and ready
// it commands the participant to change states.
class StateA : IStateRing
{
// Keep track of the participant.
IStateRingParticipant participant;
// The constructor should know what object belongs to this state.
// that is, which object is participating.
public StateA(IStateRingParticipant participant)
{
this.participant = participant;
}
// We do all of our processing in this state.
// Then when it's time we simply let go of the participant and
// instantiate a new state.
public void GoodAndReady()
{
new StateB(participant);
}
}
class StateB : IStateRing
{
IStateRingParticipant participant;
public StateB(IStateRingParticipant participant)
{
this.participant = participant;
}
}
コンストラクターの一部は、参加者を受け入れることに注意してください。状態はすべての実際の処理を管理し、状態が良好で準備が整ったら、次の状態 (現在の状態も保持されます) をインスタンス化します。
状態パターンを使用して、すべての機能を状態にルーティングし、(状態の新しいインスタンスをインスタンス化することによって) 状態をいつ変更する必要があるかを判断させます。