Understanding the way of Singleton Design Pattern

Göksu Deniz
19 min readJan 20, 2023

--

Image is created by DALL-E 2 AI

The Singleton design pattern is a way to ensure that a class has only one instance throughout the lifetime of an application, and to provide a global point of access to this instance. The key idea behind the pattern is to use a private constructor to prevent other classes from instantiating the class, and to use a static method to return the single instance of the class, creating the instance if it doesn’t already exist.

Here is an example of how the Singleton pattern could be implemented in C#:

public sealed class Singleton
{
private static Singleton instance = null;
private static readonly object padlock = new object();

Singleton()
{
// private constructor to prevent instantiation
}

public static Singleton Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
}

In this example, the Singleton class has a private constructor, which prevents other classes from instantiating it directly. The class also has a public property Instance with a getter method, which returns the single instance of the class. The method first checks if an instance of the class already exists and if not, it creates a new instance of the class and assigns it to the instance variable. The method is also thread-safe, it uses a lock statement to ensure that only one thread can create an instance at a time.

You can use it in the following way:

Singleton s = Singleton.Instance;

The Singleton design pattern is useful in situations where you need to have a single instance of a class that controls the action throughout the execution of an application. For example, you might use a Singleton to manage access to a shared resource, like a database or a file system.

However, it’s worth noting that overuse of the Singleton pattern can make your code more difficult to test, as it can introduce global state into your application. Additionally, it can also cause issues in multi-threaded environments.

There are several benefits to using the Singleton pattern:

  1. Control of object creation: The Singleton pattern gives you control over the instantiation of a class, by ensuring that only one instance of the class exists in the application.
  2. Global access point: The Singleton pattern provides a global access point to the single instance of a class, which can be accessed from any part of the application.
  3. Reduced resource usage: By having only one instance of a class, the Singleton pattern can help reduce resource usage in an application.
  4. Consistent state: With only one instance of a class, the Singleton pattern can help ensure that the state of the class is consistent throughout the application.
  5. Simplified code: The Singleton pattern can help simplify the code by removing the need to pass around references to the same object, allowing you to use the same instance across the application.
  6. Thread-safety: If implemented carefully, Singleton can be thread-safe, it can ensure that only one thread can create an instance of a class at a time.

It’s worth noting that, as with any design pattern, the Singleton pattern should be used judiciously and with consideration for the specific needs of your application. Overuse of the pattern can make your code more difficult to test and can introduce global state into your application.

The Singleton pattern is a way to make sure that only one copy of a special class is created and used throughout the whole program. This is helpful when you want to make sure that only one thing is controlling a certain part of the program, like making sure that there is only one “boss” in charge of a group of things. It also helps save computer memory by only having one copy of the special class instead of multiple copies.

For example, let’s say you are making a computer game and you want to make sure that there is only one “player” in the game. You can use the Singleton pattern to make sure that only one player is created and controlled throughout the whole game. This way, you don’t have to worry about multiple players running around and causing confusion.

Another example, let’s say you have a program that controls the temperature of a room, and you want to make sure that only one thing is controlling the temperature. You can use the Singleton pattern to make sure that only one “thermostat” is controlling the temperature.

It’s important to use this pattern wisely, because if you use it too much, it can make the program hard to understand and change. It is also important to make sure that it’s thread-safe, because in concurrent environments multiple threads can access the shared resource at the same time and make the class behave in unexpected ways.

Here’s an example of how the Singleton pattern could be used to implement a single player in a game using C#:

public sealed class Player
{
private static Player instance = null;
private static readonly object padlock = new object();

private Player()
{
// private constructor to prevent instantiation
}

public static Player Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Player();
}
return instance;
}
}
}

public int Health { get; set; }
public int Score { get; set; }
public string Name { get; set; }

public void Move(int x, int y)
{
// code to move the player
}

public void Attack()
{
// code to make the player attack
}
}

In this example, the Player class has a private constructor, which prevents other classes from instantiating it directly. The class also has a public property Instance with a getter method, which returns the single instance of the class. The method first checks if an instance of the class already exists and if not, it creates a new instance of the class and assigns it to the instance variable. The method is also thread-safe, it uses a lock statement to ensure that only one thread can create an instance at a time.

The Player class has properties like Health, Score, and Name and methods such as Move(int x, int y),Attack() that can be used to control the player.

You can use it in the following way:

Player player = Player.Instance;
player.Name = "Player1";
player.Move(10, 20);

This way you can ensure that there’s only one player in the game and you can access the player’s properties and methods from anywhere in the code base.

