Singleton Pattern
Explore the Singleton pattern in C#. Learn with code examples.
The Singleton Pattern is something many people have used without even realising it. It’s used in software engineering to restrict the instantiation of a class to one object. In other words, it ensures that a class has only one instance and provides a global point of access to that instance.
In other…other words it’s useful in situations where you need to ensure that only one instance of a class exists and that it is easily accessible to other parts of your code.
A good example would be a logging system that needs to be accessed by multiple components. You use a singleton to sure that there is only one instance of the logging system, and that all components access the same instance.
Coding Examples
C# Example
Here’s a simple example of how to implement the Singleton Pattern in C#:
|
|
Here we see Singleton has a private constructor to prevent object instantiation from outside the class. Instead, we use the Instance property to access the class:
|
|
That’s as simple as it gets!
C++ Example
In C++ things become a bit more intricate:
|
|
Let’s explain things a bit:
- Private Constructor and Destructor: The constructor and destructor of the
Singletonclass are private to prevent direct instantiation and destruction. - Deleted Copy Constructor and Assignment Operator: The copy constructor and assignment operator are deleted to prevent copying of the singleton instance.
- Static Method for Access: The
getInstancestatic method provides a global point of access to the singleton instance. It uses a local static variable to ensure that only one instance is created and it is destroyed when the program terminates. - Example Method: The
showMessagemethod is an example method that can be called on the singleton instance.
Python Example
In Python we have more than one ways of achieving this, let’s look at a more complex example first:
|
|
This method gives you a lot of control over how the singleton is instantiated, though it is complex to understand at first and I find myself referencing it time and again.
A different method would be using a decorator:
|
|
This is more concise and easy to understand.
My favourite method though, is using a module:
|
|
In Python, modules are singletons by design, meaning that they are only loaded once and the same instance is used throughout the application.
Thread-safety
Something I neglected to touch on in the C# example was that it was fine and well, in a single-threaded environment. If we move to multiple threads this class might not be very safe to use. I’m not going to address thread safety, as a concept, in this article but a simple way to make this singleton thread safe would be as follows:
|
|
Here we used the double-check locking technique and the Singleton classic is made thread-safe by using the lock statement to ensure that only one threat at a time can create the instance of the class.
The volatile keyword tells the compiler to exclude the property from various optimisations and ultimately ensures the value of instance is always up-to-date across different threads.
The double-check locking technique is used to minimise the performance impact of the lock statement. We can implement the class similarly using something like semaphores.
A final note is that this implementation is lazy-loaded. In other words, the Singleton is not instantiated until it is requested. You could easily create it eagerly by initialising the instance field when it is declared.