Implementing Factory Pattern in C

Dec. 30. 2015

Factory method is a very important design pattern in object oriented designs[1]. The main goal of this pattern is to allow the client to create objects without having to specify the details of the objects to create. It returns one of several possible classes that share a common super class. This post is not going to discuss factory pattern in C++ or any other OOP language. Instead, a similar implementation of the pattern in the programming language C will be discussed.

Function Factory: prototype

Function factory has two slightly different prototypes:

and

Function Factory: A Demonstrative Example

Next, I am going to demonstrate function factory in a simple example.

As what we have pointed out above, the function factory in the above program can also be declared as follows

Below is the output of the above program

References

Written by

Senior Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store