状态模式 怎么有点像解释器

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Sensis.UnitTest.ProductEntityCore
{
    public class Program
    {
        static void Main(string[] args)
        {
            WorkFlow workFlow = new WorkFlow();
            workFlow.State = State.Four;
            workFlow.ihandle = new One();
            workFlow.Invoke();
            Console.Read();
        }
    }

    public enum State
    {
        One, Two, Three, Four, Five
    }

    public interface IHandle
    {
        void Handle(State state);
    }

    public class WorkFlow
    {
        public WorkFlow()
        {
        }

        public WorkFlow(IHandle newHandle,State state)
        {
            this.ihandle = newHandle;
            this.State = state;
        }

        private State m_state;

        public State State
        {
            get { return m_state; }
            set { m_state = value; }
        }

        public IHandle ihandle;


        public void Invoke()
        {
            ihandle.Handle(this.State);
        }

    }


    public class One : IHandle
    {
        public void Handle(State state)
        {
            if (state == State.One)
            {
                Console.WriteLine("第一步");
            }
            else
            {
                new WorkFlow(new Two(),state).Invoke();
            }
        }

    }

    public class Two : IHandle
    {
        public void Handle(State state)
        {
            if (state == State.Two)
            {
                Console.WriteLine("第二步");
            }
            else
            {
                new WorkFlow(new Three(), state).Invoke();
            }
        }

    }


    public class Three : IHandle
    {
        public void Handle(State state)
        {
            if (state == State.Three)
            {
                Console.WriteLine("第三步");
            }
            else
            {
                new WorkFlow(new Four(), state).Invoke();
            }
        }

    }

    public class Four : IHandle
    {
        public void Handle(State state)
        {
            if (state == State.Four)
            {
                Console.WriteLine("第四步");
            }
            else
            {
                Console.WriteLine("Null");
            }
        }

    }

}

 


 

posted @ 2009-04-28 17:57  游侠_1  阅读(161)  评论(0编辑  收藏  举报