使用 ANSI C 实现工厂模式

发布于 2024-09-08 12:16:29 字数 196 浏览 4 评论 0原文

谁能给我指点一下如何使用 ANSI C 实现工厂模式的参考资料?如果涵盖更多模式,那将是一个额外的好处。在 C++ 中执行此操作对我来说微不足道,但由于 C 没有类和多态性,我不太确定如何执行此操作。我正在考虑拥有一个包含所有常见数据类型的“基本”结构,然后使用 void 指针,并按照与顶部基本结构中相同的顺序定义结构的所有公共部分?或者不能保证它们在记忆中以相同的方式结束?

Can anyone point me to a reference on how to implement the factory pattern using ANSI C? If more patterns are covered to that would just be a bonus. Doing this in C++ i trivial for me, but since C does not have classes and polymorphism I'm not quite sure how to do it. I was thinking about having a "base" struct with all the common data types and then using void pointers, and defining all the common parts of the structs in the same order as in the base struct at the top? Or is it not guaranteed that they end up in the same way in memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

热鲨 2024-09-15 12:16:29

工厂模式也可以在 C 中实现,假设以下是您的接口定义的操作:

typedef int (*operations_1) (void *data);
typedef int (*operations_2) (void *data);

typedef struct impl_ops_t
{
    operations_1 op1;
    operations_2 op2;
} impl_ops_t;

因此,为了能够获得实现此类接口的实现实例,
您应该定义一个包含数据和操作的结构,然后您可以定义创建
将返回该结构的操作,也可能是销毁操作:

typedef struct ctx_t
{
    void       *data;
    impl_ops_t *operations;
} ctx_t;

typedef ctx_t   (*create_handle)   (void);
typedef void    (*destroy_handle)  (ctx_t **ptr);

typedef struct factory_ops
{
    create_handle  create;
    destroy_handle destroy;
} factory_ops;

您还应该定义一个为您提供工厂方法的函数,
也许你应该有一种方法来获得正确的实施
您的需求(可能不是像下面的示例中那样的简单参数):

typedef enum IMPL_TYPE
{
    IMPL_TYPE_1,
    IMPL_TYPE_2
} IMPL_TYPE;
factory_ops* get_factory(int impl_type);

所以它将像这样使用:

main (...)
{
    factory_ops fact = get_factory(IMPL_TYPE_2);

    // First thing will be calling the factory method of the selected implementation
    // to get the context structure that carry both data ptr and functions pointer
    ctx_t *c = fact->create();

    // So now you can call the op1 function
    int a = c->operations->op1(c->data);
    // Doing something with returned value if you like..
    int b = c->operations->op2(c->data);
    // Doing something with returned value if you like..

    fact->destroy(&c);
    return 0;
}

Factory pattern can be implemented in C also, suppose the following are the operations that your interface defines:

typedef int (*operations_1) (void *data);
typedef int (*operations_2) (void *data);

typedef struct impl_ops_t
{
    operations_1 op1;
    operations_2 op2;
} impl_ops_t;

So, to be able to get an implementations instance that implements such interface,
you should define a structure with both data and operations, then you can define the create
operation that will return to you that structure and probably a destroy operations also:

typedef struct ctx_t
{
    void       *data;
    impl_ops_t *operations;
} ctx_t;

typedef ctx_t   (*create_handle)   (void);
typedef void    (*destroy_handle)  (ctx_t **ptr);

typedef struct factory_ops
{
    create_handle  create;
    destroy_handle destroy;
} factory_ops;

You should also define a function that provides you the factory methods,
maybe you should have a way to get the right implementation basing on
your needs (probably not a simple parameter like in the example below):

typedef enum IMPL_TYPE
{
    IMPL_TYPE_1,
    IMPL_TYPE_2
} IMPL_TYPE;
factory_ops* get_factory(int impl_type);

So it will be used like:

main (...)
{
    factory_ops fact = get_factory(IMPL_TYPE_2);

    // First thing will be calling the factory method of the selected implementation
    // to get the context structure that carry both data ptr and functions pointer
    ctx_t *c = fact->create();

    // So now you can call the op1 function
    int a = c->operations->op1(c->data);
    // Doing something with returned value if you like..
    int b = c->operations->op2(c->data);
    // Doing something with returned value if you like..

    fact->destroy(&c);
    return 0;
}
情定在深秋 2024-09-15 12:16:29

C 有函数指针和结构体。所以你可以用 C 实现类。

这样的东西应该会给你一个线索。

void class1_foo() {}
void class2_foo() {}

struct polyclass
{
    void (*foo)();
};

polyclass make_class1() { polyclass result; result.foo = class1_foo; return result; }

C have function pointers and structs. So you can implement classes in C.

something like this should give you a clue.

void class1_foo() {}
void class2_foo() {}

struct polyclass
{
    void (*foo)();
};

polyclass make_class1() { polyclass result; result.foo = class1_foo; return result; }
妥活 2024-09-15 12:16:29

这里位于页面底部,是一系列有关 C 语言模式的文章

Here, at the bottom of the page, is a series of articles about patterns in C

泅人 2024-09-15 12:16:29

工厂模式是一种面向对象的设计模式。

C 不是面向对象的语言。

因此,我会质疑你在 C 语言中建立工厂的目标是什么。

The Factory pattern is an Object-Oriented design pattern.

C is not an Object-Oriented language.

Therefore, I would question what your goal is for a Factory in C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文