C# 设计模式 责任链模式例子

请添加图片描述

1、请求

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chain
{
    public class Request
    {
        /// <summary>
        /// 请求类型
        /// </summary>
        public string RequestTypes { get; set; }

        /// <summary>
        /// 费用
        /// </summary>
        public int money { get; set; }


    }

    public enum RequestType
    {
        // 转账
        Tansfer, 
        // 现金
        Cash_out
    }
}

2、风控抽象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chain
{
    // 风控抽象类
    public abstract class RiskControlManager
    {
        protected string name;

        // 更严格风控
        protected RiskControlManager superior;

        public RiskControlManager(string name)
        {
            this.name = name;
        }

        // 设置更加严格的风控
        public void SetSuperior(RiskControlManager superior)
        {
            this.superior = superior;
        }

        // 处理请求
        public abstract void HandlerRequest(Request request);
    }
}

3、具体处理类1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chain
{
    public class FirstRiskControl : RiskControlManager
    {
        public FirstRiskControl(string name):base(name)
        {
            
        }

        public override void HandlerRequest(Request request)
        {
            //(RequestType)Enum.Parse(typeof(RequestType), request.RequestTypes) == RequestType.Tansfer
            if (request.RequestTypes != "" &&
                request.money <= 1000)
            {
                Console.WriteLine("普通操作,输入支付密码即可。");
                Console.WriteLine(name + ":" + request.RequestTypes +",金额:" + request.money.ToString() + " 处理完成");
            }
            else
            {
                // 下个节点进行处理
                if(superior != null)
                {
                    superior.HandlerRequest(request);
                }
            }
        }
    }
}

4、具体处理类2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chain
{
    public class SecondRiskControl : RiskControlManager
    {
        public SecondRiskControl(string name) : base(name)
        {

        }

        public override void HandlerRequest(Request request)
        {
            //(RequestType)Enum.Parse(typeof(RequestType), request.RequestTypes) == RequestType.Tansfer
            if (request.RequestTypes != "" &&
                request.money > 1000 && request.money <= 10000)
            {
                Console.WriteLine("稍大操作,输入支付密码 + 验证码");
                Console.WriteLine(name + ":" + request.RequestTypes + ",金额:" + request.money.ToString() + " 处理完成");
            }
            else
            {
                // 下个节点进行处理
                if (superior != null)
                {
                    superior.HandlerRequest(request);
                }
            }
        }
    }
}

5、具体处理类3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chain
{
    public class ThirdRiskControl : RiskControlManager
    {
        public ThirdRiskControl(string name) : base(name)
        {

        }

        public override void HandlerRequest(Request request)
        {
            if (request.RequestTypes != "" &&
                request.money > 10000)
            {
                Console.WriteLine("大额操作,输入支付密码 + 验证码 + 人脸识别。");
                Console.WriteLine(name + ":" + request.RequestTypes + ",金额:" + request.money.ToString() + " 处理完成");
            }
            else
            {
                // 下个节点进行处理
                if (superior != null)
                {
                    superior.HandlerRequest(request);
                }
            }
        }
    }
}

6、责任链接使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chain
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建风控流程
            RiskControlManager firstRiskControl = new FirstRiskControl("初级风控");
            RiskControlManager secondRiskControl = new SecondRiskControl("中级风控");
            RiskControlManager thirdRiskControl = new ThirdRiskControl("高级风控");

            // 风控流程 创建
            firstRiskControl.SetSuperior(secondRiskControl);
            secondRiskControl.SetSuperior(thirdRiskControl);

            // 创建请求
            Request request1 = new Request();
            request1.RequestTypes = RequestType.Tansfer.ToString();


            // 处理请求
            Console.WriteLine("-------------------------------------------");
            request1.money = 200;
            firstRiskControl.HandlerRequest(request1);

            // 处理请求
            Console.WriteLine("-------------------------------------------");
            request1.money = 2000;
            firstRiskControl.HandlerRequest(request1);

            // 处理请求
            Console.WriteLine("-------------------------------------------");
            request1.money = 20000;
            firstRiskControl.HandlerRequest(request1);

            Console.ReadLine();
        }
    }
}

在这里插入图片描述

优缺点

优点:
1、客户只需将需求放入责任链即可
2、可以动态调整 责任链接
3、增强系统的扩展性
4、每个类只需要做好自己的事情即可

缺点:
1、处理过度分散
2、不能保证每次、某个处理被调用,调试比较复杂

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值