The Singleton pattern is used to restrict the instantiation of a class to just one object. The singleton pattern must satisfy the single instance and global access principles.

image

A nice pattern, but: Why not use a global static variable?

The answer lies in the fact that this generally is not considered object-oriented. However, there are various different ways to implement the Singleton pattern, one more elegant than the other.

In the original implementation the client just creates a singleton and does its work, like setting and getting a ‘global’ variable.

Instantiation of a singleton is done by calling its instantiation method. It checks if it is already instantiated. If this is the case, it returns the object. Otherwise, it creates one.

The problem with this code is that it is not thread-safe. As a result, it is possible to create more instance. This is an unwanted side-effect of multithreading. You could use locking, but there is also a lock-free and thread-safe version.

I certainly like this one! Read more about the pros and cons in the article Implementing the Singleton Pattern in C#, written by John Skeet.

Want to see a simplified real world example? Just rename Singleton to Logger and implement a method Log, like this:

See? The Singleton is a powerful pattern if you implement it correctly.

This post is part of my series on the foundational design patterns in C#. In this series we explore the ancient design patterns and their use in real-world programming situations of today. In these series of posts we explore each pattern and look at simplified real-world C# code that implements the pattern.