找回密码
 立即注册
首页 业界区 安全 设计模式学习:在支付系统中的实战应用 ...

设计模式学习:在支付系统中的实战应用

薛小春 7 天前
一、策略模式:灵活切换支付方式

场景需求

系统需要支持支付宝、微信支付、银联等多种支付渠道,且可能随时新增支付方式。
模式实现


  • 定义支付策略接口
  1. public interface IPaymentStrategy
  2. {
  3.     void ProcessPayment(decimal amount, string currency);
  4. }
复制代码

  • 实现具体支付策略类
  1. // 支付宝策略
  2. public class AlipayStrategy : IPaymentStrategy
  3. {
  4.     public void ProcessPayment(decimal amount, string currency)
  5.     {
  6.         Console.WriteLine($"支付宝支付:{amount}{currency}");
  7.         // 调用支付宝SDK实现
  8.     }
  9. }
  10. // 微信支付策略(实现类似)
  11. public class WechatPayStrategy : IPaymentStrategy { /*...*/ }
复制代码

  • 创建支付上下文类
  1. public class PaymentContext
  2. {
  3.     private IPaymentStrategy _strategy;
  4.     public void SetStrategy(IPaymentStrategy strategy) => _strategy = strategy;
  5.    
  6.     public void ExecutePayment(decimal amount, string currency)
  7.     {
  8.         _strategy.ProcessPayment(amount, currency);
  9.     }
  10. }
复制代码
二、装饰器模式:动态扩展支付功能

场景需求

为支付流程添加日志记录、风控验证等附加功能。
模式实现
  1. public abstract class PaymentDecorator : IPaymentStrategy
  2. {
  3.     protected IPaymentStrategy _strategy;
  4.     public PaymentDecorator(IPaymentStrategy strategy)
  5.     {
  6.         _strategy = strategy;
  7.     }
  8.     public virtual void ProcessPayment(decimal amount, string currency)
  9.     {
  10.         _strategy.ProcessPayment(amount, currency);
  11.     }
  12. }
  13. // 日志装饰器
  14. public class LoggingDecorator : PaymentDecorator
  15. {
  16.     public LoggingDecorator(IPaymentStrategy strategy) : base(strategy) {}
  17.     public override void ProcessPayment(decimal amount, string currency)
  18.     {
  19.         Console.WriteLine($"[LOG] 支付请求:{amount}{currency}");
  20.         base.ProcessPayment(amount, currency);
  21.     }
  22. }
  23. public class RiskCheckDecorator : PaymentDecorator
  24. {
  25.     public RiskCheckDecorator(IPaymentStrategy strategy) : base(strategy) {}
  26.     public override void ProcessPayment(decimal amount, string currency)
  27.     {
  28.         Console.WriteLine("[RISK] 风控验证中...");
  29.         base.ProcessPayment(amount, currency);
  30.     }
  31. }
  32. // 使用示例
  33. var payment = new LoggingDecorator(new RiskCheckDecorator(new AlipayStrategy()));
  34. payment.ProcessPayment(100, "CNY");
复制代码
三、工厂模式:统一支付对象创建

场景需求

根据配置参数动态创建支付策略实例。
模式实现
  1. public class PaymentFactory
  2. {
  3.     public static IPaymentStrategy Create(string paymentType)
  4.     {
  5.         switch (paymentType.ToLower())
  6.         {
  7.             case "alipay":
  8.                 return new AlipayStrategy();
  9.             case "wechat":
  10.                 return new WechatPayStrategy();
  11.             case "unionpay":
  12.                 return new UnionPayStrategy();
  13.             default:
  14.                 throw new ArgumentException("不支持的支付方式");
  15.         }
  16.     }
  17. }
  18. // 调用示例
  19. var strategy = PaymentFactory.Create("alipay");
  20. strategy.ProcessPayment(200, "USD");
复制代码
四、责任链模式:支付流程处理

场景需求

处理支付请求时需要依次执行:参数校验 → 风控检查 → 实际支付 → 结果通知。
模式实现
  1. public abstract class PaymentHandler
  2. {
  3.     protected PaymentHandler _nextHandler;
  4.    
  5.     public PaymentHandler SetNext(PaymentHandler handler)
  6.     {
  7.         _nextHandler = handler;
  8.         return _nextHandler;
  9.     }
  10.     public abstract void Handle(PaymentRequest request);
  11. }
  12. // 参数校验处理器
  13. public class ValidationHandler : PaymentHandler
  14. {
  15.     public override void Handle(PaymentRequest request)
  16.     {
  17.         if (string.IsNullOrEmpty(request.OrderId))
  18.             throw new ArgumentException("订单号无效");
  19.         
  20.         _nextHandler?.Handle(request);
  21.     }
  22. }
  23. // 构建处理链
  24. var chain = new ValidationHandler();
  25. chain.SetNext(new RiskCheckHandler())
  26.      .SetNext(new PaymentProcessorHandler())
  27.      .SetNext(new NotificationHandler());
  28. chain.Handle(request);
