C# Design Pattern --- Responsibility Chain Pattern

Chain of Responsibility

Chain of Responsibility (Chain of Responsibility) is an object behavior pattern. In the Chain of Responsibility pattern, many objects are connected to form a chain by each object's reference to its next home. Requests are passed on the chain until an object on the chain decides to handle the request. The client making this request does not know which object on the chain will eventually handle the request, which allows the system to dynamically reorganize and assign responsibilities without affecting the client. That is, usually each receiver contains a reference to another receiver. If an object cannot handle the request, it passes the same request to the next recipient, and so on.

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

 

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/131598864