The composite pattern in F#
28 Apr 2011Triggered by an interesting stack overflow question on whether functional languages replace object oriented design patterns I decided to take a look at how you would implement various object oriented design patterns in a functional style. In this post I explore the composite pattern.
Object oriented example
Say we are designing a system for a fast food company where individual menu items have a price, and combinations of menu items can be ordered as “combo meals”. In this scenario we could apply the object oriented composite pattern as follows:
Functional version
In F# one of the basic functional types is the discriminated union. Discriminated unions provide support for values that can be one of a number of named cases, possibly each with different values and types. In addition discriminated unions can be recursive. As such we can implement the composite pattern in a single discriminated union.
One obvious difference between the functional and object oriented versions is the terseness that comes with using a discriminated union. Another is that the discriminated union publicly exposes the fact that a component is created by composition. However, this isn’t necessarily a bad design decision as it aligns with the functional emphasis on adding new functionality to existing types. This means that the GetPrice member does not need to be defined as part of the type. Instead it can be implemented independently of the type as a processing function.