复制代码
五、综合应用示例


  • 定义支付策略接口
  1. public interface IPaymentStrategy
  2. {
  3.     void ProcessPayment(decimal amount, string currency);
  4. }
复制代码

  • 实现具体支付策略类
  1. // 支付宝策略
  2. public class AlipayStrategy : IPaymentStrategy
  3. {
  4.     public void ProcessPayment(decimal amount, string currency)
  5.     {
  6.         Console.WriteLine($"支付宝支付:{amount}{currency}");
  7.         // 调用支付宝SDK实现
  8.     }
  9. }
  10. // 微信支付策略(实现类似)
  11. public class WechatPayStrategy : IPaymentStrategy { /*...*/ }// 银联支付策略(实现类似)public class UnionPayStrategy : IPaymentStrategy { /*...*/ }
复制代码

  • 创建支付请求类
  1. public class PaymentFactory
  2. {
  3.     public static IPaymentStrategy Create(string paymentType)
  4.     {
  5.         switch (paymentType.ToLower())
  6.         {
  7.             case "alipay":
  8.                 return new AlipayStrategy();
  9.             case "wechat":
  10.                 return new WechatPayStrategy();
  11.             case "unionpay":
  12.                 return new UnionPayStrategy();
  13.             default:
  14.                 throw new ArgumentException("不支持的支付方式");
  15.         }
  16.     }
  17. }
复制代码

  • 创建支付装饰类
  1. public abstract class PaymentDecorator : IPaymentStrategy
  2. {
  3.     protected IPaymentStrategy _strategy;
  4.     public PaymentDecorator(IPaymentStrategy strategy)
  5.     {
  6.         _strategy = strategy;
  7.     }
  8.     public virtual void ProcessPayment(decimal amount, string currency)
  9.     {
  10.         _strategy.ProcessPayment(amount, currency);
  11.     }
  12. }
  13. // 日志装饰器
  14. public class LoggingDecorator : PaymentDecorator
  15. {
  16.     public LoggingDecorator(IPaymentStrategy strategy) : base(strategy) {}
  17.     public override void ProcessPayment(decimal amount, string currency)
  18.     {
  19.         Console.WriteLine($"[LOG] 支付请求:{amount}{currency}");
  20.         base.ProcessPayment(amount, currency);
  21.     }
  22. }
复制代码

  • 创建支付上下文类
  1. public class PaymentContext
  2. {
  3.     private IPaymentStrategy _strategy;
  4.     public void SetStrategy(IPaymentStrategy strategy) => _strategy = strategy;
  5.    
  6.     public void ExecutePayment(decimal amount, string currency)
  7.     {
  8.         new LoggingDecorator(_strategy).ProcessPayment(amount, currency);
  9.     }
  10. }
复制代码

  • 创建支付处理链类
  1. public abstract class PaymentHandler
  2. {
  3.     protected PaymentHandler _nextHandler;
  4.    
  5.     public PaymentHandler SetNext(PaymentHandler handler)
  6.     {
  7.         _nextHandler = handler;
  8.         return _nextHandler;
  9.     }
  10.     public abstract void Handle(PaymentRequest request);
  11. }
  12. // 参数校验处理器
  13. public class ValidationHandler : PaymentHandler
  14. {
  15.     public override void Handle(PaymentRequest request)
  16.     {
  17.         if (string.IsNullOrEmpty(request.OrderId))
  18.             throw new ArgumentException("订单号无效");
  19.         
  20.         _nextHandler?.Handle(request);
  21.     }
  22. }
  23. // 风控检查处理器
  24. public class RiskCheckHandler : PaymentHandler
  25. {
  26.     public override void Handle(PaymentRequest request)
  27.     {
  28.         Console.WriteLine("[RISK] 风控验证中...");
  29.         _nextHandler?.Handle(request);
  30.     }
  31. }
  32. // 支付结果通知处理器
  33. public class NotificationHandler : PaymentHandler
  34. {
  35.     public override void Handle(PaymentRequest request)
  36.     {
  37.         Console.WriteLine($"支付结果通知:{request.OrderId}");
  38.         _nextHandler?.Handle(request);
  39.     }
  40. }
  41. // 实际支付处理器
  42. public class PaymentProcessorHandler : PaymentHandler
  43. {
  44.     public override void Handle(PaymentRequest request)
  45.     {
  46.         Console.WriteLine($"[PAYMENT] 实际支付:{request.Amount}{request.Currency}");
  47.         var strategy = PaymentFactory.Create(request.PaymentType);
  48.         PaymentContext context = new PaymentContext();
  49.         context.SetStrategy(strategy);
  50.         context.ExecutePayment(request.Amount, request.Currency);
  51.         _nextHandler?.Handle(request);
  52.     }
  53. }
  54. // 构建处理链
  55. var chain = new ValidationHandler();
  56. chain.SetNext(new RiskCheckHandler())
  57.      .SetNext(new PaymentProcessorHandler())
  58.      .SetNext(new NotificationHandler());
  59. chain.Handle(request);
复制代码
相关代码:https://github.com/huangmingji/design-pattern-learning/tree/main/design-pattern-learning-1

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册