• 责任链设计模式


    责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。

    在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。

    通过以下一个场景,以代码的形式进行说明责任链的实现

    人物:

    员工(Staff):小明、组长(GroupLeader):李磊、课长(Charge):张越、部长(minister):任盈盈、总经理(Manager):令狐冲

    职位关系:

    员工<组长<课长<部长

    情景:

    小明刚认识了一个女孩(菜花),想请假找她玩,以增进和她的感情。

    请假流程:

    请假8小时以内,组长可以决定,超过8小时则需要课长审批,

    请假16小时以内,课长可以决定,超过16小时则需要部长审批,

     请假24小时以内,部长可以决定,超过24小时则需要总经理审批,

     请假32小时以内,总经理可以决定,超过32小时审批不通过

    开始:

    打开VS,新建控制台应用程序,添加申请请假单类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignConsoleApp1
    {
        /// <summary>
        /// 请假申请单
        /// </summary>
      public  class ApplyContext
        {
            /// <summary>
            /// 请假人
            /// </summary>
            public String Name { get; set; }
            /// <summary>
            /// 请假小时数
            /// </summary>
            public int Hour { get; set; }
            /// <summary>
            /// 审核是否通过
            /// </summary>
            public Boolean IsAuditing { get; set; }
            /// <summary>
            /// 审核建议
            /// </summary>
            public String Opinion { get; set; }
        }
    }
    请假申请单

    添加审核基类(抽象类)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignConsoleApp1
    {
        /// <summary>
        /// 审核基类
        /// </summary>
      public abstract class BaseAuditor
        {
            /// <summary>
            /// 审核人
            /// </summary>
            public String Name { get; set; }
            /// <summary>
            /// 这个很关键,代表下一个审核人
            /// </summary>
            public BaseAuditor _nextAuditor { get; private set; }
            /// <summary>
            /// 设置下一个审核人
            /// </summary>
            /// <param name="baseAuditor"></param>
            public void setNextAuditor(BaseAuditor baseAuditor) {
                this._nextAuditor = baseAuditor;
            }
            /// <summary>
            /// 审核方法
            /// </summary>
            /// <param name="applyContext"></param>
            public abstract void auditor(ApplyContext applyContext);
            //员工(Staff):小明、组长(GroupLeader):李磊、课长(Charge):张越、部长(minister):任盈盈、总经理(Manager):令狐冲
        }
    }
    审核基类(抽象类)

    添加组长(GroupLeader),继承审核基类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignConsoleApp1
    {
        /// <summary>
        /// 组长类
        /// </summary>
        public class GroupLeader : BaseAuditor
        {
            /// <summary>
            /// 实现基类的抽象审核方法
            /// </summary>
            /// <param name="applyContext"></param>
            public override void auditor(ApplyContext applyContext)
            {
                if (applyContext.Hour <= 8)
                {
                    applyContext.Opinion += "
    
    ;" + base.Name + "组长审核通过";
                }
                else {
                    if (base._nextAuditor != null)
                    {
                        base._nextAuditor.auditor(applyContext);
                    }
                    else {
                        applyContext.Opinion += "
    
    ;" + base.Name + "组长审核没有通过,理由:时间太长";
                    }
                }
            }
        }
    }
    组长类(GroupLeader )

    课长(Charge)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignConsoleApp1
    {
        /// <summary>
        /// 课长类
        /// </summary>
        public class Charge : BaseAuditor
        {
            /// <summary>
            /// 实现基类的抽象审核方法
            /// </summary>
            /// <param name="applyContext"></param>
            public override void auditor(ApplyContext applyContext)
            {
                if (applyContext.Hour <=16 )
                {
                    applyContext.Opinion += "
    
    ;" + base.Name + "课长审核通过";
                }
                else {
                    if (base._nextAuditor != null)
                    {
                        base._nextAuditor.auditor(applyContext);
                    }
                    else {
                        applyContext.Opinion += "
    
    ;" + base.Name + "课长审核没有通过,理由:时间太长";
                    }
                }
            }
        }
    }
    课长(Charge)

    部长(minister)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignConsoleApp1
    {
        /// <summary>
        /// 部长类
        /// </summary>
        public class Minister : BaseAuditor
        {
            /// <summary>
            /// 实现基类的抽象审核方法
            /// </summary>
            /// <param name="applyContext"></param>
            public override void auditor(ApplyContext applyContext)
            {
                if (applyContext.Hour <= 8)
                {
                    applyContext.Opinion += "
    
    ;" + base.Name + "部长审核通过";
                }
                else {
                    if (base._nextAuditor != null)
                    {
                        base._nextAuditor.auditor(applyContext);
                    }
                    else {
                        applyContext.Opinion += "
    
    ;" + base.Name + "部长审核没有通过,理由:时间太长";
                    }
                }
            }
        }
    }
    部长(minister)

    总经理(Manager)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignConsoleApp1
    {
        /// <summary>
        /// 总经理类
        /// </summary>
        public class Manager : BaseAuditor
        {
            /// <summary>
            /// 实现基类的抽象审核方法
            /// </summary>
            /// <param name="applyContext"></param>
            public override void auditor(ApplyContext applyContext)
            {
                if (applyContext.Hour <= 8)
                {
                    applyContext.Opinion += "
    
    ;" + base.Name + "总经理审核通过";
                }
                else {
                    if (base._nextAuditor != null)
                    {
                        base._nextAuditor.auditor(applyContext);
                    }
                    else {
                        applyContext.Opinion += "
    
    ;" + base.Name + "总经理审核没有通过,理由:时间太长";
                    }
                }
            }
        }
    }
    总经理(Manager)

    层级关系类,职位人员的创建及职位关系的建立

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignConsoleApp1
    {
        /// <summary>
        /// 层级关系类,职位人员的创建及职位关系的建立
        /// </summary>
       public class Relationship
        {
            private static BaseAuditor baseAuditor;
            static Relationship() {
                Manager manager = new Manager() {
                    Name="令狐冲"
                };
                Minister minister = new Minister()
                {
                    Name = "任盈盈",
                };
                Charge charge = new Charge() { Name="张越"};
                GroupLeader groupLeader = new GroupLeader() {
                    Name = "小明"
                };
                //设置上下级关系
                groupLeader.setNextAuditor(charge);
                charge.setNextAuditor(minister);
                minister.setNextAuditor(manager);
                baseAuditor = groupLeader;
            }
            public static BaseAuditor getBaseAuditor() {
                return baseAuditor;
            }
        }
    }
    层次关系类

     请假情景开始:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool bo = true;
                ApplyContext applyContext = new ApplyContext() {
                    Name="小明",
                    Opinion= "请假理由:为了祖国下一代,约个妹子谈恋爱
    
    "
                };
                BaseAuditor baseAuditor = Relationship.getBaseAuditor();
                while (bo) {
                    Console.WriteLine("请输入大于0的请假小时数,0或者非数字会退出");
                    try
                    {
                        applyContext.Hour= int.Parse( Console.ReadLine());
                        //审核
                        baseAuditor.auditor(applyContext);
                        if (applyContext.IsAuditing) {
                            Console.WriteLine(applyContext.Opinion + "
    
    打电话给胖妞,亲爱的胖妞,我的请假通过了
    
    胖妞说:好的,我给你做你最喜欢吃的酸菜");
                        } else {
                            Console.WriteLine(applyContext.Opinion + "
    
    打电话给胖妞,胖妞,我的请假没有通过了
    
    胖妞说:你可以请假时间短一点,你再试试");
                        }
                        applyContext.Opinion = "";
                        Console.WriteLine("=================================");
                    }
                    catch (Exception ex) {
    
                        Console.WriteLine(ex.Message);
                        bo = false;
                    }
                }
                Console.Read();
            }
        }
    }
    请假情景开始

     

  • 相关阅读:
    多项式大合集
    【题解】Codeforces 961G Partitions
    【题解】Counting D-sets(容斥+欧拉定理)
    【题解】分特产(组合数+容斥)
    【题解】P4247 [清华集训]序列操作(线段树修改DP)
    【题解】没有上司的舞会
    【题解】数字组合(NTT+组合 滑稽)
    【瞎总结】组合模型及其组合意义的阐释
    P2822 组合数问题——巧用前缀和
    P3239 [HNOI2015]亚瑟王——概率DP
  • 原文地址:https://www.cnblogs.com/Torey/p/9650689.html
Copyright © 2020-2023  润新知