C++ 设计模式-策略模式

支付策略

#include <iostream>
#include <memory>
#include <unordered_map>
#include <vector>
#include <ctime>// 基础策略接口
class PaymentStrategy {
public:virtual ~PaymentStrategy() = default;virtual std::string name() const = 0;virtual bool validate(double amount, const std::string& currency) const = 0;virtual void pay(double amount, const std::string& currency) const = 0;virtual double calculate_fee(double amount) const = 0;
};// 策略工厂系统
class StrategyFactory {
public:using StrategyCreator = std::function<std::unique_ptr<PaymentStrategy>()>;static StrategyFactory& instance() {static StrategyFactory instance;return instance;}void register_strategy(const std::string& id, StrategyCreator creator) {creators_[id] = creator;}std::unique_ptr<PaymentStrategy> create(const std::string& id) const {if (auto it = creators_.find(id); it != creators_.end()) {return it->second();}return nullptr;}std::vector<std::string> available_strategies() const {std::vector<std::string> names;for (const auto& [id, _] : creators_) {names.push_back(id);}return names;}private:std::unordered_map<std::string, StrategyCreator> creators_;
};// 自动注册宏
#define REGISTER_PAYMENT_STRATEGY(StrategyClass, strategy_id) \namespace { \struct AutoRegister_##StrategyClass { \AutoRegister_##StrategyClass() { \StrategyFactory::instance().register_strategy( \strategy_id, \[]{ return std::make_unique<StrategyClass>(); } \); \} \}; \AutoRegister_##StrategyClass auto_reg_##StrategyClass; \}// 微信支付策略
class WechatPayStrategy : public PaymentStrategy {const double max_amount_ = 50000.0; // 单笔最大金额public:std::string name() const override { return "WeChat Pay"; }bool validate(double amount, const std::string& currency) const override {return currency == "CNY" && amount <= max_amount_;}void pay(double amount, const std::string& currency) const override {std::cout << "微信支付成功\n"<< "金额: ¥" << amount << "\n"<< "请在小程序确认支付" << std::endl;}double calculate_fee(double amount) const override {return amount * 0.001; // 0.1%手续费}
};
REGISTER_PAYMENT_STRATEGY(WechatPayStrategy, "wechat_pay");// PayPal策略
class PayPalStrategy : public PaymentStrategy {const std::vector<std::string> supported_currencies_{"USD", "EUR", "GBP"};public:std::string name() const override { return "PayPal"; }bool validate(double amount, const std::string& currency) const override {return std::find(supported_currencies_.begin(), supported_currencies_.end(), currency) != supported_currencies_.end();}void pay(double amount, const std::string& currency) const override {std::cout << "Processing PayPal payment\n"<< "Amount: " << currency << " " << amount << "\n"<< "Redirecting to PayPal login..." << std::endl;}double calculate_fee(double amount) const override {return std::max(0.3, amount * 0.05); // 5% + $0.3}
};
REGISTER_PAYMENT_STRATEGY(PayPalStrategy, "paypal");// 比特币策略(带实时汇率)
class BitcoinStrategy : public PaymentStrategy {// 模拟实时汇率获取double get_bitcoin_price() const {static const double BASE_PRICE = 45000.0; // 基础价格// 模拟价格波动return BASE_PRICE * (1.0 + 0.1 * sin(time(nullptr) % 3600));}public:std::string name() const override { return "Bitcoin"; }bool validate(double amount, const std::string& currency) const override {return currency == "BTC" || currency == "USD";}void pay(double amount, const std::string& currency) const override {if (currency == "USD") {double btc_amount = amount / get_bitcoin_price();std::cout << "Converting USD to BTC: " << "₿" << btc_amount << std::endl;amount = btc_amount;}std::cout << "区块链交易确认中...\n"<< "转账金额: ₿" << amount << "\n"<< "预计确认时间: 10分钟" << std::endl;}double calculate_fee(double amount) const override {return 0.0001 * get_bitcoin_price(); // 固定矿工费}
};
REGISTER_PAYMENT_STRATEGY(BitcoinStrategy, "bitcoin");// 支付处理器
class PaymentProcessor {std::unordered_map<std::string, std::unique_ptr<PaymentStrategy>> strategies_;public:void load_strategy(const std::string& id) {if (auto strategy = StrategyFactory::instance().create(id)) {strategies_[id] = std::move(strategy);}}void process_payment(const std::string& currency, double amount) {auto strategy = select_strategy(currency, amount);if (!strategy) {throw std::runtime_error("No available payment method");}std::cout << "\n=== 支付方式: " << strategy->name() << " ==="<< "\n金额: " << currency << " " << amount<< "\n手续费: " << strategy->calculate_fee(amount)<< "\n-------------------------" << std::endl;strategy->pay(amount, currency);}private:PaymentStrategy* select_strategy(const std::string& currency, double amount) {// 选择优先级:本地支付 > 国际支付 > 加密货币for (auto& [id, strategy] : strategies_) {if (id == "wechat_pay" && strategy->validate(amount, currency)) {return strategy.get();}}for (auto& [id, strategy] : strategies_) {if (id == "alipay" && strategy->validate(amount, currency)) {return strategy.get();}}for (auto& [id, strategy] : strategies_) {if (strategy->validate(amount, currency)) {return strategy.get();}}return nullptr;}
};// 使用示例
int main() {PaymentProcessor processor;// 加载所有注册的支付方式for (const auto& id : StrategyFactory::instance().available_strategies()) {processor.load_strategy(id);}// 人民币支付try {processor.process_payment("CNY", 200.0);processor.process_payment("CNY", 60000.0); // 应触发异常} catch (const std::exception& e) {std::cerr << "支付失败: " << e.what() << std::endl;}// 美元支付processor.process_payment("USD", 500.0);// 比特币支付processor.process_payment("BTC", 0.1);processor.process_payment("USD", 1000.0); // 自动转换为BTCreturn 0;
}

代码解析

