0

Flashcards と Sets の 2 つのクラスがあります。フラッシュカードは 7 つのステージのいずれかにあります。各ステージには TimeSpan プロパティが必要です。目標は、カードが存在するステージに応じて、特定の期間の後にカードを表示することです。

セットもこれらの 7 つの段階の 1 つであり、すべてのカードの中で最も低い段階です。

現在、両方のクラスに「public int Stage」プロパティがありますが、これは理想的ではないように感じます。

クラス/オブジェクト定義に関して、これをモデル化する適切な方法は何でしょうか? 問題が発生した場合に備えて、MVC4 EF CodeFirst を使用しています。

4

1 に答える 1

0

ステート マシン: 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;
    }
}

コンストラクターの一部は、参加者を受け入れることに注意してください。状態はすべての実際の処理を管理し、状態が良好で準備が整ったら、次の状態 (現在の状態も保持されます) をインスタンス化します。

状態パターンを使用して、すべての機能を状態にルーティングし、(状態の新しいインスタンスをインスタンス化することによって) 状態をいつ変更する必要があるかを判断させます。

于 2013-02-07T18:42:09.490 に答える