C语言实现工厂模式

149 篇文章 2 订阅
55 篇文章 3 订阅
这篇博客展示了如何在C++中运用结构体和函数指针实现一个简单的工厂模式,用于创建不同类型的鞋子,包括皮革鞋和旅行鞋。通过定义`myShoes`结构体并设置`produce_shoes`成员函数指针,根据传入的类型动态分配内存并初始化鞋子对象。在`main`函数中,创建了一个皮革鞋对象并调用了对应的生产方法。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
typedef struct _shoes
{
    int type;
    void (*produce_shoes)(struct _shoes *);
}myShoes;
void produce_leather_shoes(myShoes *pshoes) 
{
    if (NULL != pshoes)
    {
        printf("produce the leather shoes!\n");
    }
}
void produce_travel_shoes(myShoes *pshoes) 
{
    if (NULL != pshoes)
    {
        printf("produce the travel shoes!\n");
    }
}
#define LEATHER_TYPE 0x1
#define TRAVEL_TYPE  0x2
myShoes *produce_new_shoes(int type)
{
    if (type != LEATHER_TYPE && type != TRAVEL_TYPE)
    {
        return NULL;
    }
    myShoes *pshoes = (myShoes *)malloc(sizeof(myShoes));
    if (NULL == pshoes) 
    {
        return NULL;

    }
    pshoes->type = type;
    if (LEATHER_TYPE == type) 
    {
        pshoes->produce_shoes = produce_leather_shoes;
    }
    else
    {
        pshoes->produce_shoes = produce_travel_shoes;
    }
    return pshoes;
}
int main() 
{
    myShoes *pshoes = produce_new_shoes(LEATHER_TYPE);
    if (pshoes)
    {
        pshoes->produce_shoes(pshoes);
        free(pshoes);
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
工厂模式是一种常见的设计模式,它提供了一种创建对象的方法,而无需暴露对象创建的逻辑。在 C 语言中,实现工厂模式的一种常见方式是使用函数指针。下面是一个简单的例子,演示了如何使用工厂模式创建不同类型的对象: ```c #include <stdio.h> #include <stdlib.h> // 定义一个抽象基类 typedef struct { void (*print)(void); } Shape; // 定义具体类 typedef struct { Shape shape; int width; int height; } Rectangle; typedef struct { Shape shape; int radius; } Circle; // 定义具体类的构造函数 void Rectangle_construct(Rectangle* rect, int width, int height) { rect->shape.print = (void(*)(void))Rectangle_print; rect->width = width; rect->height = height; } void Circle_construct(Circle* circle, int radius) { circle->shape.print = (void(*)(void))Circle_print; circle->radius = radius; } // 定义具体类的成员函数 void Rectangle_print(void) { printf("This is a rectangle with width=%d, height=%d\n", ((Rectangle*)this)->width, ((Rectangle*)this)->height); } void Circle_print(void) { printf("This is a circle with radius=%d\n", ((Circle*)this)->radius); } // 工厂函数,根据类型创建对象 void* Shape_create(char type, int arg1, int arg2) { switch (type) { case 'r': Rectangle* rect = (Rectangle*)malloc(sizeof(Rectangle)); Rectangle_construct(rect, arg1, arg2); return rect; case 'c': Circle* circle = (Circle*)malloc(sizeof(Circle)); Circle_construct(circle, arg1); return circle; default: return NULL; } } int main() { Shape* shape1 = (Shape*)Shape_create('r', 20, 30); Shape* shape2 = (Shape*)Shape_create('c', 10, 0); shape1->print(); shape2->print(); free(shape1); free(shape2); return 0; } ``` 在上面的代码中,我们定义了一个抽象基类 `Shape`,它有一个成员函数 `print`,用于输出对象的信息。然后,我们又定义了两个具体类 `Rectangle` 和 `Circle`,它们分别继承自 `Shape`。每个具体类都有一个构造函数和一个成员函数 `print`。 接下来,我们定义了一个工厂函数 `Shape_create`,它根据类型参数创建相应的对象。如果类型参数是 `'r'`,则创建一个 `Rectangle` 对象;如果类型参数是 `'c'`,则创建一个 `Circle` 对象。最后,我们在 `main` 函数中使用工厂函数创建了两个对象,并调用它们的 `print` 函数输出了对象的信息。注意,我们在程序结束时需要手动释放对象占用的内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值