zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }

  • 相关阅读:
    JavaScriptCore框架介绍
    可执行程序加载到内存的过程
    阿里云服务器怎么更换系统盘
    什么是A记录  域名
    宝塔服务器管理助手Linux面版-使用教程
    虚拟主机、VPS以及云主机的区别和对比
    怎样选择一个好的虚拟主机
    解Linux SSH命令大全,新手必看SSH命令
    虚拟主机,VPS,云主机之间的区别?
    云服务器和虚拟主机的区别:
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1530868.html
Copyright © 2011-2022 走看看