开发者

WPF and State Machine Pattern

I want to implement functionality that give possibility switch tabItems (TabItem1,TabItem2,TabItem3), when user click button "next" on the Form. And for that reason I'd like to use State Machine Pattern.

These things I've already implemented, but I have no clue, is it right or not?:

public abstract class State
    {
        #region Constructros
           public State()
        {
        }


        public State(State state)
        {
            this.CurrentState = state.CurrentState;
            this.PreviousState = state.PreviousState;
            this.NextState = state.NextState;
        }


        public State(Machine machine,string strCurrentState,string strPreviousState,string strNextState)
        {
            this.CurrentState = strCurrentState;
            this.PreviousState = strPreviousState;
            this.NextState = strNextState;
            this.SetParams();
        }
        #endregion

        public const string WELCOME             = "WELCOME";
        public const string EMR_CONFIGURATION   = "EMR_CONFIGURATION";
        public const string MIGRATION           = "MIGRATION";
        public const string END_OF_STATES       = "END_OF_STATES";


        private Machine machine;
        private string currentState;
        private string previousState;
        private string nextState;
        private string nameState;

        public virtual void SetParams() { }
        public virtual void ChangeState(Machine m, State s)
        {
            m.ChangeState(s);
        }


        //Get The name of State
        public Machine Machine
        {
            get { return this.machine; }
            set { this.machine = value; }
        }

        //Current State
        public string CurrentState
        {
            get { return this.currentState; }
            set { this.currentState = value; }
        }
        //Previous State
        public string PreviousState
        {
            get { return this.previousState; }
            set { this.previousState = value; }
        }
        //Next State
        public string NextState
        {
            get { return this.nextState; }
            set { this.nextState = value; }
        }

        public string NameState
        {
            get { return this.nameState; }
            set { this.nameState = value; }
        }

    }

 public class Machine
    {
        public State currentState;

        public Machine (string strCurrentState,string strPreviousState,string strNextState)
        {
            currentState = new WelcomeState(this, strCurrentState, strPreviousState, strNextState);
        }

        public void ChangeState(State setState)
        {
            currentState = setState; 
        }

        public void  SetCurrentState (string state)
        {
            currentState.CurrentState = state;
        }
    }

 class Transition
    {
        public State state;
        private static Transition getInstance;
        protected Transition(){}

        public static Transition GetInstance()
        {
            if (getInstance == null)
            {
                getInstance = new Transition();
            }
            return getInstance;
        }

        public void Transform(State state)
        {
            if (state == null)
            {
                return;
            }

            // Get the type of state.
            string stateType = state.GetType().Name;


            // WELCOME TabItem 
            if (state.CurrentState == State.WELCOME)
            {
                state.ChangeState(state.Machine, new WelcomeState(state)); 
            }

            //EMR_CONFIGURATION TabItem
            if (state.CurrentState == State.EMR_CONFIGURATION)
            {
                state.ChangeState(state.Machine, new EMRConfigurationState(state)); 
            }

            //MIGRATION TabItem 
            if (state.CurrentState == State.EMR_CONFIGURATION)
            {
                state.ChangeState(state.Machine, new MigrationState(state));
            }

        }
    }

  public class WelcomeState : State
    {
        public WelcomeState(State state) : base(state)
  {
        }

        public WelcomeState (Machine machine, string strCurrentState,string strPreviousState,string strNextState):
            base(machine, strCurrentState, strPreviousState, strNextState)
            {
            }


        public override void SetParams()
  {
            this.CurrentState  = State.WELCOME;
            this.PreviousState = State.WELCOME;
            this.NextState     = State.EMR_CONFIGURATION;
  }
    }

 public class EMRConfigurationState : State
    {
        public EMRConfigurationState(State state) : base(state)
  {
        }

        public EMRConfigurationState(Machine machine, string strCurrentState, string strPreviousState, string strNextState) :
            base(machine, strCurrentState, strPreviousState, strNextState)
            {
            }

        public override void SetParams()
  {
            this.CurrentState  = State.EMR_CONFIGURATION;
            this.PreviousState = State.WELCOME;
            this.NextState     = State.MIGRATION;  
  }
    }

 public class MigrationState: State
    {
        public MigrationState(State state) : base(state)
  {
        }

        public Migr开发者_JAVA百科ationState(Machine machine, string strCurrentState, string strPreviousState, string strNextState) :
            base(machine, strCurrentState, strPreviousState, strNextState)
            {
            }

        public override void SetParams()
  {
            this.CurrentState  = State.MIGRATION;
            this.PreviousState = State.EMR_CONFIGURATION;
            this.NextState     = State.END_OF_STATES;
  }
    }


 public partial class Window1 : Window
    {
        private WizardPresenter wizPresenterWelcome = null;
        public List<WizardPresenter> stateList;
        public Window1()
        {
            InitializeComponent();

            stateList = new List<WizardPresenter>
                                        {
                                            new WizardPresenter(StatePresenter.WELCOME,
                                                StatePresenter.WELCOME,
                                                StatePresenter.EMR_CONFIGURATION),

                                            new WizardPresenter(StatePresenter.EMR_CONFIGURATION,
                                                StatePresenter.WELCOME,StatePresenter.MIGRATION),

                                            new WizardPresenter(StatePresenter.MIGRATION,
                                                StatePresenter.EMR_CONFIGURATION,
                                                StatePresenter.WELCOME),
                                        };

            tabControl.ItemsSource = stateList;
        }

        private WizardPresenter GetWizardPresenter(string strState)
        {
            foreach (WizardPresenter presenter in stateList)
            {
                if (presenter.currentStatePresenter.CurrentStatePresenter == strState)
                {
                    return presenter;
                    break;
                }
            }
            return null;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (this.wizPresenterWelcome == null)
            {
                try
                {
                    {
                        WizardPresenter wp = (WizardPresenter)tabControl.SelectedItem;
                        string nextState = wp.currentStatePresenter.NextStatePresenter;

                        tabControl.SelectedIndex = stateList.IndexOf(GetWizardPresenter(nextState));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error:" + ex.Message + "Error");
                }
            }
        }
    }


If when you run it works then you've got it right, if when you run it doesn't work then you've got it wrong.


In general:

  1. When you're rephrasing a question, edit the original post and add explanations there.
  2. You can create a context class that holds all the relevant states and the current, previous and next state. Each state can handle each change and decide the next state based on:

    • Runtime value of Context variables.
    • The context's previous state.
      etc.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