1.模板方法:其實就是抽象出來一層出來
2.觀察者模式:事件的方式
3.責任鏈模式:
請假審批:
AbstractAuditor auditor = AuditorBuilder.Build();
auditor.Audit(context);
可以指定是哪一級,使用SetNext指定下一流程是哪個。
public class AuditorBuilder { public static AbstractAuditor Build() { //更具備擴充套件性 AbstractAuditor pm1 = new PM(); AbstractAuditor charge = new Charge(); AbstractAuditor manager = new Manager(); pm1.SetNext(charge); charge.SetNext(manager); //反射+配置檔案 return pm1; } }
抽象類如下:裡面包含方法如下:
public abstract class AbstractAuditor { public string Name { get; set; } public abstract void Audit(ApplyContext context); private AbstractAuditor _Auditor = null; public void SetNext(AbstractAuditor auditor) { this._Auditor = auditor; } public void AuditNext(ApplyContext context) { if (_Auditor != null) { _Auditor.Audit(context); } } }
PM例項:
class PM :AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine("這裡是專案經理審批"); if (context.Hour <= 8) { context.AuditResult = true; context.AnditRemark = "enjoy your vacation"; } else { //Console.WriteLine("這裡是主管審批"); //AbstractAuditor abstractAuditor = new Charge(); //abstractAuditor.Audit(context); //if(_Auditor != null) //{ // _Auditor.Audit(context); //} base.AuditNext(context); } } }
Charge例項:
class Charge : AbstractAuditor { public override void Audit(ApplyContext context) { Console.WriteLine("這裡是主管審批"); if (context.Hour <= 16) { context.AuditResult = true; context.AnditRemark = "enjoy your vacation"; } else { base.AuditNext(context); } } }