Linux 下 C 语言实现工厂模式:设计理念与实战
-
- [🧠 一、工厂模式简介](#🧠 一、工厂模式简介)
-
- 什么是工厂模式?
- [C 语言实现设计模式的挑战](#C 语言实现设计模式的挑战)
- [🏗️ 二、实现简单工厂模式(Simple Factory)](#🏗️ 二、实现简单工厂模式(Simple Factory))
- [🧪 三、Linux 应用场景举例](#🧪 三、Linux 应用场景举例)
- [🧰 四、进阶设计:注册表式工厂(更灵活)](#🧰 四、进阶设计:注册表式工厂(更灵活))
- [🧠 五、总结](#🧠 五、总结)
- [🔗 六、参考资料](#🔗 六、参考资料)
在嵌入式开发和系统编程中,良好的架构设计 能有效提升代码的可维护性、可扩展性。虽然 C 语言不是面向对象语言,但通过结构体与函数指针等手段,我们依然可以实现经典的设计模式。本文将深入讲解如何在 Linux 环境下使用 C 语言实现工厂模式(Factory Pattern),并结合实际案例说明其优势与应用场景。
🧠 一、工厂模式简介
什么是工厂模式?
工厂模式是一种创建型设计模式,用于隐藏对象创建逻辑,将实例化过程交由工厂类负责。调用者只关心产品的"接口",不关心具体实现,从而实现解耦。
C 语言实现设计模式的挑战
- 没有类和继承
- 无多态机制
- 类型系统不如 C++ 灵活
解决办法:
- 使用
struct
模拟类 - 使用函数指针模拟方法
- 使用枚举/注册表实现工厂分发
🏗️ 二、实现简单工厂模式(Simple Factory)
我们以"不同类型的传感器"为例,定义一个统一的设备接口,由工厂创建不同的传感器对象。
1. 定义传感器接口(device.h)
c
#ifndef DEVICE_H
#define DEVICE_H
typedef struct Device {
void (*init)(void);
void (*read)(void);
void (*destroy)(struct Device* self);
} Device;
#endif
2. 各类型传感器实现(sensor_temp.c / sensor_humidity.c)
温度传感器
c
#include <stdio.h>
#include <stdlib.h>
#include "device.h"
static void temp_init() {
printf("温度传感器初始化完成\n");
}
static void temp_read() {
printf("温度传感器读取数据:25°C\n");
}
Device* create_temp_sensor() {
Device* dev = (Device*)malloc(sizeof(Device));
dev->init = temp_init;
dev->read = temp_read;
dev->destroy = free;
return dev;
}
湿度传感器
c
#include <stdio.h>
#include <stdlib.h>
#include "device.h"
static void humidity_init() {
printf("湿度传感器初始化完成\n");
}
static void humidity_read() {
printf("湿度传感器读取数据:60%%\n");
}
Device* create_humidity_sensor() {
Device* dev = (Device*)malloc(sizeof(Device));
dev->init = humidity_init;
dev->read = humidity_read;
dev->destroy = free;
return dev;
}
3. 定义工厂接口(factory.h / factory.c)
factory.h
c
#ifndef FACTORY_H
#define FACTORY_H
#include "device.h"
typedef enum {
SENSOR_TEMP,
SENSOR_HUMIDITY
} SensorType;
Device* sensor_factory_create(SensorType type);
#endif
factory.c
c
#include "factory.h"
extern Device* create_temp_sensor();
extern Device* create_humidity_sensor();
Device* sensor_factory_create(SensorType type) {
switch (type) {
case SENSOR_TEMP:
return create_temp_sensor();
case SENSOR_HUMIDITY:
return create_humidity_sensor();
default:
return NULL;
}
}
4. 使用工厂的主函数(main.c)
c
#include <stdio.h>
#include "factory.h"
int main() {
Device* sensor1 = sensor_factory_create(SENSOR_TEMP);
Device* sensor2 = sensor_factory_create(SENSOR_HUMIDITY);
if (sensor1) {
sensor1->init();
sensor1->read();
sensor1->destroy(sensor1);
}
if (sensor2) {
sensor2->init();
sensor2->read();
sensor2->destroy(sensor2);
}
return 0;
}
🧪 三、Linux 应用场景举例
用户空间:
- 日志模块封装 :使用工厂返回
file_logger
/syslog_logger
/udp_logger
- 插件加载器:通过配置动态加载插件创建函数指针并注册
内核空间类比:
- platform_driver 机制 :
of_device_id
表现类似"注册表+工厂" - probe 函数中的创建与注册过程 模拟工厂动态创建驱动实例
🧰 四、进阶设计:注册表式工厂(更灵活)
c
typedef struct {
SensorType type;
Device* (*create_func)(void);
} SensorRegistryEntry;
static SensorRegistryEntry registry[] = {
{ SENSOR_TEMP, create_temp_sensor },
{ SENSOR_HUMIDITY, create_humidity_sensor },
};
Device* sensor_factory_create(SensorType type) {
for (int i = 0; i < sizeof(registry)/sizeof(registry[0]); ++i) {
if (registry[i].type == type) {
return registry[i].create_func();
}
}
return NULL;
}
这种写法易于扩展,只需新增注册项。
🧠 五、总结
- 工厂模式是创建型设计模式的典型代表,适用于对象种类较多、结构相似的场景。
- 在 C 语言中可以通过结构体+函数指针灵活模拟面向对象思想。
- 工厂模式可以大大降低模块之间的耦合性,使系统更易于维护和扩展。
🔗 六、参考资料
- 《设计模式:可复用面向对象软件的基础》
- 《Linux 设备驱动开发详解》
- 见附件示例代码 https://download.csdn.net/download/yll7702/90919839