Introduction

Imagine you are in a small office with five people, but there is only one printer. Everyone shares that same printer to get their work done. You wouldn't buy a brand-new printer every time someone wants to print a single page; that would be a waste of money and space.

In programming, the Singleton Design Pattern works exactly like that office printer. It ensures that a class has only one instance (one "printer") and provides a single point of access to it for the entire application.

Why Use the Singleton Pattern?

Sometimes, creating multiple copies of an object is inefficient or even dangerous. You use a Singleton when you need to manage a shared resource, such as:

Implementation Example

In C#, we implement a Singleton by making the constructor private (so no one else can create it) and providing a static property to access the single instance.

public sealed class DatabaseConnection
{
    // 1. The single instance is stored here
    private static DatabaseConnection _instance = null;
    private static readonly object _lock = new object();

    // 2. Private constructor prevents others from using "new"
    private DatabaseConnection() 
    {
        Console.WriteLine("Connected to Database.");
    }

    // 3. The only way to get the object
    public static DatabaseConnection Instance
    {
        get
        {
            // Thread-safety: prevents two threads from creating two instances
            lock (_lock)
            {
                if (_instance == null)
                {
                    _instance = new DatabaseConnection();
                }
                return _instance;
            }
        }
    }

    public void Query(string sql) => Console.WriteLine($"Executing: {sql}");
}

Key Advantages

Conclusion

In this article, we have seen how the Singleton Pattern ensures that critical resources are managed through a single, shared instance. By using a private constructor and a thread-safe access point, you can build .NET applications that are more efficient and prevent the chaos of redundant objects.