C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: A singleton can simplify complex code. Singletons have a static property that you must access to get the object reference.
Info: The member called _instance is a static instance of the SiteStructure class. This means it only can be created once by the runtime.
ReadonlyAnd: We use this static reference to an actual, regular object. The Instance static property allows easy access to the SiteStructure singleton.
PropertyConstructor: A private constructor means that a class cannot be created anywhere but inside its own methods. It must be accessed through Instance.
ConstructorSealedSingleton implementation: C#
/// <summary>
/// Sample singleton object.
/// </summary>
public sealed class SiteStructure
{
    /// <summary>
    /// This is an expensive resource.
    /// We need to only store it in one place.
    /// </summary>
    object[] _data = new object[10];
    /// <summary>
    /// Allocate ourselves.
    /// We have a private constructor, so no one else can.
    /// </summary>
    static readonly SiteStructure _instance = new SiteStructure();
    /// <summary>
    /// Access SiteStructure.Instance to get the singleton object.
    /// Then call methods on that instance.
    /// </summary>
    public static SiteStructure Instance
    {
        get { return _instance; }
    }
    /// <summary>
    /// This is a private constructor, meaning no outsiders have access.
    /// </summary>
    private SiteStructure()
    {
        // Initialize members here.
    }
}
Differences: There are important differences between the singleton design pattern and the static keyword on classes.
Important: Static classes and singletons both provide sharing of redundant objects in memory, but they differ in usage and implementation.
Static: You can use static classes to store single-instance, global data. The class will usually be initialized lazily, at the last possible moment.
However: You lose control over the exact behavior and static constructors can cause some slowdowns in programs.
Static class example: C#
/// <summary>
/// Static class example. Note the static keyword usage.
/// </summary>
static public class SiteStatic
{
    /// <summary>
    /// The data must be a static member in this example.
    /// </summary>
    static object[] _data = new object[10];
    /// <summary>
    /// C# doesn't define when this constructor is run, but it will likely
    /// be run right before it is used.
    /// </summary>
    static SiteStatic()
    {
        // Initialize all of our static members.
    }
}
And: Unlike static classes, we can use singletons as parameters to methods, or objects.
ParametersObjectUsing singleton as parameter: C#
//
// We want to call a function with this structure as an object.
// Get a reference from the Instance property on the singleton.
//
SiteStructure site = SiteStructure.Instance;
OtherFunction(site); // Use singleton as parameter.
Here: We can use the singleton on any method that accepts the interface. We don't need to rewrite anything over and over again.
Singletons used with interface: C#
/// <summary>
/// Stores signatures of various important methods related to the site.
/// </summary>
public interface ISiteInterface
{
};
/// <summary>
/// Skeleton of the singleton that inherits the interface.
/// </summary>
class SiteStructure : ISiteInterface
{
    // Implements all ISiteInterface methods.
    // Omitted.
}
/// <summary>
/// Here is an example class where we use a singleton with the interface.
/// </summary>
class TestClass
{
    /// <summary>
    /// Sample.
    /// </summary>
    public TestClass()
    {
        // Send singleton object to any function that can take its interface.
        SiteStructure site = SiteStructure.Instance;
        CustomMethod((ISiteInterface)site);
    }
    /// <summary>
    /// Receives a singleton that adheres to the ISiteInterface interface.
    /// </summary>
    private void CustomMethod(ISiteInterface interfaceObject)
    {
        // Use the singleton by its interface.
    }
}