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

news/2025/3/5 10:19:05/文章来源:https://www.cnblogs.com/huangmingji/p/18751831

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

场景需求

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

模式实现

  1. 定义支付策略接口
public interface IPaymentStrategy 
{void ProcessPayment(decimal amount, string currency);
}
  1. 实现具体支付策略类
// 支付宝策略
public class AlipayStrategy : IPaymentStrategy
{public void ProcessPayment(decimal amount, string currency){Console.WriteLine($"支付宝支付:{amount}{currency}");// 调用支付宝SDK实现}
}// 微信支付策略(实现类似)
public class WechatPayStrategy : IPaymentStrategy { /*...*/ }
  1. 创建支付上下文类
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($"[LOG] 支付请求:{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("[RISK] 风控验证中...");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 void SetNext(PaymentHandler handler) => _nextHandler = handler;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);

五、综合应用示例

  1. 定义支付策略接口
public interface IPaymentStrategy 
{void ProcessPayment(decimal amount, string currency);
}
  1. 实现具体支付策略类
// 支付宝策略
public class AlipayStrategy : IPaymentStrategy
{public void ProcessPayment(decimal amount, string currency){Console.WriteLine($"支付宝支付:{amount}{currency}");// 调用支付宝SDK实现}
}// 微信支付策略(实现类似)
public class WechatPayStrategy : IPaymentStrategy { /*...*/ }// 银联支付策略(实现类似)
public class UnionPayStrategy : IPaymentStrategy { /*...*/ }
  1. 创建支付请求类
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("不支持的支付方式");}}
}
  1. 创建支付装饰类
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($"[LOG] 支付请求:{amount}{currency}");base.ProcessPayment(amount, currency);}
}
  1. 创建支付上下文类
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);}
}
  1. 创建支付处理链类
public abstract class PaymentHandler
{protected PaymentHandler _nextHandler;public void SetNext(PaymentHandler handler) => _nextHandler = handler;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("[RISK] 风控验证中...");_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($"[PAYMENT] 实际支付:{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);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/893901.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI制作

首先,我们需要设计数据库表结构。根据需求,我们需要两个表:仓库表和物资台账明细表。1.1 仓库表 (warehouse) 字段名 数据类型 描述 warehouse_id VARCHAR(10) 仓库编号(唯一) name VARCHAR(50) 仓库名称 location VARCHAR(100) 仓库位置 capacity INT 仓库容量 created_a…

为应用程序分配单个Sharepoint站点的权限

最近工作需要在Sharepoint Online上建立一个站点用于信息同步。另一个租户下有一个站点的信息将需要同步到这个新站点中。 为此,我们需要新建一个应用程序用于数据同步。这个可以在Microsoft Entra admin center中完成。找到Applications--App registrations在这里新建一个应用…

花3分钟来了解一下Vue3中的插槽到底是什么玩意

前言 插槽看着是一个比较神秘的东西,特别是作用域插槽还能让我们在父组件里面直接访问子组件里面的数据,这让插槽变得更加神秘了。其实Vue3的插槽远比你想象的简单,这篇文章我们来揭开插槽的神秘面纱。 欧阳也在找工作,坐标成都求内推! 看个demo 我们先来看个常见的插槽de…

OKR 推行五大注意事项

OKR(目标与关键成果法)是一种高效的目标管理工具,但在推行过程中往往会面临诸多挑战。以下是OKR推行的五大难点,以及在推行过程中需要注意的五个关键事项,并结合Tita平台的特点进行简要介绍。OKR推行的五大难点目标设定不合理确定清晰、合理且可衡量的目标和关键成果是推行…

注册用户同步没有了

解决: https://vip.kingdee.com/knowledge/642363720735104256?productLineId=1&isKnowledge=2&lang=zh-CN

同步注册用户没有了

解决: https://vip.kingdee.com/knowledge/642363720735104256?productLineId=1&isKnowledge=2&lang=zh-CN

2025.03.04 CW 模拟赛 D. 积木

D. 积木 和之前容斥专题的一道题有点像. 思路 注意到虽然 \(1 \le n, m \le 50\), 但是 * 的个数不超过 12 个. 于是我们可以考虑对 * 的个数进行状压, 也就是钦定哪些 * 必须作为积木的中心 \((\)下文统称为 o\()\). 钦定完成, 我们考虑什么情况下是不合法的. 如下图, 这两种情…

9.0版本要求使用https登录管理中心

解决:如下图 https://vip.kingdee.com/knowledge/552811796516494592?productLineId=1&isKnowledge=2&lang=zh-CN

西门子 smart 700 人机界面 HMI erwa.cn二娃备忘

情况是这样,触摸屏型号为smart 700IE,以前的程序已经确定丢失了,已经是两年前其他人搞的程序了。目前想要将此smart 700IE触摸屏程序得出来,该怎么实现,请详细告知,非常感谢 最佳答案 Smart Panels 均不支持“回传”功能,只支持“备份”“恢复”功能。 那“回传”与“…

文件同步备份软件,让文件同步更安全更稳定!

在数字化时代,数据已然成为企业的核心资产,而确保服务器之间文件的高效同步,是保障数据一致性、维持业务连续性的关键所在。无论是大型企业构建多数据中心的复杂架构,还是分布式系统力求各节点数据的统一,文件同步备份软件都发挥着无可替代的作用。它就像是企业数据流转的…

无法加载kingdee k3 cloud erp V7.2的许可文件,许可文件可能被篡改,请联系管理员

原因:如下图,正式补丁在安装时出现了异常导致。 解决:重新安装正式补丁以及临时补丁。

2025年最火爆的5款跨网文件安全交换系统优缺点对比

跨网文件安全交换系统主要用于在不同网络环境间安全传输文件,确保数据的机密性、完整性和可用性。 能解决跨网文件传输的方式有很多,比如FTP/FTPS、SFTP、云存储服务、P2P文件共享以及Ftrans Ferry跨网文件安全交换系统各有优缺点。以下是它们的详细对比: 一、FTP/FTPS优点:…