  1. 策略扩展机制
REGISTER_PAYMENT_STRATEGY(WechatPayStrategy, "wechat_pay");
  • 自动注册:通过宏实现新策略的零配置接入
  • 唯一标识:每个策略有唯一的注册ID(如wechat_pay)
  1. 微信支付实现
class WechatPayStrategy : public PaymentStrategy {bool validate(...) { /* 校验人民币 */ }void pay(...) { /* 微信特有流程 */ }
};
  • 本地化支持:仅接受人民币
  • 移动支付流程:模拟小程序支付场景
  1. PayPal国际支付
class PayPalStrategy : public PaymentStrategy {bool validate(...) { /* 支持多币种 */ }void pay(...) { /* 跳转PayPal */ }
};
  • 多币种支持:USD/EUR/GBP
  • 典型手续费模型:固定费用+百分比
  1. 比特币支付
class BitcoinStrategy : public PaymentStrategy {double get_bitcoin_price() { /* 模拟实时价格 */ }void pay(...) { /* 自动转换法币 */ }
};
  • 加密货币支持:直接接受BTC或自动转换USD
  • 动态手续费:基于当前币价计算矿工费
  1. 智能策略选择
PaymentStrategy* select_strategy(...) {// 优先选择本地支付方式// 次选国际支付// 最后考虑加密货币
}
  • 业务优先级:体现支付方式选择策略
  • 动态路由:根据金额和币种自动路由

执行结果示例

=== 支付方式: WeChat Pay ===
金额: CNY 200
手续费: 0.2
-------------------------
微信支付成功
金额: ¥200
请在小程序确认支付支付失败: No available payment method=== 支付方式: PayPal ===
金额: USD 500
手续费: 25.3
-------------------------
Processing PayPal payment
Amount: USD 500
Redirecting to PayPal login...=== 支付方式: Bitcoin ===
金额: BTC 0.1
手续费: 4.5
-------------------------
区块链交易确认中...
转账金额: ₿0.1
预计确认时间: 10分钟=== 支付方式: Bitcoin ===
金额: USD 1000
手续费: 4.5
-------------------------
Converting USD to BTC: ₿0.0221132
区块链交易确认中...
转账金额: ₿0.0221132
预计确认时间: 10分钟

待扩展

  1. 新增策略步骤

    • 继承PaymentStrategy实现新类
    • 实现所有纯虚函数
    • 使用REGISTER_PAYMENT_STRATEGY注册
  2. 动态配置

    // 示例:从JSON加载策略配置
    void load_config(const json& config) {for (auto& item : config["strategies"]) {auto strategy = factory.create(item["id"]);strategy->configure(item["params"]);add_strategy(std::move(strategy));}
    }
    
  3. 混合支付

