C# Design Patterns: #1 – The Singleton

SingletonThe Singleton is one of the simpler design patterns. As the name suggests, the Singleton creates a single, static, globally accessible class. It is an alternative to global variables and static classes, each which come with their own limitations.

Singletons are excellent areas to store global information, such as database connection strings, system paths, and parsed XML configurations. In addition, Singletons can be used to store global image caches so that images can be shared between functions.

Wherever possible, use a Singleton to store system-wide data, as opposed to static or global variables. The basic code behind a Singleton is as follows:

public class G
  private static readonly G instance = new G();
  public static G Instance { get { return instance; } } //Global entry point
  static G() { }
  public G() { /* Insert Constructor Here */ }
  public string ConString = ""; //Sample Member Variable
  public SqlConnection GetConnection() { } //Sample Method

There is one static variable in a Singleton: the reference to its instantiated class. Class member variables and functions of a Singleton are referenced in the following form:

G.Instance.ConString //Member Variable
G.Instance.GetConnection() //Method

In the implementation of the Singleton, “G.Instance.” can be shortened to “G.I.” to reduce repetitive typing. If this shorter syntax is used, developers should comment the Singleton well to explain its functionality and help future developers understand the code.

As the application evolves, the Singleton will pay back dividends in ease of maintenance and improved architecture. As opposed to global variables, the Singleton keeps all relevant information in one place, so that variables are not scattered throughout the software. Simple query operations can show all instances of Singleton use, and debugging is significantly simplified.

Beginning programmers may be drawn to static classes. While static classes are an improvement over global variables, they are less flexible than Singleton classes. Instead of controlling initialization and destruction, the programmer forfeits control to the interpreter through the use of static classes. In addition, static classes have limited capability to subclass, and don’t allow more than one instance of the class if at some point it will be necessary (i.e. in a multi-hosted application).

Singletons can provide all the functionality of static classes and more. Since the global data cache is an essential system component, and it will often undergo significant change throughout the lifecycle of the software, it is always a good idea to invest in the extra four lines of code and utilize the more powerful Singleton construct.

Written by Andrew Palczewski

About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.

RSS Twitter LinkedIn Facebook Email

2 thoughts on “C# Design Patterns: #1 – The Singleton”

  1. Your singleton does not look like a singleton as it has a public constructor: this means that anyone can instanciate this G class ; it should be private. Also, the G class should be sealed.

  2. Thanks for sharing the alternate implementation Matf! You’re right that the sealed class and private constructor go hand-in-hand. In practice, I prefer this implementation of the singleton; it gives more flexibility for inheritance in advanced scenarios, such custom overrides of the singleton for client implementations.

    If you want to make sure that novice users of your class don’t have the capability to make mistakes, such as create private instances of the class, then the sealed class / private constructor is a great way to go!

Leave a Reply

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