第 4 篇:策略模式 (Strategy) —— 算法的热插拔艺术

专栏导读 :你是否遇到过这种崩溃瞬间:产品卖给 A 客户要用 Modbus 协议,卖给 B 客户要用私有协议,卖给 C 客户要加密传输。你的代码里是不是充斥着无数的 #ifdef CUSTOMER_A 或者 if (mode == 1)? 策略模式教你用 C 语言实现"多态",让算法像 USB 设备一样,插上就能用,拔掉就消失。


1. 场景还原 (The Pain)

假设你正在维护一款智能门锁。核心功能是"验证密码"。 起初,只需要简单的明文比对。后来,高配版加入了指纹(Hash 校验),海外版要求符合安全规范(CRC32 或 AES)。

菜鸟的写法:条件判断的地狱

typedef enum { AUTH_SIMPLE, AUTH_CRC32, AUTH_HASH } AuthMode;

bool Verify_Password(uint8_t* input, uint8_t* stored, int len, AuthMode mode) {

if (mode == AUTH_SIMPLE) {

// 明文比对

return (memcmp(input, stored, len) == 0);

}

else if (mode == AUTH_CRC32) {

// 还要去包含 CRC 的头文件,耦合严重

uint32_t crc_in = Cal_CRC32(input, len);

uint32_t crc_store = *(uint32_t*)stored;

return (crc_in == crc_store);

}

else if (mode == AUTH_HASH) {

// ... 又是一大坨代码 ...

}

return false;

}

架构师的审视

这种代码违反了设计模式的最高天条:开闭原则 (Open-Closed Principle) ------ 对扩展开放,对修改关闭。

  1. 耦合过重Verify_Password 函数必须认识所有的算法。如果你要加一个新的"视网膜扫描",你得修改这个经过测试的稳定函数,万一改坏了,旧功能也挂了。

  2. 代码臃肿:低配版芯片 Flash 只有 32KB,根本装不下 AES 库,但因为写在一个文件里,链接器很难剥离未用到的代码(除非编译器优化极强)。


2. 模式图解 (The Concept)

策略模式的核心是将**"做什么" (Interface)** 和 "怎么做" (Implementation) 分离。

  • Context (环境):持有算法的指针,不关心具体是哪个算法。

  • Strategy (接口):定义一套标准的函数指针结构体(V-Table)。

  • Concrete Strategy (具体策略):实现具体的算法。


3. 代码实战 (The Code)

C 语言实现多态的秘密武器:结构体 + 函数指针

3.1 定义接口 (The Interface)

这是所有算法必须遵守的契约。

// IValidator.h

#include <stdint.h>

#include <stdbool.h>

// 前置声明,类似 C++ 的 this 指针

typedef struct Validator_t Validator;

// 策略接口定义 (虚函数表)

typedef struct {

// 初始化算法

void (*init)(Validator* self);

// 执行计算

uint32_t (*calculate)(Validator* self, const uint8_t* data, uint32_t len);

// 销毁/清理 (可选)

void (*deinit)(Validator* self);

} ValidatorOps;

// 基类结构体

struct Validator_t {

const ValidatorOps* ops; // 指向虚表的指针

// 可以添加公共属性,如超时时间等

uint32_t timeout;

};

3.2 具体策略实现 (Concrete Strategies)

我们把每个算法写在独立的 .c 文件里,互不干扰。

策略 A:简单校验 (Simple)

// Validator_Simple.c

#include "IValidator.h"

static uint32_t simple_cal(Validator* self, const uint8_t* data, uint32_t len) {

uint32_t sum = 0;

for(int i=0; i<len; i++) sum += data[i];

return sum; // 简单的累加和

}

// 定义具体的虚表

static const ValidatorOps s_simple_ops = {

.init = NULL, // 不需要初始化

.calculate = simple_cal,

};

// 构造函数

void Validator_Simple_Init(Validator* v) {

v->ops = &s_simple_ops; // 核心:绑定指针

}

策略 B:CRC32 校验 (CRC32)

// Validator_CRC32.c

#include "IValidator.h"

#include "stm32_crc.h" // 依赖硬件库

static void crc_init(Validator* self) {

__HAL_RCC_CRC_CLK_ENABLE(); // 开启硬件时钟

}

