Jump to content
  • Advertisement
Sign in to follow this  
pompoko

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

This topic is 3796 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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!