Singleton

As the name implies, the Singleton is a design pattern that ensures only a single instance of the class is created. When implemented, the designer is making the decision that one instance is either all that’s needed or required. This can ensure all users are viewing and modifying the same data; however, that can also cause race-conditions so synchronicity and proper queueing are required to correctly implement.

A singleton can be implemented in several ways. Here is one implementation in C#. I am running low on time, so I will add more examples later on.

public static class Singleton
{
    private static Singleton instance = null;
    public Singleton() {} 

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

Leave a Reply

Your email address will not be published. Required fields are marked *