C语言 C中是否有任何“设计模式”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4112796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are there any "design patterns" in C?
提问by onemasse
I know that design patterns is generally something that's connected to OO programming, but do you have some pattern you often use when you program C?
我知道设计模式通常与面向对象编程有关,但是您在编写 C 时是否经常使用某些模式?
I'm not interested in simple translations of the classical OO patterns and please don't mention Duff's device. ;-)
我对经典 OO 模式的简单翻译不感兴趣,请不要提及 Duff 的设备。;-)
回答by CoolAJ86
My favorite is the "Patterns in C" series by Adam Tornhill:
我最喜欢的是Adam Tornhill的“Patterns in C”系列:
Also: I always think of gotoas a great poor man's tool for the decorator pattern.
另外:我一直认为goto装饰者模式是一个伟大的穷人的工具。
Update: I'd highly recommend using Rust(rust-lang.org) rather than C except where you are required to use c. Rust has all of the benefits of c, including speed and binary library compatibility with c, but the compiler handles much of the complexity to ensure that the code is memory safe and does not contain data races. It's also portable, has a standard library for common tasks, and much easier to program with for various design patterns.
更新:我强烈建议使用Rust( rust-lang.org) 而不是 C,除非你需要使用 c。Rust 具有 c 的所有优点,包括速度和与 c 的二进制库兼容性,但编译器处理了大部分复杂性以确保代码是内存安全的并且不包含数据竞争。它也是可移植的,具有用于常见任务的标准库,并且更易于针对各种设计模式进行编程。
回答by Vijay Mathew
Design Patterns could be viewed as missing language features. The Introduction of Design Patterns: Elements of Reusable Object-Oriented Softwarestates:
设计模式可以被视为缺少语言功能。设计模式介绍:可重用面向对象软件的元素指出:
The choice of programming language is important because it influences one's point of view. Our patterns assume Smalltalk/C++-level language features, and that choice determines what can and cannot be implemented easily. If we assumed procedural languages, we might have included design patterns called "Inheritance," "Encapsulation," and "Polymorphism."Similarly, some of our patterns are supported directly by the less common object-oriented languages. CLOS has multi-methods, for example, which lessen the need for a pattern such as Visitor. (italics mine)
编程语言的选择很重要,因为它会影响一个人的观点。我们的模式假设 Smalltalk/C++ 级别的语言特性,而这种选择决定了哪些可以轻松实现,哪些不能轻松实现。如果我们假设使用过程语言,我们可能会包含称为“继承”、“封装”和“多态”的设计模式。同样,我们的一些模式直接由不太常见的面向对象语言支持。例如,CLOS 具有多种方法,从而减少了对诸如访问者之类的模式的需求。(斜体我的)
The sentence in italics is the answer to your question.
斜体字是你问题的答案。
回答by Edmund
Polymorphism via callbacks, e.g. the standard library's qsortfunction. All it needs is a way to compare two elements, and it can sort an array of them.
通过回调实现多态,例如标准库的qsort函数。它所需要的只是一种比较两个元素的方法,它可以对它们的数组进行排序。
You can be much more sophisticated than this by using sets of functions (vtables) to represent the pertinent properties of a type so that a generic routine can process it usefully. For example, the read, write, etc. calls on an open file, or network port.
通过使用函数集(vtables)来表示类型的相关属性,以便通用例程可以有效地处理它,您可以比这更复杂。例如,读取、写入等调用打开的文件或网络端口。
回答by Vlad
Yes, there are. Lazy initialization, singleton, object pool, object state etc. are easily implemented in pure C.
是的,有。延迟初始化、单例、对象池、对象状态等在纯 C 中很容易实现。
Example (lazy initialization)
示例(延迟初始化)
#include <stdio.h>
struct foo
{
int payload;
};
int calculate_payload()
{
printf("%s\n", "Performing lengthy initialization...");
return 42;
}
struct foo *get_default_foo()
{
static int foo_calculated = 0;
static struct foo default_foo;
if (!foo_calculated) /* assuming single-threaded access */
{
foo_calculated = 1;
default_foo.payload = calculate_payload();
}
return &default_foo;
}
int main()
{
struct foo *foo1, *foo2;
printf("%s\n", "Starting the program");
foo1 = get_default_foo();
printf("%d\n", foo1->payload);
foo2 = get_default_foo();
printf("%d\n", foo2->payload);
return 0;
}
回答by src
From the top of my head
从我的头顶
回答by Sim Sun
Virtual File System is perfect example for learning the Design Pattern.
虚拟文件系统是学习设计模式的完美例子。
回答by Peter G.
Design Patterns often model things that are just one level from what an existing environment offers. If you take C with its standard library as the environment an eminent design pattern is Object Orientation.
设计模式通常对现有环境提供的只是一个级别的事物进行建模。如果将 C 及其标准库作为环境,那么一个著名的设计模式是面向对象。

