责任链模式(Chain of Responsibility)
责任链模式(Chain of Responsibility)是一种对象行为模式。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织和分配责任。也就是通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。
using System; using System.Collections.Generic; namespace ConsoleApplication { //一般每个接口或类都写在单独的.cs文件中 //本示例为了执行查看方便才写在一起 /// /// 请假申请 /// public class ApplyContext { public int Id { get; set; } public string Name { get; set; } /// /// 请假时长 /// public int Hour { get; set; } public string Description { get; set; } public bool AuditResult { get; set; } public string AuditRemark { get; set; } } public class AuditorManager { private static BaseAuditor AuditorProcess = null; static AuditorManager() { PM pm = new PM() { Name = "cjavapy" }; Charge charge = new Charge() { Name = "coder" }; Manager manager = new Manager() { Name = "leader" }; CTO cto = new CTO() { Name = "levi" }; pm.SetNext(manager); manager.SetNext(cto); AuditorProcess = pm; } public static BaseAuditor GetAuditor() { return AuditorProcess; } } public abstract class BaseAuditor { protected BaseAuditor _NextAudtitor { get; private set; } public void SetNext(BaseAuditor nextAuditor) { this._NextAudtitor = nextAuditor; } protected void AuditNext(ApplyContext context) { if (this._NextAudtitor == null) { context.AuditResult = false; context.AuditRemark = "审批不通过"; } else { this._NextAudtitor.Audit(context); } } public string Name { get; set; } public abstract void Audit(ApplyContext context); } public class Charge : BaseAuditor { public override void Audit(ApplyContext context) { if (context.Hour <= 16) { context.AuditResult = true; context.AuditRemark = "C#"; } else { if (base._NextAudtitor == null) { context.AuditResult = false; context.AuditRemark = "审批不通过"; } else { base._NextAudtitor.Audit(context); } } } } public class CTO : BaseAuditor { public override void Audit(ApplyContext context) { if (context.Hour <= 40) { context.AuditResult = true; context.AuditRemark = Name + " audit"; } else { if (base._NextAudtitor == null) { context.AuditResult = false; context.AuditRemark = "审批不通过"; } else { base._NextAudtitor.Audit(context); } } } } public class Manager : BaseAuditor { public override void Audit(ApplyContext context) { if (context.Hour <= 24) { context.AuditResult = true; context.AuditRemark = Name+ " audit"; } else { if (base._NextAudtitor == null) { context.AuditResult = false; context.AuditRemark = "审批不通过"; } else { base._NextAudtitor.Audit(context); } } } } public class PM : BaseAuditor { //PM:<=8 通过申请 // >8 让上级审批 // 指定了上级 public override void Audit(ApplyContext context) { context.AuditRemark += "PM开始处理"; if (context.Hour <= 8) { context.AuditResult = true; context.AuditRemark = "run"; } else { base.AuditNext(context); } } } class Program { static void Main(string[] args) { ApplyContext context = new ApplyContext() { Id = 11, Name = "coder", Hour = 10, Description = "我想玩游戏", AuditResult = false, AuditRemark = "" }; BaseAuditor auditor = AuditorManager.GetAuditor(); auditor.Audit(context); if (context.AuditResult) { Console.WriteLine("审批通过"); } else { Console.WriteLine("审批没通过"); } Console.WriteLine(context.AuditRemark); } } }