상태 모드 인 스 턴 스

5270 단어 디자인 모드
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 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
{
    class AfteroonState:State
    {
        public override void WriteProgram(Work w)
        {
           if(w.Hour<17)
            {
                Console.WriteLine("    {0}        ",w.Hour);
            }
            else
            {w.setState(new eveningState());
            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 < 21)
                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();
            
            
        }
    }
}

좋은 웹페이지 즐겨찾기