C# 设计模式之 状态模式
来源:互联网 发布:linux压缩包命令 编辑:程序博客网 时间:2024/05/18 20:53
拆分复杂的if-else判断分支 把这些逻辑放在类里面完成
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPytternDemo{ public interface ITime { void SayHello(); } public abstract class BaseTime : ITime { public int Time { get; set; } public abstract void SayHello(); public BaseTime(int t) { this.Time = t; } } public class Morning : BaseTime { public Morning(int t) : base(t) { } public override void SayHello() { if (this.Time >= 7 && this.Time < 12) { Console.WriteLine("good morning,sir" + " time is {0}" , this.Time); } else { new Noon(this.Time).SayHello(); } } } public class Noon : BaseTime { public Noon(int t) : base(t) { } public override void SayHello() { if (this.Time >= 12 && this.Time <= 13) { Console.WriteLine("have a nice lunch ,sir" + " time is {0}" , this.Time); } else { new AfterNoon(this.Time).SayHello(); } } } public class AfterNoon : BaseTime { public AfterNoon(int t) : base(t) { } public override void SayHello() { if (this.Time > 13 && this.Time <= 18) { Console.WriteLine("good afternoon,sir" + " time is {0}" , this.Time); } else { new Night(this.Time).SayHello(); } } } public class Night : BaseTime { public Night(int t) : base(t) { } public override void SayHello() { if (this.Time > 18 && this.Time <= 24) { Console.WriteLine("good evening,sir" + " time is {0}" ,this.Time); } else { new DeepNight(this.Time).SayHello(); } } } public class DeepNight : BaseTime { public DeepNight(int t) : base(t) { } public override void SayHello() { if (this.Time >= 0 && this.Time < 7) { Console.WriteLine("have a good sleep,sir" + " time is {0}" ,this.Time); } else { Console.WriteLine("oh,unexpected time {0},", this.Time); } } }} static void Main(string[] args) { BaseTime bt = new Morning(1); bt.SayHello(); bt.Time = 8; bt.SayHello(); bt.Time = 12; bt.SayHello(); bt.Time = 14; bt.SayHello(); bt.Time = 21; bt.SayHello(); Console.ReadLine(); }
- C# 设计模式之 状态模式
- [设计模式] 状态模式-C#
- 【C#设计模式-状态模式】
- C#设计模式之21——状态模式
- C#设计模式之状态者模式(State Pattern)
- C#设计模式学习小结之四 状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之状态模式
- 设计模式之--状态模式
- android_应用开发之(使用<include>标签重用布局)
- One Person
- Java数据库连接池(一)
- js动态给table添加行(tr)
- win7 下打开 ice demo 工程文件出错的问题解决方法
- C# 设计模式之 状态模式
- ASP.NET必须知道的:HttpModule,HttpHandler
- 使用 window.name 解决跨域问题
- tempdb 表空间监控
- C++ 私有、保护、公有继承
- MODIS数据说明
- 自定义消息机制研究学习(二)——做一些改动,定制自己的消息机制
- Weblogic WLST scripting memo
- 应用服务器发生 hang 的诊断方法