状态模式实例

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

namespace zhuangtai
{
    public abstract class State
    {
        public abstract void WriteProgram(Work w);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zhuangtai
{
    public class Work
    {
        private State current;
        public Work()
        {
            current = new ForenonState();
        }
        private double hour;
        public double Hour
        {
            get
            {
                return hour;

            }
            set
            {
                hour = value;
            }
        }
        private bool finish = false;
        public bool TaskFinished
        {
            get
            { return finish; }
            set
            { finish = value; }
        }
        public void setState(State s)
        {
            current = s;
        }
        public void WriteProgram()
        {
            current.WriteProgram(this);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zhuangtai
{
    public class ForenonState:State
    {public override void WriteProgram(Work w)
        {
        if(w.Hour<12)
        {
            Console.WriteLine("当前时间:{0}点上午工作精神百倍", w.Hour);

            
        }
        else
        { w.setState(new NoonState());
        w.WriteProgram();
        }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zhuangtai
{
    public class eveningState:State
    {public override void WriteProgram(Work w)
        {
        if(w.TaskFinished)
        {
            w.setState(new RestState());
            w.WriteProgram();
        }
        else
        {
            if (w.Hour < 12)
                Console.WriteLine("当前时间{0}点加班哦", w.Hour);
            else
            { w.setState(new Sleeping());
            w.WriteProgram();
            }
        }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zhuangtai
{
    public class NoonState:State
    {
        public override void WriteProgram(Work w)
        {
           if(w.Hour<13)
           {
               Console.WriteLine("当前时间{0}点饿了困了", w.Hour);
           }
           else
           { w.setState(new AfteroonState());
           w.WriteProgram();
           }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zhuangtai
{
    public class eveningState:State
    {public override void WriteProgram(Work w)
        {
        if(w.TaskFinished)
        {
            w.setState(new RestState());
            w.WriteProgram();
        }
        else
        {
            if (w.Hour < 12)
                Console.WriteLine("当前时间{0}点加班哦", w.Hour);
            else
            { w.setState(new Sleeping());
            w.WriteProgram();
            }
        }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zhuangtai
{
    public class RestState:State
    {
        public override void WriteProgram(Work w)
        {
            Console.WriteLine("当前时间{0}点下班回家了", w.Hour);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zhuangtai
{
    class Sleeping:State
    {
        public override void WriteProgram(Work w)
        {
            Console.WriteLine("当前时间{0}不行了睡着了", w.Hour);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zhuangtai
{
    class Program
    {
        static void Main(string[] args)
        {
            Work ep = new Work();
            ep.Hour=9;
            ep.WriteProgram();
            ep.Hour = 10;
            ep.WriteProgram();
            ep.Hour = 11;
            ep.WriteProgram();
            ep.Hour=12;
            ep.WriteProgram();
            ep.Hour=13;
            ep.WriteProgram();
            ep.Hour=14;
            ep.WriteProgram();
            ep.Hour=15;
            ep.WriteProgram();
            ep.Hour=16;
            ep.WriteProgram();
            ep.TaskFinished = false;
            ep.Hour=17;
            ep.WriteProgram();
            ep.Hour=18;
            ep.WriteProgram();
            ep.Hour=19;
            ep.WriteProgram();
            
            ep.WriteProgram();
            ep.Hour = 20;
            ep.Hour = 21;
            ep.WriteProgram();
            ep.Hour = 22;
            ep.WriteProgram();
            ep.Hour = 23;
            ep.WriteProgram();
            ep.Hour = 24;
            ep.WriteProgram();
            
            
        }
    }
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/89476730