In C#, the sealed keyword is used to indicate that a class cannot be inherited. A sealed class cannot be used as a base class, and any class that is derived from a sealed class cannot be used as a base class itself.

In the case of the Singleton pattern, we use the sealed keyword to prevent other classes from inheriting from our Singleton class. If a class is not sealed, and it's used as a base class, it would be possible for a derived class to create multiple instances of the Singleton class, breaking the singleton pattern. By sealing the class, we ensure that the singleton pattern cannot be broken by any derived classes.

Additionally, using sealed class for Singleton pattern, makes it harder for the developer to accidentally create multiple instances of the class, as the developer will not be able to inherit from the class, so the only way to create an instance of the class is to use the provided static method, in this way the developer will be force to use the pattern in the way it was intended to be used.

It’s possible to use the static keyword to implement the Singleton pattern in C#. When a class is declared as static, all its members are also implicitly static, this means that the class cannot be instantiated and all its members can be accessed without creating an instance of the class.

Here’s an example of how the Singleton pattern could be implemented using a static class in C#:

public static class Player
{
private static Player instance = null;
private static readonly object padlock = new object();

static Player()
{
// private constructor to prevent instantiation
}

public static Player Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Player();
}
return instance;
}
}
}

public int Health { get; set; }
public int Score { get; set; }
public string Name { get; set; }

public void Move(int x, int y)
{
// code to move the player
}

public void Attack()
{
// code to make the player attack
}
}

In this example, the Player class is declared as static, so it cannot be instantiated and all its members are also implicitly static.

It’s important to note that since a static class cannot be inherited, so it’s not possible to create a derived class from a static class.

In general, it’s not considered a best practice to use static classes as Singletons, because it's less flexible, it cannot be inherited, and it's less discoverable, as it does not follow the same naming conventions as other classes. It's considered better to implement the Singleton pattern using a non-static class, as it's more discoverable and more flexible.

There are several ways to implement the Singleton design pattern, including:

1. Eager Initialization

In this approach, the instance of the singleton class is created at the time of class loading, this way the instance is guaranteed to be created before it’s used.

public class Singleton
{
private static readonly Singleton instance = new Singleton();

// Private constructor to prevent instantiation
private Singleton() { }

public static Singleton Instance
{
get
{
return instance;
}
}
}

In this example, the instance variable is declared as static readonly, which means that it is a static variable that is created at the time the class is loaded, and it's read-only, meaning it can't be modified after it's created.

In the private constructor, we prevent other classes from instantiating the class directly. The class also has a public property Instance with a getter method, which returns the single instance of the class.

The instance of the class is created immediately when the class is loaded, this is why it’s called Eager Initialization.

It’s worth noting that Eager Initialization can lead to wasted resources if the singleton is never used, but it ensures that the singleton is always available and ready to use when it’s needed.

2. Lazy Initialization

In this approach, the instance of the singleton class is created only when it is first requested. This approach is generally considered more efficient in terms of memory usage, but it may cause threading issues if not implemented carefully.

public class Singleton
{
private static Singleton instance;

// Private constructor to prevent instantiation
private Singleton() { }

public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}

In this example, the instance variable is declared as static but not initialized, so it's only created when the Instance property is accessed for the first time.

In the private constructor, we prevent other classes from instantiating the class directly. The class also has a public property Instance with a getter method, which returns the single instance of the class. The method first checks if an instance of the class already exists, and if not, it creates a new instance of the class and assigns it to the instance variable.

This approach is considered more efficient in terms of memory usage, as the instance of the class is created only when it is first requested, this is why it’s called Lazy Initialization.

It’s important to note that Lazy Initialization can cause threading issues if not implemented carefully, because multiple threads might access the Instance property at the same time and create multiple instances of the class.

To overcome this issue, you can use Double-Checked Locking which uses a double-checked locking mechanism to ensure that the singleton instance is created only once and that multiple threads can access the instance safely.

3. Double-Checked Locking

This approach is a variation of the Lazy Initialization approach and it’s designed to avoid the threading issues that can occur with Lazy Initialization. It uses a double-checked locking mechanism to ensure that the singleton instance is created only once and that multiple threads can access the instance safely.

public class Singleton
{
private static Singleton instance;
private static readonly object padlock = new object();

// Private constructor to prevent instantiation
private Singleton() { }

public static Singleton Instance
{
get
{
if (instance == null)
{
lock (padlock)
{
if (instance == null)
{
instance = new Singleton();
}
}
}
return instance;
}
}
}

In this example, the instance variable is declared as static, and it's not initialized, so it's only created when the Instance property is accessed for the first time.

