设计模式-责任链设计模式
责任链设计模式,是行为型设计模式的巅峰之作。
现在有一个场景,请假申请。请假时间的长短,需要不同级别的领导才能审批。
万物皆对象嘛,请假需要工号、姓名、原因、时长、结果等等,那我们来定义一个请假的类。其实其就是一个上下文环境(Context),保存业务处理中参数-----中间结果-----最终结果。行为型设计模式重用的标配,把行为转移。
public class ApplyContext { public int Id { get; set; } public string Name { get; set; } /// <summary> /// 请假时长 /// </summary> public int Hour { get; set; } public string Description { get; set; } public bool AuditResult { get; set; } public string AuditRemark { get; set; } }
请假需要new一个实例:
ApplyContext context = new ApplyContext() { Id = 506, Name = "bingle", Hour = 100, Description = "世界那么大,我想去看看!", AuditResult = false, AuditRemark = "" };
第一种方式,审批逻辑都写在上端,直接就是需求翻译,没有任何加工,谈不上什么扩展,面向过程:
if (context.Hour <= 8) { Console.WriteLine("PM审批通过"); } else if (context.Hour <= 16) { Console.WriteLine("主管审批通过"); } else { Console.WriteLine("************"); }
第二种方式,面向对象:封装--继承--多态,转移业务逻辑,也只是一个翻译机,翻译完+面向对象,其实完全没有涉及加工没有思考。
领导类S:
public class PM { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 8) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } } } public class Charge { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 16) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } } } public class Manager { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 24) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } } } public class Chief { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 48) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } } } public class CEO : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 96) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } } } public class ChiarMan : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 1000000) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } } }
下面进行逻辑的翻译:
AbstractAuditor pm = new PM() { Name = "bingle1" }; pm.Audit(context); if (!context.AuditResult) { AbstractAuditor charge = new Charge() { Name = "bingle2" }; charge.Audit(context); if (!context.AuditResult) { AbstractAuditor manager = new Manager() { Name = "bingle3" }; manager.Audit(context); if (!context.AuditResult) { //找下一环节 } } }
不要直接翻译的逻辑,需要涉及,现在的场景分析是,1、权限范围内,审批通过;2、权限范围外,交给下一环节审批,写的代码又多了一个,指定洗衣环节,甩锅大法开始。
首先定义一个抽象父类:
public abstract class AbstractAuditor { public string Name { get; set; } public abstract void Audit(ApplyContext context); private AbstractAuditor _NextAuditor = null; public void SetNext(AbstractAuditor auditor) { this._NextAuditor = auditor; } protected void AuditNext(ApplyContext context) { if (this._NextAuditor != null) { this._NextAuditor.Audit(context); } else { context.AuditResult = false; context.AuditRemark = "不允许请假!"; } } }
领导类需要继承这个抽象类:
public class PM : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 8) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } else { AbstractAuditor charge = new Charge() { Name = "象扑君" }; charge.Audit(context); } } }
其他的领导类以此类推.....
调用的时候,只需要找最低权限的人,去审批
AbstractAuditor pm = new PM() { Name = "bingle" }; pm.Audit(context);
以上,转移了申请提交的逻辑,这次符合实际情况,有了自己的思考和设计在里面。
甩锅大法开始了,一样,领导类需要实现抽象父类,不过在Audit方法中稍作了改动
public class PM : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 8) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } else { base.AuditNext(context); } } } public class Manager : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 24) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } else { base.AuditNext(context); } } } public class Chief : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 48) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } else { base.AuditNext(context); } } } public class ChiarMan : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 1000000) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } else { base.AuditNext(context); } } } public class Charge: AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 16) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } else { base.AuditNext(context); } } } public class CEO : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine($"This is {this.GetType().Name} {this.Name} Audit"); if (context.Hour <= 96) { context.AuditResult = true; context.AuditRemark = "允许请假!"; } else { base.AuditNext(context); } } }
下面需要找领导审批了:
AbstractAuditor pm = new PM() { Name = "bingle1" }; AbstractAuditor charge = new Charge() { Name = "bingle1" }; AbstractAuditor manager = new Manager() { Name = "bingle1" }; AbstractAuditor chief = new Chief() { Name = "bingle1" }; AbstractAuditor ceo = new CEO() { Name = "bingle1" }; //pm.SetNext(charge); //charge.SetNext(manager); pm.SetNext(manager); manager.SetNext(chief); chief.SetNext(ceo); ceo.SetNext(new ChiarMan() { Name = "bingle1" }); //流程的可扩展 pm.Audit(context); if (!context.AuditResult) { Console.WriteLine("不干了!"); }
以上,把流畅环节逻辑从业务类转移了。