23种设计模式-21-责任链模式

请假申请:

/// <summary>
    /// 请假申请对象
    /// </summary>
    public class LeaveApply
    {
        public LeaveApply(string applyUserName, int applyDays)
        {
            this.ApplyUserName = applyUserName;
            this.ApplyDays = applyDays;
            Console.WriteLine("{0}申请请假{1}天!", applyUserName, applyDays);
            this.ApproverRecords = new List<ApproverRecord>();
        }

        /// <summary>
        /// 请假人姓名
        /// </summary>
        public string ApplyUserName { get; set; }

        /// <summary>
        /// 请假时长(单位:天)
        /// </summary>
        public int ApplyDays { get; set; }

        /// <summary>
        /// 审批记录
        /// </summary>
        public List<ApproverRecord> ApproverRecords { get; set; }

        /// <summary>
        /// 审批结果
        /// </summary>
        public bool? ApproveResult { get; set; }
    }

审批对象(抽象类)

/// <summary>
    /// 审批人对象(抽象)
    /// </summary>
    public abstract class Approver
    {
        public Approver(string approveName)
        {
            this.ApproveName = approveName;
        }

        private Approver _NextApprover;

        /// <summary>
        /// 下一级审批人
        /// </summary>
        public Approver NextApprover
        {
            get { return _NextApprover; }
            set
            {
                Console.WriteLine("将{0}的下一级审批人设置为{1}", this.ApproveName, value.ApproveName);
                _NextApprover = value;
            }
        }

        /// <summary>
        /// 审批人姓名
        /// </summary>
        public string ApproveName { get; set; }

        /// <summary>
        /// 审批的方法,不同实现类责任不同,所以交给实现类去实现
        /// </summary>
        /// <param name="leaveApply"></param>
        public abstract void ApproveApply(LeaveApply leaveApply);
    }

审批人实现类:

1.部门主管:

/// <summary>
    /// 部门主管
    /// </summary>
    public class DeptManager : Approver
    {
        public DeptManager(string approveName) : base(approveName)
        {

        }

        public override void ApproveApply(LeaveApply leaveApply)
        {
            var record = new ApproverRecord(this);
            leaveApply.ApproverRecords.Add(record);
            if (leaveApply.ApplyDays < 3)
            {
                record.Result = true;
                leaveApply.ApproveResult = true;
                Console.WriteLine("{0}的请假时间小于3天,我直接审批通过,审批结束!", leaveApply.ApplyUserName);
            }
            else
            {
                Console.WriteLine("{0}的请假时间超过3天,我审批通过,接下来交给{1}审批!", leaveApply.ApplyUserName, this.NextApprover.ApproveName);
                this.NextApprover.ApproveApply(leaveApply);
            }
        }
    }

2.部门总监:

/// <summary>
    /// 部门总监
    /// </summary>
    public class DeptInspector : Approver
    {
        public DeptInspector(string approveName) : base(approveName)
        {

        }

        public override void ApproveApply(LeaveApply leaveApply)
        {
            var record = new ApproverRecord(this);
            leaveApply.ApproverRecords.Add(record);
            if (leaveApply.ApplyDays < 7)
            {
                record.Result = true;
                leaveApply.ApproveResult = true;
                Console.WriteLine("{0}的请假时间小于7天,我审批通过,审批结束!", leaveApply.ApplyUserName);
            }
            else
            {
                Console.WriteLine("{0}的请假时间超过7天,我审批通过,接下来交给{1}审批!", leaveApply.ApplyUserName, this.NextApprover.ApproveName);
                this.NextApprover.ApproveApply(leaveApply);
            }
        }
    }

3.老板:

/// <summary>
    /// 老板
    /// </summary>
    public class Boss : Approver
    {
        public Boss(string approveName) : base(approveName)
        {

        }

        public override void ApproveApply(LeaveApply leaveApply)
        {
            var record = new ApproverRecord(this);
            leaveApply.ApproverRecords.Add(record);
            if (leaveApply.ApplyDays < 15)
            {
                record.Result = true;
                leaveApply.ApproveResult = true;
                Console.WriteLine("{0}的请假时间小于15天,我审批通过,审批结束!", leaveApply.ApplyUserName);
            }
            else
            {
                Console.WriteLine("{0}请假时长超过15天,公司需要开会讨论下才能给出审批结果!", leaveApply.ApplyUserName);
            }
        }
    }

审批记录实体(无关紧要):

/// <summary>
    /// 审批记录实体
    /// </summary>
    public class ApproverRecord
    {
        public ApproverRecord(Approver approver)
        {
            this.Approver = approver;
        }

        public Approver Approver { get; set; }

        public bool? Result { get; set; }
    }

测试:

class Program
    {
        /// <summary>
        /// 责任链模式:
        /// 在日常生活中,我们经常见到一种情况,就是不同的情况需要不同的对待
        ///
        /// 例如:
        /// 你需要请假,提交了请假申请后,
        /// 如果你请假时间是1-3天,你的部门主管同意就可以了,
        /// 如果你请假时间是3-7天,那你的部门主管同意后还不行,还需要你的部门总监同意才可以,
        /// 如果你请假时间大于7天,那你的部门主管和部门总监同意都还不行,必须需要老板同意才可以
        /// 上面的这种模式,就是一个很明显的责任链模式
        ///
        /// 当然我们最基础的实现, 就是使用if else不停的去判断,
        /// 这样会导致后期很难维护,并且每个模块之间的耦合性太高,使得程序没有扩展性
        ///
        /// 使用责任链模式,就可以进行解耦,并且还易于扩展
        ///
        /// 当然责任链模式也不是说没有缺点:
        /// 缺点就是导致可能会有性能的问题,还有就是有可能请求不会被处理(这个很少见)
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //初始化请假申请
            LeaveApply apply1 = new LeaveApply("张三", 1);
            LeaveApply apply2 = new LeaveApply("小明", 5);
            LeaveApply apply3 = new LeaveApply("王五", 10);
            LeaveApply apply4 = new LeaveApply("王五", 15);
            //初始化审批人
            Approver deptManager = new DeptManager("部门主管");
            Approver deptInspector = new DeptInspector("部门总监");
            Approver boss = new Boss("老板");

            Console.WriteLine("-------------------------------------------------");
            //设置责任链
            deptManager.NextApprover = deptInspector;
            deptInspector.NextApprover = boss;

            Console.WriteLine("-------------------------------------------------");
            //开始审批
            deptManager.ApproveApply(apply1);
            Console.WriteLine("-------------------------------------------------");
            deptManager.ApproveApply(apply2);
            Console.WriteLine("-------------------------------------------------");
            deptManager.ApproveApply(apply3);
            Console.WriteLine("-------------------------------------------------");
            deptManager.ApproveApply(apply4);

            Console.ReadKey();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值