In the private constructor, we prevent other classes from instantiating the class directly. The class also has a public property Instance with a getter method, which returns the single instance of the class. The method uses a lock statement to ensure that only one thread can create an instance of the class at a time.

This approach is a variation of the Lazy Initialization approach, it uses a double-checked locking mechanism to ensure that the singleton instance is created only once and that multiple threads can access the instance safely.

Double-Checked Locking is considered a more efficient way of implementing the Singleton pattern, as it avoids the overhead of acquiring and releasing a lock every time the Instance property is accessed, this is because it checks if the instance is created before acquiring a lock.

It’s worth noting that, as with any design pattern, the Singleton pattern should be used judiciously and with consideration for the specific needs of your application, and if you’re using a version of C# that is less than 4.0, the Double-Checked Locking might not be thread safe due to the specific behavior of the memory model in older versions of C#.

4. Static Block Initialization

This approach is similar to Eager Initialization, but it allows for more complex operations to be performed during the initialization of the singleton class.

public class Singleton
{
private static Singleton instance;

static Singleton()
{
instance = new Singleton();
}

// Private constructor to prevent instantiation
private Singleton() { }

public static Singleton Instance
{
get
{
return instance;
}
}
}

In this example, the instance variable is declared as static, and it's initialized in a static block, which is executed only once when the class is loaded.

In the private constructor, we prevent other classes from instantiating the class directly. The class also has a public property Instance with a getter method, which returns the single instance of the class.

This approach is similar to Eager Initialization, but it allows for more complex operations to be performed during the initialization of the singleton class.

It’s worth noting that the static block initialization ensures that the singleton instance is created before it’s used and that it’s thread safe, but it can lead to wasted resources if the singleton is never used.

It is also possible to use a combination of the static block initialization and Lazy Initialization to make it more efficient in terms of memory usage by only creating the singleton when it’s first requested.

5. Enum Singleton (for JAVA developers)

This approach is considered the most effective way of implementing the Singleton pattern in Java. Enum values are globally accessible and are guaranteed to be unique.

public enum Singleton {
INSTANCE;

public void doSomething() {
// your code here
}
}

In this example, the Singleton class is declared as an enum, and it has a single instance called INSTANCE, which is automatically created by the JVM when the enum type is initialized.

Enum values are globally accessible, and they’re guaranteed to be unique, so it’s a good way to implement the Singleton pattern in Java.

It’s worth noting that Enum Singleton is considered the most effective way of implementing the Singleton pattern in Java, it’s thread-safe by default, and it provides serialization out-of-the-box. Also, it’s impossible to create multiple instances of the class by using reflection or serialization.

The Enum Singleton approach can be used in C#, but it is not considered a best practice. Enumerations in C# are not designed for this purpose and it might not be as effective as it is in Java.

In C#, Enumerations are lightweight, value types that are used to represent a set of named constants, they are not classes, so they don’t have constructors, methods, or properties.

Also, C# has built-in support for thread-safe lazy initialization, unlike Java, which makes other approaches more suitable in C#.

It’s important to consider the specific needs of your application when choosing the appropriate implementation of the Singleton pattern. It’s always better to use a well-established and appropriate design pattern rather than trying to fit a square peg into a round hole.

6. Bill Pugh Singleton

This approach uses a nested static inner class to hold singleton instance, this approach is considered more efficient than the previous one, because it uses the class loading mechanism to handle the singleton instantiation.

public class Singleton
{
private Singleton() { }

private class SingletonHolder
{
static readonly Singleton instance = new Singleton();
}

public static Singleton Instance
{
get
{
return SingletonHolder.instance;
}
}
}

In this example, The Singleton class has a private constructor, this is to prevent instantiation of the class.

It has a nested private class called SingletonHolder that holds the single instance of the Singleton class. The instance of the Singleton class is created when the SingletonHolder class is loaded by the JIT compiler.

The SingletonHolder class is never instantiated, so the instance of the Singleton class is only created when it is first requested. This makes it a lazy initialization approach.

It’s worth noting that the Bill Pugh Singleton approach uses the class loading mechanism to handle the singleton instantiation, so it’s more efficient than the traditional Lazy Initialization approach, because it doesn’t require locking.

It’s considered one of the most efficient ways of implementing the Singleton pattern, and it’s also thread safe, because it uses the class loading mechanism.

7. Monostate Singleton

This approach uses a single instance variable shared across all instances of the singleton class, it’s a way of having multiple instances of a class while still having a single point of control.

public class Singleton
{
private static int health;
private static int score;
private static string name;

public int Health
{
get { return health; }
set { health = value; }
}

public int Score
{
get { return score; }
set { score = value; }
}

public string Name
{
get { return name; }
set { name = value; }
}

public void Move(int x, int y)
{
// code to move the player
}

public void Attack()
{
// code to make the player attack
}
}

