[.net] Is this a sound State Pattern (with generics)?

Started by
-1 comments, last by pompoko 15 years, 10 months ago
I wonder whether I should specify this 'generic' Context object which all subsequent classes inherit, or whether I should leave that part to the inheriting classes (ie specifying Turn inside the DealCards etc. classes)

namespace Montauk.Logic
{
    abstract class State<T>
    {
        private T _context;

        public State(T context)
        {
            _context = context;
        }

        public abstract void EnterState()
        {
        }

        public abstract void Execute()
        {
        }

        public abstract void Transition(State<T> nextState)
        {
        }

        public T Context
        {
            get { return _context; }
            set { _context = value; }
        }
    }
}
Implemented, it would look something like this

namespace Montauk.Logic
{
    class Turn
    {
        private State<Turn> _state = new DealCards(this);
        

        public void Turn()
        {
            
        }

        public State<Turn> State
        {
            get { return _state; }
            set { _state = value; }
        }
    }
}

namespace Montauk.Logic
{
    class DealCards : State<Turn>
    {

        public override void Execute()
        {
            if (1)
            {
                Transition(new HeadlinePhase(Context));
            }
        }
        public override void Transition(State<Turn> nextState)
        {
            Context.State = nextState;
        }

    }
}

namespace Montauk.Logic
{
    class HeadlinePhase : State<Turn>
    {
        

    }
}



This topic is closed to new replies.

Advertisement