static uint32_t crc_cal(Validator* self, const uint8_t* data, uint32_t len) {

return HAL_CRC_Calculate(&hcrc, (uint32_t*)data, len);

}

static const ValidatorOps s_crc32_ops = {

.init = crc_init,

.calculate = crc_cal,

};

void Validator_CRC32_Init(Validator* v) {

v->ops = &s_crc32_ops;

}

3.3 业务调用 (Context)

// main.c

#include "IValidator.h"

// 全局的校验器实例

Validator g_current_validator;

// 根据配置选择策略 (Factory 模式的雏形)

void System_Init() {

uint8_t config = Read_Flash_Config();

if (config == MODE_HIGH_END) {

Validator_CRC32_Init(&g_current_validator);

} else {

Validator_Simple_Init(&g_current_validator);

}

// 初始化选定的算法

if (g_current_validator.ops->init) {

g_current_validator.ops->init(&g_current_validator);

}

}

// 业务逻辑:完全不需要知道底层是 CRC 还是 Sum

bool Verify_Packet(uint8_t* data, int len, uint32_t expected) {

// 多态调用!

uint32_t result = g_current_validator.ops->calculate(&g_current_validator, data, len);

return (result == expected);

}

4. 内存与性能分析 (The Cost)

空间开销

  • Flash : 这种写法对 Linker 非常友好。如果你在 System_Init 里没有调用 Validator_CRC32_Init,聪明的链接器(开启 --gc-sections)会自动把 Validator_CRC32.c 的代码完全剔除。这就实现了**"用多少,链多少"**。

  • RAM : 每个策略实例只需要一个指针大小(4 字节)。虚表 ValidatorOps 声明为 const,存放在 Flash 中,不占 RAM。

时间开销

  • 间接调用 : ops->calculate(...) 是一次间接函数调用 (Indirect Call)

  • 汇编视角:

    • 直接调用:BL 0x08001234 (跳转到固定地址)

    • 间接调用:LDR R0, [R1]; BLX R0 (先读地址再跳转)

  • 性能损耗: 相比直接调用,多了 2-3 个指令周期。

  • 架构师建议 : 不要在百万次的极速循环内部使用策略模式(例如像素级渲染)。但对于通信包校验、按键处理、传感器读取这种毫秒级任务,性能损耗可以忽略不计,换来的是巨大的架构灵活性。


5. 变种与延伸 (The Evolution)

5.1 运行时动态切换 (Hot Swap)

这就是标题中"热插拔"的由来。 比如一个无线接收模块,信号好时用 Strategy_Fast (无校验,速度快),信号差时自动切换为 Strategy_Reliable (高强度校验)。 你只需要执行 v->ops = &s_reliable_ops;,业务层代码无需任何变动。

5.2 依赖注入与打桩 (Mocking for Test)

这是策略模式在单元测试 中的杀手级应用。 在 PC 上跑单元测试时,无法调用 HAL_CRC_Calculate(因为没有硬件)。 你可以写一个 Validator_Mock.c,在里面伪造数据。

// Test_Main.c

void Test_Logic() {

Validator v;

Validator_Mock_Init(&v); // 注入假对象

// 测试业务逻辑,此时 calculate 调用的是假的桩函数

Verify_Packet(..., ...);

}

相关推荐
!停1 小时前
数据结构空间复杂度
java·c语言·算法
不染尘.1 小时前
二分算法(优化)
开发语言·c++·算法
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.1 小时前
Keepalived高可用配置指南
服务器·网络·php
不吃橘子的橘猫1 小时前
Verilog HDL基础(概念+模块)
开发语言·学习·算法·fpga开发·verilog
2401_832298101 小时前
腾讯云第九代CVM,玄灵网卡加持重构算力新范式
网络
苦藤新鸡1 小时前
49.二叉树的最大路径和
数据结构·算法·深度优先
源代码•宸1 小时前
Leetcode—144. 二叉树的前序遍历【简单】
经验分享·算法·leetcode·面试·职场和发展·golang·dfs
Cloud Traveler1 小时前
Archcraft携手cpolar打造轻量化远程开发环境
网络·云原生·eureka
七夜zippoe2 小时前
网络安全实战:从TLS/SSL到JWT与OAuth2.0的完整防御体系构建
网络·安全·web安全·ssl·tls