State 模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//work : 变量 time, bfinished, state,
//work : 方法 WriteProgram, SetState,
//state: 方法 WriteProgram(work w)
namespace state
{
    public static class Program
    {
        public static void Main()
        {
            work w = new work();
            w.Hour = 10;
            w.WriteProgram();

            w.Hour = 12;
            w.WriteProgram();

            w.Hour = 13;
            w.WriteProgram();

            w.Hour = 15;
            w.WriteProgram();

            w.Hour = 19;
            w.WriteProgram();

//            w.bTaskFinish = true;
            w.Hour = 21;
            w.WriteProgram();

            w.Hour = 22;
            w.WriteProgram();

            Console.Read();
        }
    }
    public abstract class state
    {
        public abstract void WriteProgram(work w);
    }
    public class ForenoonState:state
    {
        public override void WriteProgram(work w)
        {
            if (w.Hour < 12)
            {
                Console.WriteLine("current time: {0} 点  上午工作时间,精神百倍....", w.Hour);
            }
            else
            {
                w.SetState(new noonState());   //转到下午WriteProgram状态
                w.WriteProgram();
            }
           
        }
    }
    public class noonState:state
    {
        public override void WriteProgram(work w)
        {
            if (w.Hour < 13)
            {
                Console.WriteLine("current time: {0} 点  中午吃饭,午休时间,困....", w.Hour);
            }
            else
            {
                w.SetState(new AfternoonState());   //转到下午WriteProgram状态
                w.WriteProgram();
            }
        }
    }
    public class AfternoonState:state
    {
        public override void  WriteProgram(work w)
        {
            if (w.Hour < 17)
            {
                Console.WriteLine("current time: {0} 点  下午上班时间,精神百倍....", w.Hour);
            }
            else
            {
                w.SetState(new EveningState());     //转到下午WriteProgram状态
                w.WriteProgram();
            }
        }
    }
    public class EveningState:state
    {
        public override void  WriteProgram(work w)
        {
            if (w.bTaskFinish)
            {
                w.SetState(new RestState());    //转到下午WriteProgram状态
                w.WriteProgram();
            }
            else
            {
                if (w.Hour < 21)
                {
                    Console.WriteLine("current time: {0} 点  晚上加班时间,累啊....", w.Hour);
                }
                else
                {
                    w.SetState(new SleepingState());    //转到下午WriteProgram状态
                    w.WriteProgram();
                }
            }
        }
    }
    public class SleepingState:state
    {
        public override void  WriteProgram(work w)
        {
            Console.WriteLine("current time: {0} 点  加班睡着了....", w.Hour);
        }
    }
    public class RestState:state
    {
        public override void  WriteProgram(work w)
        {
            Console.WriteLine("current time: {0} 点  下班回家了 ....", w.Hour);
        }
    }
    public class work       //时间、状态、是否完成、工作
    {
        public work()
        {
            current = new ForenoonState();
        }
        private state current;
        private int time;
        private bool bFinished = false;
        public int Hour
        {
            get { return time; }
            set { time = value; }
        }
        public bool bTaskFinish
        {
            get { return bFinished; }
            set { bFinished = value; }
        }
        public void SetState(state s)
        {
            current = s;
        }
        public void WriteProgram()
        {
            current.WriteProgram(this);
        }
    }
}

posted on 2009-07-25 13:57  Acor  阅读(154)  评论(0编辑  收藏  举报

导航