设计模式中工厂模式的C语言实现

在C语言中实现工厂模式(Factory Pattern)通常需要模拟面向对象的编程方式。工厂模式的核心思想是通过工厂函数来创建不同类型的对象,隐藏对象创建的细节。下面是一个简单的工厂模式在C语言中的实现。

工厂模式示例:几何形状工厂

我们将模拟一个工厂来创建不同的几何形状对象(例如:圆形、矩形、三角形),每个几何形状都有一个 draw 方法。

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义抽象的 Shape 接口
typedef struct Shape {
    void (*draw)(struct Shape*);  // 函数指针,用于表示多态的 draw 方法
} Shape;

// 定义圆形结构体
typedef struct Circle {
    Shape base;  // 基础 Shape 类型
} Circle;

// 定义矩形结构体
typedef struct Rectangle {
    Shape base;  // 基础 Shape 类型
} Rectangle;

// 定义三角形结构体
typedef struct Triangle {
    Shape base;  // 基础 Shape 类型
} Triangle;

// 实现 Circle 的 draw 方法
void drawCircle(Shape* shape) {
    printf("Drawing a Circle.\n");
}

// 实现 Rectangle 的 draw 方法
void drawRectangle(Shape* shape) {
    printf("Drawing a Rectangle.\n");
}

// 实现 Triangle 的 draw 方法
void drawTriangle(Shape* shape) {
    printf("Drawing a Triangle.\n");
}

// 工厂函数,用于创建不同类型的 Shape 对象
Shape* createShape(const char* shapeType) {
    if (strcmp(shapeType, "Circle") == 0) {
        Circle* circle = (Circle*)malloc(sizeof(Circle));
        circle->base.draw = drawCircle;  // 绑定 Circle 的 draw 方法
        return (Shape*)circle;
    } else if (strcmp(shapeType, "Rectangle") == 0) {
        Rectangle* rectangle = (Rectangle*)malloc(sizeof(Rectangle));
        rectangle->base.draw = drawRectangle;  // 绑定 Rectangle 的 draw 方法
        return (Shape*)rectangle;
    } else if (strcmp(shapeType, "Triangle") == 0) {
        Triangle* triangle = (Triangle*)malloc(sizeof(Triangle));
        triangle->base.draw = drawTriangle;  // 绑定 Triangle 的 draw 方法
        return (Shape*)triangle;
    }
    return NULL;
}

// 释放内存
void destroyShape(Shape* shape) {
    if (shape != NULL) {
        free(shape);
    }
}

int main() {
    // 创建 Circle 对象
    Shape* circle = createShape("Circle");
    if (circle != NULL) {
        circle->draw(circle);  // 输出: Drawing a Circle.
        destroyShape(circle);
    }

    // 创建 Rectangle 对象
    Shape* rectangle = createShape("Rectangle");
    if (rectangle != NULL) {
        rectangle->draw(rectangle);  // 输出: Drawing a Rectangle.
        destroyShape(rectangle);
    }

    // 创建 Triangle 对象
    Shape* triangle = createShape("Triangle");
    if (triangle != NULL) {
        triangle->draw(triangle);  // 输出: Drawing a Triangle.
        destroyShape(triangle);
    }

    return 0;
}

代码说明:

  1. 接口模拟Shape 结构体中包含一个函数指针 draw,用于模拟面向对象语言中的接口及多态行为。

  2. 具体类CircleRectangleTriangle 结构体都继承自 Shape(通过包含 Shape 结构体),每种形状都有自己具体的 draw 方法。

  3. 工厂函数createShape 函数通过传入的形状类型字符串(如 "Circle"、"Rectangle" 等)动态创建相应的对象,并返回 Shape* 类型的指针。

  4. 多态调用 :在主函数中,通过调用 draw 函数指针,能够实现对具体对象的多态调用。

  5. 内存管理:工厂函数动态分配内存,因此在使用完对象后需要手动释放内存,避免内存泄漏。

这个设计模式在C语言中通过函数指针和结构体组合的方式来实现了类似面向对象的多态行为。

相关推荐
困鲲鲲11 分钟前
设计模式:状态模式 State
设计模式·状态模式
蝸牛ちゃん3 小时前
设计模式(十一)结构型:外观模式详解
设计模式·系统架构·软考高级·外观模式
weixin_470880263 小时前
设计模式实战:自定义SpringIOC(亲手实践)
设计模式·面试·个人提升·springioc·设计模式实战·spring框架原理
阳光明媚sunny4 小时前
创建型设计模式-工厂方法模式和抽象工厂方法模式
设计模式·工厂方法模式
困鲲鲲6 小时前
设计模式:代理模式 Proxy
设计模式·代理模式
就是帅我不改8 小时前
深入实战工厂模式与观察者模式:工厂模式与观察者模式在电商系统中的应用
后端·设计模式
云中飞鸿14 小时前
结合项目阐述 设计模式:单例、工厂、观察者、代理
设计模式
蝸牛ちゃん20 小时前
设计模式(二十四)行为型:访问者模式详解
设计模式·系统架构·软考高级·访问者模式
zy小狮子20 小时前
【设计模式系列】策略模式vs模板模式
设计模式·策略模式
找不到、了21 小时前
Java设计模式之<装饰器模式>
java·设计模式·装饰器模式