状态模式:主要解决的是当控制一个对象状态转变的表达式过于复杂时的情况,把状态的判断逻辑转移到表示不用状态的类中,可以把复杂的逻辑判断简化。

即将复杂的逻辑分到各个类中,体现职责单一原则,方便日后维护。

 状态类:(State)

namespace StatePattern.CLASS
{
    abstract class State
    {
        public abstract void Handle(Context context);
    }
}
View Code

Context类

namespace StatePattern.CLASS
{
    class Context
    {
        private State state;
        public Context(State state)
        {
            this.state = state;
        }

        public State State
        {
            get { return state; }
            set 
            {
                state = value;
                Console.WriteLine("当前状态:" + state.GetType().Name);
            }
        }

        public void Request()
        {
            state.Handle(this);
        }
    }
}
View Code

ConcreteStateA状态A类

namespace StatePattern.CLASS
{
    class ConcreateStateA:State
    {
        public override void Handle(Context context)
        {
            context.State = new ConcreteStateB();
        }
    }
}
View Code

ConcreteStatusB状态B类

namespace StatePattern.CLASS
{
    class ConcreteStateB:State
    {
        public override void Handle(Context context)
        {
            context.State = new ConcreateStateA();
        }
    }
}
View Code

测试类(TestMain)

namespace StatePattern
{
    class TestMain
    {
        static void Main(string[] args)
        {
            Context con = new Context(new ConcreateStateA());

            con.Request();
            con.Request();

            Console.ReadLine();
        }
    }
}
View Code

 

本例以上班的工作状态为例:功能图如下:

抽象状态类(State) 封装与work的一个特定状态相关的行为

namespace StatePatternTest.CLASS
{
    abstract class State
    {
        public abstract void WriteProgram(Work w);
    }
}
View Code

上午工作状态类(MorningState):继承于抽象状态类(State)

namespace StatePatternTest.CLASS
{
    class MorningState:State
    {
        public override void WriteProgram(Work w)
        {
            if (w.Hour < 12)
            {
                Console.WriteLine("当前时间:{0}点,上午工作,精神百倍!", w.Hour);
            }
            else
            {
                w.SetState(new NoonState());
                w.WriteProgram();
            }
        }
    }
}
View Code

中午状态类(NoonState):继承于抽象状态类(State)

namespace StatePatternTest.CLASS
{
    class NoonState:State
    {
        public override void WriteProgram(Work w)
        {
            if (w.Hour < 13)
            {
                Console.WriteLine("当前时间:{0}点,中午休息啦!", w.Hour);
            }
            else
            {
                w.SetState(new AfternoonState());
                w.WriteProgram();
            }
        }
    }
}
View Code

下午状态类(AfternoonState):继承于抽象状态类(State)

namespace StatePatternTest.CLASS
{
    class AfternoonState:State
    {
        public override void WriteProgram(Work w)
        {
            if (w.Hour < 17)
            {
                Console.WriteLine("当前时间:{0}点 下午状态还不错,继续努力!",w.Hour);
            }
            else
            {
                w.SetState(new EveningState());
                w.WriteProgram();
 
            }
        }
    }
}
View Code

傍晚状态类(EveningState):继承于抽象状态类(State)

namespace StatePatternTest.CLASS
{
    class EveningState:State
    {
        public override void WriteProgram(Work w)
        {
            if (w.TaskFinish)
            {
                w.SetState(new RestState());
                w.WriteProgram();
            }
            else
            {
                if (w.Hour < 21)
                {
                    Console.WriteLine("当前时间:{0}点 加班哦,疲累之极!",w.Hour);
                }
                else
                {
                    w.SetState(new SleepingState());
                    w.WriteProgram();
                }
            }
        }
    }
}
View Code

下班休息状态类(RestState):继承于抽象状态类(State)

namespace StatePatternTest.CLASS
{
    class RestState:State
    {
        public override void WriteProgram(Work w)
        {
            Console.WriteLine("当前时间:{0}点,下班回家了!",w.Hour);
        }
    }
}
View Code

睡眠状态类(SleepingState):继承于抽象状态类(State)

namespace StatePatternTest.CLASS
{
    class SleepingState:State
    {
        public override void WriteProgram(Work w)
        {
            Console.WriteLine("当前时间:{0}点 不行了,睡着了!",w.Hour);
        }
    }
}
View Code

工作类(Work)封装了状态类(State)的一些行为和属性

namespace StatePatternTest.CLASS
{
    class Work
    {
        private State state;
        private double hour;
        private bool finish = false;
        public Work(State state)
        {
            this.state = state;
        }

        /// <summary>
        /// 工作时间
        /// </summary>
        public double Hour
        {
            get { return hour; }
            set { hour = value; }
        }

        /// <summary>
        /// 任务是否完成
        /// </summary>
        public bool TaskFinish
        {
            get { return finish; }
            set { finish = value; }
        }
        
        /// <summary>
        /// 设置状态
        /// </summary>
        /// <param name="state"></param>
        public void SetState(State state)
        {
            this.state = state;
        }

        /// <summary>
        /// 当前状态改变
        /// </summary>
        public void WriteProgram()
        {
            state.WriteProgram(this);
        }
    }
}
View Code

测试类:(TestMain)

namespace StatePatternTest
{
    class TestMain
    {
        static void Main(string[] args)
        {
            Work work = new Work(new MorningState());
            work.Hour = 9;
            work.WriteProgram();
            work.Hour = 10;
            work.WriteProgram();
            work.Hour = 12;
            work.WriteProgram();
            work.Hour = 13;
            work.WriteProgram();
            work.Hour = 14;
            work.WriteProgram();
            work.Hour = 17;

            //work.TaskFinish = true;
            work.WriteProgram();
            work.Hour = 19;
            work.WriteProgram();
            work.Hour = 22;
            work.WriteProgram();

            Console.ReadLine();
        }
    }
}
View Code

结果图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2013-08-12 19:48  zxd543  阅读(148)  评论(0编辑  收藏  举报