设计模式学习笔记--状态模式
Posted bzyzhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式学习笔记--状态模式相关的知识,希望对你有一定的参考价值。
1 using System; 2 3 namespace State 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/28 19:44:40 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// Work说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class Work 12 { 13 private State current; 14 15 public Work() 16 { 17 current = new ForenoonState(); 18 } 19 20 private double hour; 21 22 public double Hour 23 { 24 get { return hour; } 25 set { hour = value; } 26 } 27 28 private bool finish = false; 29 30 public bool TaskFinished 31 { 32 get { return finish; } 33 set { finish = value; } 34 } 35 36 public void SetState(State state) 37 { 38 current = state; 39 } 40 41 public void WriteProgram() 42 { 43 current.WriteProgram(this); 44 } 45 } 46 }
1 using System; 2 3 namespace State 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/28 19:43:56 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// State说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public abstract class State 12 { 13 public abstract void WriteProgram(Work work); 14 } 15 }
1 using System; 2 3 namespace State 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/28 19:45:39 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// ForenoonState说明:上午工作状态 10 /// </summary> 11 public class ForenoonState:State 12 { 13 public override void WriteProgram(Work work) 14 { 15 if (work.Hour < 12) 16 { 17 Console.WriteLine("当前时间:{0}点 上午工作,精神百倍",work.Hour); 18 } 19 else 20 { 21 work.SetState(new NoonState()); 22 work.WriteProgram(); 23 } 24 } 25 } 26 }
1 using System; 2 3 namespace State 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/28 19:52:33 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// NoonState说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class NoonState:State 12 { 13 public override void WriteProgram(Work work) 14 { 15 if (work.Hour < 13) 16 { 17 Console.WriteLine("当前时间:{0}点 饿了,午饭:犯困,午休。", work.Hour); 18 } 19 else 20 { 21 work.SetState(new AfternoonState()); 22 work.WriteProgram(); 23 } 24 } 25 } 26 }
1 using System; 2 3 namespace State 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/28 19:54:17 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// AfternoonState说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class AfternoonState:State 12 { 13 public override void WriteProgram(Work work) 14 { 15 if (work.Hour < 17) 16 { 17 Console.WriteLine("当前时间:{0}点 下午状态还不错,继续努力。", work.Hour); 18 } 19 else 20 { 21 work.SetState(new EveningState()); 22 work.WriteProgram(); 23 } 24 } 25 } 26 }
1 using System; 2 3 namespace State 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/28 19:56:32 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// EveningState说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class EveningState:State 12 { 13 public override void WriteProgram(Work work) 14 { 15 if (work.TaskFinished) 16 { 17 work.SetState(new RestState()); 18 work.WriteProgram(); 19 } 20 else 21 { 22 if (work.Hour < 21) 23 { 24 Console.WriteLine("当前时间:{0}点 加班哦,疲累至极!", work.Hour); 25 } 26 else 27 { 28 work.SetState(new SleepingState()); 29 work.WriteProgram(); 30 } 31 } 32 } 33 } 34 }
1 using System; 2 3 namespace State 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/28 19:58:45 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// SleepingState说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class SleepingState:State 12 { 13 public override void WriteProgram(Work work) 14 { 15 Console.WriteLine("当前时间:{0}点 不行了,睡着了!", work.Hour); 16 } 17 } 18 }
1 using System; 2 3 namespace State 4 { 5 /// <summary> 6 /// 作者:bzyzhang 7 /// 时间:2016/5/28 19:59:52 8 /// 博客地址:http://www.cnblogs.com/bzyzhang/ 9 /// RestState说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 10 /// </summary> 11 public class RestState:State 12 { 13 public override void WriteProgram(Work work) 14 { 15 Console.WriteLine("当前时间:{0}点 下班回家了!", work.Hour); 16 } 17 } 18 }
1 namespace State 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Work emergencyProjects = new Work(); 8 emergencyProjects.Hour = 9; 9 emergencyProjects.WriteProgram(); 10 emergencyProjects.Hour = 10; 11 emergencyProjects.WriteProgram(); 12 emergencyProjects.Hour = 12; 13 emergencyProjects.WriteProgram(); 14 emergencyProjects.Hour = 13; 15 emergencyProjects.WriteProgram(); 16 emergencyProjects.Hour = 14; 17 emergencyProjects.WriteProgram(); 18 emergencyProjects.Hour = 17; 19 emergencyProjects.WriteProgram(); 20 21 emergencyProjects.TaskFinished = true; 22 23 emergencyProjects.WriteProgram(); 24 emergencyProjects.Hour = 19; 25 emergencyProjects.WriteProgram(); 26 emergencyProjects.Hour = 22; 27 emergencyProjects.WriteProgram(); 28 } 29 } 30 }
以上是关于设计模式学习笔记--状态模式的主要内容,如果未能解决你的问题,请参考以下文章