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、不能保证每次、某个处理被调用,调试比较复杂