Functional Singleton Pattern
Learn the Functional Singleton Pattern: when to use it, practical examples in C# and F#, and its relevance in modern programming.
This is my favorite pattern. First pattern I learned. And till date I don't know where to use it. So, I use everywhere. :P
I have mainly used it for database connection objects and then all JavaScript frameworks are exposing Singleton objects only. I can say it is quite useful in the case of web.
The fun part about this pattern is that most people, including me, always use this pattern to show off that we know patterns and we follow it in practice but in most cases that practice starts and stops with the Singleton Pattern.
Let's understand the pattern. As the name suggests, this pattern is propagating singularity. An object of a specific class will be created once and only once. No more. Object creation is totally hidden away from the consumer and with a static method, the object will be handed over to the consumer. If there is an instance present, it will be handed over; else a new instance will be created and it will be given to the consumer.
Let's check out the C# code
See, simple and easy.
And now here is the F# code. Even simpler and easier.
Look ma, no
null. As F# is not allowing null by default in the system, there is no need to check it.
Instead, make the constructor private to stop creating objects of that type and provide the same instance of the type again and again.
Now, in current scenarios where garbage collectors are so powerful and we are having quite a good amount of hardware to process, Singleton is not used while writing domain/business code. But still, it is widely used on the API side to expose specific functionality.
I am still to find relevance in the case of functional programming. In functional programming, type is different from class. In normal cases, they don't have behaviors attached to them. So, it reduces the need for this pattern.
Frequently Asked Questions
The Singleton Pattern ensures that only one instance of a specific class is created throughout the application's lifetime. Object creation is hidden from the consumer, and a static method provides access to the single instance—if it exists, it's returned; otherwise, a new instance is created and then reused for all subsequent requests.
The Singleton Pattern is particularly useful for database connection objects and is widely used in JavaScript frameworks. It's most effective on the API side to expose specific functionality, though modern development with powerful garbage collectors tends to avoid it in domain and business logic code.
Make the constructor of your class private to prevent direct instantiation, then provide a static method that returns the single instance. The first call creates the instance, and subsequent calls return the existing instance. Languages like F# simplify this further by eliminating null checks.
The Singleton Pattern has limited relevance in functional programming because types in functional languages differ from classes and typically don't have attached behaviors, reducing the need for instance control patterns like Singleton.