In this example, the Singleton class has properties Health, Score and Name that are shared across all instances of the class. This means that when one instance changes the value of one of these properties, all the other instances will have the same value.

This approach, also known as Monostate, allows for multiple instances of a class while still having a single point of control.

You can use it in the following way:

Singleton player1 = new Singleton();
Singleton player2 = new Singleton();
player1.Health = 100;
Console.WriteLine(player2.Health); // Outputs 100

It’s worth noting that the Monostate Singleton is not thread-safe by default, so it’s important to add synchronization when using multiple instances in a multi-threaded environment to make it thread safe.

8. Dependency Injection

This approach uses a framework or a library to handle the instantiation of the singleton class. The framework or library is responsible for creating and managing the singleton instance, allowing for more flexibility and ease of testing.

public interface ISingleton
{
void DoSomething();
}

public class Singleton : ISingleton
{
public void DoSomething()
{
// your code here
}
}

In this example, we have an interface ISingleton and a class Singleton that implements the interface.

The class can be registered as a singleton in a Dependency Injection container such as Autofac, Ninject, or Unity.

var container = new ContainerBuilder();
container.RegisterType<Singleton>().SingleInstance();

This will ensure that only one instance of the class is created and that it is reused throughout the application.

You can use it in the following way:

var singleton = container.Resolve<ISingleton>();
singleton.DoSomething();

This approach uses a framework or a library to handle the instantiation of the singleton class. The framework or library is responsible for creating and managing the singleton instance, allowing for more flexibility and ease of testing.

You can use the Dependency Injection approach with service registrations on startup.

When you register your services on startup, you can register the Singleton class as a single instance service. This way, the framework or library you are using will create only one instance of the Singleton class and reuse it throughout the application.

For example, in ASP.NET Core, you can use the AddSingleton method in the Startup class to register the Singleton class as a singleton service:

public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ISingleton, Singleton>();
}

This way, when the dependency injection container creates an instance of the Singleton class, it will reuse the same instance throughout the application.

It’s worth noting that you should use this approach only if you want to make sure that there’s only one instance of the class in the application and if you don’t want to manage the lifetime of the class yourself. And also, when using this approach, you should make sure that the class is thread-safe and that it handles the multi-threading correctly.

9. Lazy<T>

You can use the Lazy<T> class to implement the Singleton pattern in C#. The Lazy<T> class is a built-in C# class that provides thread-safe lazy initialization of an object, it can be used to create a singleton instance of a class.

public class Singleton
{
private static Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());

public static Singleton Instance { get { return lazy.Value; } }

private Singleton() { }
}

In this example, the lazy variable is a static Lazy<Singleton> object that is initialized with a lambda expression that creates a new instance of the Singleton class.

The Instance property returns the value of the lazy variable, which is the singleton instance of the Singleton class.

This approach is considered a more efficient way of implementing the Singleton pattern, as it avoids the overhead of creating an instance of the class until it’s first requested, and it’s thread-safe because the Lazy<T> class uses a double-checked locking mechanism to ensure that the singleton instance is created only once.

It’s worth noting that using Lazy<T> class to implement the Singleton pattern is a good option when you want to have a thread-safe and efficient singleton implementation, but it's not as flexible as the Dependency Injection approach.

The Singleton pattern can be used in conjunction with other design patterns to solve specific problems in software development. Here are some examples of how the Singleton pattern can be used with other design patterns:

1. Factory Method Design Pattern

The Singleton pattern can be used with the Factory Method pattern to create a single instance of a factory class that creates objects of a specific type. This way, the factory class can be easily accessed from anywhere in the code base, and it can ensure that only one instance of the class is created.

public class SingletonFactory
{
private static SingletonFactory instance;

public static SingletonFactory Instance
{
get
{
if (instance == null)
{
instance = new SingletonFactory();
}
return instance;
}
}

public Product CreateProduct(string productType)
{
switch (productType)
{
case "A":
return new ProductA();
case "B":
return new ProductB();
default:
throw new ArgumentException("Invalid product type");
}
}
}

2. Decorator Design Pattern

The Singleton pattern can be used with the Decorator pattern to create a single instance of a decorator class that adds new functionality to an existing class. This way, the decorator class can be easily accessed from anywhere in the code base and it can ensure that only one instance of the class is created.

public class SingletonDecorator : Component
{
private static SingletonDecorator instance;

public static SingletonDecorator Instance
{
get
{
if (instance == null)
{
instance = new SingletonDecorator();
}
return instance;
}
}

public void Operation()
{
// Additional functionality
base.Operation();
}
}

