示例#1
0
    // events could be used for this callback as well, but using private inner
    //  classes calling a private member is probably the simplest.
    private void TransitionToState <T>() where T : DocumentState, new()
    {
        // save prior for a moment
        DocumentState priorState = State;
        // this can be lookup from map instead of new() if you need to keep them
        //  alive for some reason.  I personally like flyweight states.
        DocumentState nextState = new T();

        // activate the new state.  it will get notified so it can do any one-
        //  time setup
        State = nextState;
        State.EnterState(this);

        // let the prior state know as well, so it can cleanup if needed
        if (priorState != null)
        {
            priorState.ExitState();
        }
    }