(精华)2020年8月14日 C#基础知识点 23种设计模式(行为型模式---责任链模式)

public static void Show()
        {
            AbstractAuditor pm = new PM()
            {
                Name = "清茶"
            };
            AbstractAuditor charge = new Charge()
            {
                Name = "腾坤"
            };
            pm.SetNext('ceo');//保证了环节的稳定--可以灵活配置环节

            pm.Audit('类');
        }
        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)
            {
                this._NextAuditor?.Audit(context);
            }
        }

        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 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);
                }
            }
        }

你可能感兴趣的:(C#,c#)