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?
+1
A:
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.
Justin Niessner
2010-07-08 14:22:31
That a module I'm about to build is really suitable for the factory design-patten. But the company has a strict policy of only using ansi C.
inquam
2010-07-08 14:26:57
@inquam - Understandable, but could you maybe give us an example of what you want the factory for (since normally a Factory is used to create an instance of an Interface backed by different class implementations)?
Justin Niessner
2010-07-08 14:28:50
I know and this is what I want... But have to struggle with the limitations of C. Let's say you build a system that will scan for viruses in files and report the findings. There are many different ways, and more might come up in the future to, detect the different viruses. But the reporting part for all these are the same. If I could have a way to make the "base" with data structure for holding report data etc in a similar fashion, all different "scanners" could run through the same report functions. But the actual work they to to scan could be different.
inquam
2010-07-08 14:44:32
I disagree. Object orientation is a paradigm, a paradigm which can be realised using C. I'm not saying that it's a good idea though.
manneorama
2010-07-08 16:18:57
@manneorama - Object Oriented programming is defined as a language supporting three basic concepts: inheritance, polymorphism, and encapsulation. Without inheritance and polymorphism...C doesn't qualify.
Justin Niessner
2010-07-08 16:27:35
@Justin, All that is possible to achieve using C. Take a look at this question for example; http://stackoverflow.com/questions/415452/object-orientation-in-c/415536#415536. Now, I'm not arguing that C is designed for being used in an object oriented manner and I'm not saying that it should be used in such a way, in place of a maybe more capable language. I'm saying that if someone, for some reason, wants to do it, it is possible and can be done. With substantial effort and very little gain =)
manneorama
2010-07-08 19:47:21
+1
A:
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; }
yatagarasu
2010-07-08 14:22:50