Sign in to follow this  

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

This topic is 3490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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>
    {
        

    }
}



Share this post


Link to post
Share on other sites

This topic is 3490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this