薛小春 发表于 2025-6-1 21:27:27

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

一、策略模式:灵活切换支付方式

场景需求

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


[*]定义支付策略接口
public interface IPaymentStrategy
{
    void ProcessPayment(decimal amount, string currency);
}
[*]实现具体支付策略类
// 支付宝策略
public class AlipayStrategy : IPaymentStrategy
{
    public void ProcessPayment(decimal amount, string currency)
    {
      Console.WriteLine($"支付宝支付:{amount}{currency}");
      // 调用支付宝SDK实现
    }
}

// 微信支付策略(实现类似)
public class WechatPayStrategy : IPaymentStrategy { /*...*/ }
[*]创建支付上下文类
public class PaymentContext
{
    private IPaymentStrategy _strategy;

    public void SetStrategy(IPaymentStrategy strategy) => _strategy = strategy;
   
    public void ExecutePayment(decimal amount, string currency)
    {
      _strategy.ProcessPayment(amount, currency);
    }
}二、装饰器模式:动态扩展支付功能

场景需求

为支付流程添加日志记录、风控验证等附加功能。
模式实现

public abstract class PaymentDecorator : IPaymentStrategy
{
    protected IPaymentStrategy _strategy;

    public PaymentDecorator(IPaymentStrategy strategy)
    {
      _strategy = strategy;
    }

    public virtual void ProcessPayment(decimal amount, string currency)
    {
      _strategy.ProcessPayment(amount, currency);
    }
}

// 日志装饰器
public class LoggingDecorator : PaymentDecorator
{
    public LoggingDecorator(IPaymentStrategy strategy) : base(strategy) {}

    public override void ProcessPayment(decimal amount, string currency)
    {
      Console.WriteLine($" 支付请求:{amount}{currency}");
      base.ProcessPayment(amount, currency);
    }
}

public class RiskCheckDecorator : PaymentDecorator
{
    public RiskCheckDecorator(IPaymentStrategy strategy) : base(strategy) {}
    public override void ProcessPayment(decimal amount, string currency)
    {
      Console.WriteLine(" 风控验证中...");
      base.ProcessPayment(amount, currency);
    }
}

// 使用示例
var payment = new LoggingDecorator(new RiskCheckDecorator(new AlipayStrategy()));
payment.ProcessPayment(100, "CNY");三、工厂模式:统一支付对象创建

场景需求

根据配置参数动态创建支付策略实例。
模式实现

public class PaymentFactory
{
    public static IPaymentStrategy Create(string paymentType)
    {
      switch (paymentType.ToLower())
      {
            case "alipay":
                return new AlipayStrategy();
            case "wechat":
                return new WechatPayStrategy();
            case "unionpay":
                return new UnionPayStrategy();
            default:
                throw new ArgumentException("不支持的支付方式");
      }
    }
}

// 调用示例
var strategy = PaymentFactory.Create("alipay");
strategy.ProcessPayment(200, "USD");四、责任链模式:支付流程处理

场景需求

处理支付请求时需要依次执行:参数校验 → 风控检查 → 实际支付 → 结果通知。
模式实现

public abstract class PaymentHandler
{
    protected PaymentHandler _nextHandler;
   
    public PaymentHandler SetNext(PaymentHandler handler)
    {
      _nextHandler = handler;
      return _nextHandler;
    }

    public abstract void Handle(PaymentRequest request);
}

// 参数校验处理器
public class ValidationHandler : PaymentHandler
{
    public override void Handle(PaymentRequest request)
    {
      if (string.IsNullOrEmpty(request.OrderId))
            throw new ArgumentException("订单号无效");
      
      _nextHandler?.Handle(request);
    }
}

// 构建处理链
var chain = new ValidationHandler();
chain.SetNext(new RiskCheckHandler())
   .SetNext(new PaymentProcessorHandler())
   .SetNext(new NotificationHandler());
chain.Handle(request);五、综合应用示例


[*]定义支付策略接口
public interface IPaymentStrategy
{
    void ProcessPayment(decimal amount, string currency);
}
[*]实现具体支付策略类
// 支付宝策略
public class AlipayStrategy : IPaymentStrategy
{
    public void ProcessPayment(decimal amount, string currency)
    {
      Console.WriteLine($"支付宝支付:{amount}{currency}");
      // 调用支付宝SDK实现
    }
}

// 微信支付策略(实现类似)
public class WechatPayStrategy : IPaymentStrategy { /*...*/ }// 银联支付策略(实现类似)public class UnionPayStrategy : IPaymentStrategy { /*...*/ }
[*]创建支付请求类
public class PaymentFactory
{
    public static IPaymentStrategy Create(string paymentType)
    {
      switch (paymentType.ToLower())
      {
            case "alipay":
                return new AlipayStrategy();
            case "wechat":
                return new WechatPayStrategy();
            case "unionpay":
                return new UnionPayStrategy();
            default:
                throw new ArgumentException("不支持的支付方式");
      }
    }
}
[*]创建支付装饰类
public abstract class PaymentDecorator : IPaymentStrategy
{
    protected IPaymentStrategy _strategy;