    class SplitPaymentStrategy : public PaymentStrategy {// 支持多个策略分摊支付void pay(...) override {credit_card_->pay(part1, currency);crypto_->pay(part2, currency);}
    };

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

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

相关文章

国产编辑器EverEdit - 如何在EverEdit中管理工程?

1 工程管理 1.1 应用场景 用户创建工程后&#xff0c;会涉及到工程的管理 &#xff0c;比如&#xff1a;打开工程、关闭工程等 1.2 使用方法 1.2.1 打开工程 单击主菜单工程 -> 打开工程&#xff0c;会弹出打开对话框&#xff0c;用户在对话框中选择需要打开的工程文件即…

MYSQL-数据库-DDL-DML-DQL-DCL-基础学习

MySql概念&#xff1a; 建立在关系模型基础上&#xff0c;有多张相互连接的二维表组成的数据库 SQL通用语法&#xff1a; 1.SQL语句可以单行或多行书写&#xff0c;以分号结尾 2.SQL语句可以使用空格/缩进来增强语句的可读性 3.MySQL数据库的SQL语句不区分大小写&#xff0c;关…

SpringBoot核心框架之AOP详解

SpringBoot核心框架之AOP详解 一、AOP基础 1.1 AOP概述 AOP&#xff1a;Aspect Oriented Programming&#xff08;面向切面编程&#xff0c;面向方面编程&#xff09;&#xff0c;其实就是面向特定方法编程。 场景&#xff1a;项目部分功能运行较慢&#xff0c;定位执行耗时…

【RK3588嵌入式图形编程】-SDL2-构建模块化UI

构建模块化UI 文章目录 构建模块化UI1、概述2、创建UI管理器3、嵌套组件4、继承5、多态子组件6、总结在本文中,将介绍如何使用C++和SDL创建一个灵活且可扩展的UI系统,重点关注组件层次结构和多态性。 1、概述 在前面的文章中,我们介绍了应用程序循环和事件循环,这为我们的…

第四届图像、信号处理与模式识别国际学术会议(ISPP 2025)

重要信息 会议官网&#xff1a;www.icispp.com 会议时间&#xff1a;2025年3月28-30日 会议地点&#xff1a;南京 简介 由河海大学和江苏大学联合主办的第四届图像、信号处理与模式识别国际学术会议&#xff08;ISPP 2025) 将于2025年3月28日-30日在中国南京举行。会议主…

低代码与开发框架的一些整合[2]

1.分析的项目资源说明 经过近期的的不断分析与运行对比&#xff0c;最终把注意力集中在了以下几个框架&#xff1a; 01.dibootdiboot.diboot: 写的更少, 性能更好 -> 为开发人员打造的低代码开发平台。Mybatis-plus关联查询&#xff0c;关联无SQL&#xff0c;性能高10倍&a…

Spring Boot 中事务的用法详解

引言 在 Spring Boot 中&#xff0c;事务管理是一个非常重要的功能&#xff0c;尤其是在涉及数据库操作的业务场景中。Spring 提供了强大的事务管理支持&#xff0c;能够帮助我们简化事务的管理和控制。本文将详细介绍 Spring Boot 中事务的用法&#xff0c;包括事务的基本概…

Java面试——Tomcat

优质博文&#xff1a;IT_BLOG_CN 一、Tomcat 顶层架构 Tomcat中最顶层的容器是Server&#xff0c;代表着整个服务器&#xff0c;从上图中可以看出&#xff0c;一个Server可以包含至少一个Service&#xff0c;用于具体提供服务。Service主要包含两个部分&#xff1a;Connector和…

第4章 信息系统架构(三)

4.3 应用架构 应用架构的主要内容是规划出目标应用分层分域架构&#xff0c;根据业务架构规划目标应用域、应用组和目标应用组件&#xff0c;形成目标应用架构逻辑视图和系统视图。从功能视角出发&#xff0c;阐述应用组件各自及应用架构整体上&#xff0c;如何实现组织的高阶…

python小项目编程-中级(1、图像处理)

目录 图像处理 实现 测试 unittest pytest 图像处理 实现界面化操作&#xff0c;使用PIL库实现简单的图像处理功能&#xff0c;如缩放&#xff08;设置缩放比例&#xff09;、旋转和滤镜、对比度调整、亮度调整、灰度图、二值化图&#xff08;二值图如果使用的是彩色图片需…

【Leetcode 每日一题】2209. 用地毯覆盖后的最少白色砖块

问题背景 给你一个下标从 0 0 0 开始的 二进制 字符串 f l o o r floor floor&#xff0c;它表示地板上砖块的颜色。 f l o o r [ i ] floor[i] floor[i] 为 ‘0’ 表示地板上第 i i i 块砖块的颜色是 黑色 。 f l o o r [ i ] floor[i] floor[i] 为’1’ 表示地板上第 i …

Docker 性能优化指南

Docker 提供了强大的容器化功能&#xff0c;能够帮助开发者在不同的环境中构建、测试和部署应用。然而&#xff0c;随着容器化应用的不断增长&#xff0c;Docker 容器可能会面临一些性能瓶颈&#xff0c;影响其运行效率、资源占用和扩展能力。为了确保容器在生产环境中的高效运…

2025 WE DAY品牌日| 天璇II WE X7 Pro充电桩震撼发布,能效电气开启充电革命

随着新能源产业的迅猛发展,充电桩作为电动汽车能量补给的重要基础设施,正在成为市场关注的焦点。能效电气作为充电桩领域的佼佼者,专注于研发高效、智能的充电解决方案,为电动汽车的普及与可持续发展铺设了坚实的基础。 2025年2月21日,能效电气在深圳盛大举办了以“以创新 引未…

< OS 有关 > Ubuntu 24 SSH 服务器更换端口 in jp/us VPSs

原因&#xff1a; 两台 VPS 的 ssh 端口一直被密码重试&#xff0c; us 这台已经封了 632, jp 这台两周前清过一次 sqlite3 数据&#xff0c;现在赞到 1008 Fail2Ban 是使用 sqlite3 来记录&#xff0c;数据量大后&#xff0c;硬盘的 I/O 会飙升&#xff0c;我有写过一个 app…

MATLAB学习之旅:数据插值与曲线拟合

在MATLAB的奇妙世界里,我们已经走过了一段又一段的学习旅程。从基础的语法和数据处理,到如今,我们即将踏入数据插值与曲线拟合这片充满魅力的领域。这个领域就像是魔法中的艺术创作,能够让我们根据现有的数据点,构建出更加丰富的曲线和曲面,从而更好地理解和描述数据背后…

若依-@Excel新增注解numberFormat

Excel注解中原本的scale会四舍五入小数&#xff0c;导致进度丢失 想要的效果 显示的时候保留两个小数真正的数值是保留之前的数值 还原过程 若以中有一個專門的工具类&#xff0c;用来处理excel的 找到EXCEL导出方法exportExcel()找到writeSheet,写表格的方法找到填充数据的方法…

LeetCode 热题 100_搜索二维矩阵(64_74_中等_C++)(二分查找)(暴力破解法;Z字形查找;一次二分查找)

LeetCode 热题 100_搜索二维矩阵&#xff08;64_74&#xff09; 题目描述&#xff1a;输入输出样例&#xff1a;题解&#xff1a;解题思路&#xff1a;思路一&#xff08;暴力破解法&#xff09;&#xff1a;思路二&#xff08;Z字形查找&#xff09;&#xff1a;思路三&#x…

从CNN到Transformer:遥感影像目标检测的技术演进(矿产勘探、精准农业、城市规划、林业测量、军事目标识别和灾害评估等)

在遥感影像分析领域&#xff0c;目标检测一直是研究热点之一。随着高分辨率对地观测系统的不断发展&#xff0c;遥感影像的分辨率和数据量呈爆发式增长&#xff0c;如何高效、准确地从海量数据中提取有用信息&#xff0c;成为了一个亟待解决的问题。近年来&#xff0c;深度学习…

【rt-thread】rt-thread 控制 led 的两种方式

1. pin设备 #define LED_PIN 3int led(void) {rt_uint8_t count;rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT); for(count 0 ; count < 10 ;count){ rt_pin_write(LED_PIN, PIN_HIGH);rt_kprintf("led on, count : %d %d\r\n", count, rt_pin_read(LED_PIN));…

Excell 代码处理

文章目录 Excell 代码处理cvc格式xlsl格式小结 Excell 代码处理 有时候要对excell进行分析&#xff0c;或者数据的导入导出&#xff0c;这个时候如果可以用代码读写分析操作那么会方便很多 cvc格式 CSV&#xff08;Comma-Separated Values&#xff0c;逗号分隔值&#xff09;是…