Functional Factory Pattern
The fun part working or explaining functional pattern is word functional
can be used in very fun
way. Just like it is used in title here.
Now, factory pattern may be the most used pattern in Object Oriented world. Here is code in C#
.
Factory method is basically used to abstract away the object creation. I am not worried how object is created but I am worried about the behavior of the objects that are created.
Now, lets have a look at F#
equivalent
If you can see it is short and simple and providing same result.
Now, functional programming is not one to one
map with Object Oriented programming. But we can always have similar a concept.
Factory Pattern simply hiding the object creation. And in F# we are having option type which is helping us to provide concrete definition of product. And that is the reason we can skip the concrete class and have product definition in type.
I have took examples for this post from Dofactory and Tao Liu s work. I have made few changes thought to make it as similar as possible. In future post I will try to go into greater detail about respective pattern and also check out patterns that can be absolute when you come to the world of functional programming.
F# people. Your comments and review are required. Will update the code as per the suggestions. Do provide them. I don’t mind adding examples of other functional language if anyone is helping me out.