Design Patterns — Singleton

The Singleton design pattern is one of the creational pattern from ‘Gang of Four’ (GoF), and it ensures that a class only has one instance, and provide a global point of access to it. The Singleton pattern restricts a class to instantiate its multiple objects. The Singleton defines an Instance operation that lets clients access its unique instance, and also is responsible for creating and maintaining its own unique instance.

How Singleton works

This is the UML class diagram:

This class has a property that generally is private, and has a method that returns the instance of the object. In the “Instance” method, it’s checked if the instance of the class was already created, and if it was it will return the created instance, otherwise, it will create an instance, and next time that this class is called it will return the same created instance. This can be a static class or use a static method.

When Singleton can be used

If you have a functionality that just handle something or just process something, and does not need to know the state of the object (is totally static), it’s not necessary to have many instances of the same object, spending memory to do something that a single instance can do. For some components, it only makes sense to have one instance in the system, for example:

So Singleton can be used in situations where the constructor call to a particular object is particularly expensive and you only need to have this call being called once (needs one, and only one instance of an object), and once that you construct the singleton object, you will provide everyone with the same instance of that object that was instantiated, avoiding that other clients make new copies of that object.

Structural code in C#

This structural code demonstrates the Singleton pattern which assures only a single instance (the singleton) of the class can be created.

To implement and execute the examples for this article, I’ve created a console application using .NET Core. For this structural example, we have two files, the Singleton and the Program class. This is the Singleton class:

The “_instance” property is a private property and it is a property of the same class. This property is static, so when it is created it will be kept. The Singleton class constructor is protected, so the way to create an object of type Singleton is through the Instance method. What will happen is that always when the Instance method is called, it will check if an instance of Singleton class already exists, and if exists, it will return the instance, otherwise it will create a new instance, this way it will only exist one single instance of the Singleton class.

This is the main class in the Program class where we make the call to the Singleton class:

Because these two objects are the same instance of the Singleton class, the result is that this text will be displayed:

Objects are the same instance

In .NET Core, it’s also possible to use Dependency Injection to inject the class in a Singleton way, instead of implementing the Singleton pattern, this way you will also have the same benefit which is having the same memory allocation.

This is the link for the code:

If you like this solution, I kindly ask you to give an ⭐️ in the repository.
And also invite you to follow me on GitHub.

Thanks for reading!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Henrique Siebert Domareski

I've been working with software development with .NET since 2011, and love programming and solve problems using clean code particles.