-
Notifications
You must be signed in to change notification settings - Fork 160
/
design_patterns.c
66 lines (43 loc) · 1.56 KB
/
design_patterns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
# Design patterns
A good way to learn is to look at existing libraries:
- https://github.com/libgit2/libgit2
*/
#include "common.h"
int main(void) {
/*
# Objects
Prefix every function that acts on an object with the name of the object.
The first non return parameter of the function
should be a pointer to the struct, AKA self in many languages.
E.g., libgit 2 repository methods are all of the type:
git_repository_X(git_repository* repo, ...);
*/
/*
# Private struct fields
Opaque structs and expose public fields through getter and setter methods.
*/
/*
# Static fields
Static.
For private, don't include on header.
*/
/*
# Namespaces like in C++
Prefix everything public in your entire libray with a single identifier.
E.g., in libgit2, every public identifier starts with `git_`.
Of course, with this it is not possible to omit the namespace like in C++,
but many C++ coding styles don't allow that either because it becomes confusing what is what.
*/
/*
# Inheritance
The closest substitution to inheritance is struct inclusion / composition:
typedef struct derived {
struct base* base;
}
The difference is that you cannot automatically give base methods to the inheriting class.
But even in C++, object composition is considered a more flexible approach than
inheritance and is used in some cases.
*/
return EXIT_SUCCESS;
}