    public PaymentDecorator(IPaymentStrategy strategy)
    {
      _strategy = strategy;
    }

    public virtual void ProcessPayment(decimal amount, string currency)
    {
      _strategy.ProcessPayment(amount, currency);
    }
}

// 日志装饰器
public class LoggingDecorator : PaymentDecorator
{
    public LoggingDecorator(IPaymentStrategy strategy) : base(strategy) {}

    public override void ProcessPayment(decimal amount, string currency)
    {
      Console.WriteLine($" 支付请求:{amount}{currency}");
      base.ProcessPayment(amount, currency);
    }
}
[*]创建支付上下文类
public class PaymentContext
{
    private IPaymentStrategy _strategy;

    public void SetStrategy(IPaymentStrategy strategy) => _strategy = strategy;
   
    public void ExecutePayment(decimal amount, string currency)
    {
      new LoggingDecorator(_strategy).ProcessPayment(amount, currency);
    }
}
[*]创建支付处理链类
public abstract class PaymentHandler
{
    protected PaymentHandler _nextHandler;
   
    public PaymentHandler SetNext(PaymentHandler handler)
    {
      _nextHandler = handler;
      return _nextHandler;
    }

    public abstract void Handle(PaymentRequest request);
}

// 参数校验处理器
public class ValidationHandler : PaymentHandler
{
    public override void Handle(PaymentRequest request)
    {
      if (string.IsNullOrEmpty(request.OrderId))
            throw new ArgumentException("订单号无效");
      
      _nextHandler?.Handle(request);
    }
}

// 风控检查处理器
public class RiskCheckHandler : PaymentHandler
{
    public override void Handle(PaymentRequest request)
    {
      Console.WriteLine(" 风控验证中...");
      _nextHandler?.Handle(request);
    }
}

// 支付结果通知处理器
public class NotificationHandler : PaymentHandler
{
    public override void Handle(PaymentRequest request)
    {
      Console.WriteLine($"支付结果通知:{request.OrderId}");
      _nextHandler?.Handle(request);
    }
}

// 实际支付处理器
public class PaymentProcessorHandler : PaymentHandler
{
    public override void Handle(PaymentRequest request)
    {
      Console.WriteLine($" 实际支付:{request.Amount}{request.Currency}");
      var strategy = PaymentFactory.Create(request.PaymentType);
      PaymentContext context = new PaymentContext();
      context.SetStrategy(strategy);
      context.ExecutePayment(request.Amount, request.Currency);
      _nextHandler?.Handle(request);
    }
}

// 构建处理链
var chain = new ValidationHandler();
chain.SetNext(new RiskCheckHandler())
   .SetNext(new PaymentProcessorHandler())
   .SetNext(new NotificationHandler());
chain.Handle(request);相关代码:https://github.com/huangmingji/design-pattern-learning/tree/main/design-pattern-learning-1

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

连热 发表于 2025-10-21 21:45:15

感谢分享

郜庄静 发表于 2025-12-7 00:09:29

感谢分享

赙浦 发表于 2025-12-30 00:43:37

谢谢楼主提供!

育局糊 发表于 2026-1-15 01:26:45

这个有用。

嶝扁 发表于 2026-1-19 12:31:37

鼓励转贴优秀软件安全工具和文档!

仰翡邸 发表于 2026-1-27 04:32:12

感谢发布原创作品,程序园因你更精彩

接快背 发表于 2026-1-27 05:24:17

热心回复!

院儿饯 发表于 2026-1-28 05:13:58

这个有用。

峰埋姚 发表于 2026-2-8 00:23:11

这个有用。

荆邦 发表于 2026-2-8 12:18:45

新版吗?好像是停更了吧。

亢安芙 发表于 2026-2-9 07:17:05

谢谢分享,辛苦了

揭荸 发表于 2026-2-9 08:37:02

不错,里面软件多更新就更好了

叭遭段 发表于 2026-2-11 02:46:59

过来提前占个楼

孙淼淼 发表于 2026-2-12 15:53:36

这个有用。

姜删懔 发表于 2026-2-13 13:32:04

感谢分享,下载保存了,貌似很强大

锄淫鲷 发表于 2026-2-13 22:01:19

鼓励转贴优秀软件安全工具和文档!

焦尔蕾 发表于 2026-2-21 18:15:08

谢谢楼主提供!

丁若云 发表于 2026-2-23 14:30:52

收藏一下   不知道什么时候能用到

寨重 发表于 2026-3-9 17:30:42

感谢发布原创作品,程序园因你更精彩
页: [1] 2
查看完整版本: 设计模式学习:在支付系统中的实战应用