3. Mediator Design Pattern

The Singleton pattern can be used with the Mediator pattern to create a single instance of a mediator class that coordinates communication between objects. This way, the mediator class can be easily accessed from anywhere in the code base and it can ensure that only one instance of the class is created.

public class SingletonMediator
{
private static SingletonMediator instance;

public static SingletonMediator Instance
{
get
{
if (instance == null)
{
instance = new SingletonMediator();
}
return instance;
}
}

private Dictionary<string, List<object>> subscribers;

private SingletonMediator()
{
subscribers = new Dictionary<string, List<object>>();
}

public void Register(string eventName, object subscriber)
{
if (!subscribers.ContainsKey(eventName))
{
subscribers.Add(eventName, new List<object>());
}
subscribers[eventName].Add(subscriber);
}

public void Unregister(string eventName, object subscriber)
{
if (subscribers.ContainsKey(eventName))
{
subscribers[eventName].Remove(subscriber);
}
}

public void Notify(string eventName, object data)
{
if (subscribers.ContainsKey(eventName))
{
foreach (var subscriber in subscribers[eventName])
{
// Notify subscriber
}
}
}
}

4. Command Design Pattern

By using the Singleton pattern, the command invoker class can be accessed from anywhere in the code base and it ensures that only one instance of the class is created, this way the Command pattern is implemented and the commands can be accessed and executed centrally. This allows for a more modular design, where the invoker class doesn't need to know the details of the specific command that it's executing, and the command class doesn't need to know the details of the invoker class.

public class SingletonCommandInvoker
{
private static SingletonCommandInvoker instance;

public static SingletonCommandInvoker Instance
{
get
{
if (instance == null)
{
instance = new SingletonCommandInvoker();
}
return instance;
}
}

private List<ICommand> commands;

private SingletonCommandInvoker()
{
commands = new List<ICommand>();
}

public void AddCommand(ICommand command)
{
commands.Add(command);
}

public void RemoveCommand(ICommand command)
{
commands.Remove(command);
}

public void ExecuteCommands()
{
foreach (var command in commands)
{
command.Execute();
}
}
}

I think you got the point.

Disadvantages

The Singleton pattern has some disadvantages that you should be aware of:

  1. Tight coupling: The Singleton pattern can lead to tight coupling between different parts of the codebase, as objects rely on a singleton instance and cannot be instantiated in any other way.
  2. Limited testability: Singletons can make it difficult to test the code, as the code that uses singletons cannot be easily substituted by mock objects, and it can be harder to control the state of the singleton instance in a test environment.
  3. Global state: Singletons introduce global state into the codebase, which can make the code more difficult to reason about and can lead to subtle bugs.
  4. Limited flexibility: Because a singleton can only have one instance, it can be less flexible than other design patterns.
  5. Thread-safety: Singletons are not thread-safe by default, so it’s important to make sure that the singleton instance is thread-safe when using it in a multi-threaded environment.
  6. Resource consumption: Singleton instances are created when the application starts and they are kept alive till the end of the application, that means they consume resources and memory, even if they are not used.
  7. Performance: Singleton instantiation might cause performance issues, especially if the constructor takes a lot of time to execute.
  8. Not good for Microservices architecture: Singleton pattern is not suitable for microservices architecture because a singleton instance is shared across all instances of the service.

It’s worth noting that while the Singleton pattern has some disadvantages, it can still be a useful tool in certain situations, but it’s important to use it judiciously and to be aware of its limitations.

Conclusion

In conclusion, the Singleton pattern is a design pattern that ensures that a class has only one instance and provides a global point of access to it. It can be useful in situations where a single instance of a class needs to coordinate actions across the system, however, it’s important to use it judiciously and to be aware of its limitations.

The Singleton pattern can lead to tight coupling, limited testability, global state, limited flexibility, thread-safety issues, resource consumption, performance issues and not suitable for microservices architecture.

When considering whether to use the Singleton pattern in your code, it’s important to weigh the benefits against the potential drawbacks and to consider other design patterns that might be more appropriate for your use case.

It’s also important to note that there are several ways to implement the Singleton pattern, and that each way has its own set of trade-offs, so it’s important to choose the way that best suits the requirements of your application.

Thanks for reading! If you found the article helpful, you can clap and follow. So you will notified of new articles.

# Reference

It was created with the help of ChatGPT AI.

--

--

Göksu Deniz
Göksu Deniz

Written by Göksu Deniz

Software Engineer, passionate about creating efficient solutions. Skilled in mentoring teams to deliver successful projects. Always exploring new tech trends.