C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > Linux下C语言实现工厂模式

Linux下C语言实现工厂模式方式

作者:隐身模式

这篇文章主要介绍了Linux下C语言实现工厂模式方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

在嵌入式开发和系统编程中,良好的架构设计 能有效提升代码的可维护性、可扩展性。虽然 C 语言不是面向对象语言,但通过结构体与函数指针等手段,我们依然可以实现经典的设计模式。

本文将深入讲解如何在 Linux 环境下使用 C 语言实现工厂模式(Factory Pattern),并结合实际案例说明其优势与应用场景。

一、工厂模式简介

什么是工厂模式?

工厂模式是一种创建型设计模式,用于隐藏对象创建逻辑,将实例化过程交由工厂类负责。

调用者只关心产品的“接口”,不关心具体实现,从而实现解耦。

C 语言实现设计模式的挑战

解决办法:

二、实现简单工厂模式(Simple Factory)

我们以“不同类型的传感器”为例,定义一个统一的设备接口,由工厂创建不同的传感器对象。

定义传感器接口(device.h)

#ifndef DEVICE_H
#define DEVICE_H

typedef struct Device {
    void (*init)(void);
    void (*read)(void);
    void (*destroy)(struct Device* self);
} Device;

#endif

各类型传感器实现(sensor_temp.c / sensor_humidity.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;
}
#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;
}

定义工厂接口(factory.h / factory.c)

#ifndef FACTORY_H
#define FACTORY_H

#include "device.h"

typedef enum {
    SENSOR_TEMP,
    SENSOR_HUMIDITY
} SensorType;

Device* sensor_factory_create(SensorType type);

#endif
#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;
    }
}

使用工厂的主函数(main.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 应用场景举例

用户空间

内核空间类比

四、进阶设计:注册表式工厂(更灵活)

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;
}

这种写法易于扩展,只需新增注册项。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文