Design Patterns: Singleton

This is the first post in what I hope to be a series of posts on software design patterns. What are design patterns? Design patterns in software development are repeatable code solutions to commonly reoccurring problems. Think of them as templates to common problems that other developers have come up with to solve issues previous developers have dealt with. They help to reduce bugs and can help make your code more maintainable as it grows. Use of these patterns is considered best practices and trust me, smart people have designed them for a reason. So use them. 

Singleton Pattern

A Singleton is a class that has only one instance and provides global access to itself.  In other words, once the class in substantiated, it maintains a reference to itself, and routes all future class construction calls back to the same referenced instance. 

using System;

namespace SingletonPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            MySingletonClass firstInstance = MySingletonClass.Instance;

            firstInstance.TestString = "This was set by firstInstance";
            Console.WriteLine($"Setting firstInstance to {firstInstance.TestString}");

            MySingletonClass secondInstance = MySingletonClass.Instance;
            Console.WriteLine($"Getting secondInstance value {secondInstance.TestString}");

            Console.ReadLine();

        }
    }

    public class MySingletonClass
    {
        private static MySingletonClass instance = null;

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

        public string TestString { get; set; }
        
    }
}

Here you notice that we don't new up the MySingletonClass in main, but just set it to the instance value. The class itself will handle substantiating itself. If the value of the instance is null, then the code will new up the class and set the private instance variable to the new instance, otherwise it will just return it's referenced instance value. This is a very simple implantation and probably not the most thread-safe implementation of the pattern, but the simplest form of the pattern for learning purposes.

Here is a simple but thread-safe version of the pattern

 
public class MySingletonClass
{ private static MySingletonClass instance = null; private static readonly object myLock = new object(); public static MySingletonClass Instance { get { lock (myLock) { if (instance == null) { instance = new MySingletonClass(); } return instance; } } } public string TestString { get; set; } }

There are several other ways to go about implementing this pattern, but the basics are laid out here.  As I said in the beginning, this and other design patterns were created for good reason and we should use them. Why try and reinvent the wheel.  They help solve issues in our code so we don't have to make the same mistakes. So once again use them.

Add comment

Loading