其他分享
首页 > 其他分享> > 设计模式——责任链模式

设计模式——责任链模式

作者:互联网

1.定义基础类

    public abstract class AbstractAuditor
    {
        public string Name { get; set; }

        public abstract void Audit(ApplyContext context);

        private AbstractAuditor _NextAuditor = null;

        public void SetNextAuditor(AbstractAuditor nextAuditor)
        {
            this._NextAuditor = nextAuditor;
        }
         
        protected void ExeNextAuditor(ApplyContext context)
        {
            if (_NextAuditor != null)
            {
                _NextAuditor.Audit(context);
            }
        }

    }

2.各级审核基础该类

 /// <summary>
    /// 职责问题: 
    /// 1.权限以内审批通过
    /// 2.权限以外,自动转交给上级 
    /// </summary>
    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
            {             
                ExeNextAuditor(context);

            }
        }
    }

3.调用

  AbstractAuditor pm = new PM()

            AbstractAuditor charge = new Charge()
           
            AbstractAuditor chief = new Chief()
           
            AbstractAuditor ceo = new CEO()
         
            pm.SetNextAuditor(ceo);
            charge.SetNextAuditor(chief);
            chief.SetNextAuditor(ceo);
pm.Audit(context);

  

标签:Audit,AbstractAuditor,NextAuditor,模式,责任,context,new,设计模式,public
来源: https://www.cnblogs.com/yyl001/p/16545489.html