Singleton

 Rate It (6)

Singleton 

The Singleton Design Pattern ensures that only a single instance of a given object can exist.

It does this by making the class constructor private so that it [the singleton itself] has full control over when the class instance is created.  In order to gain access to an instance of a class that implements the Singleton pattern, the developer must call a shared/static method of that Singleton class.

A VB example of a Singleton

Public Class SingletonSample

 

    'shared members

    Private Shared _instance As New SingletonSample

 

    Public Shared Function Instance() As SingletonSample

        Return _instance

    End Function

 

    'instance members

    Private Sub New()

        'public instantiation disallowed

    End Sub

 

    'other instance members

    '...

 

End Class

A C# example of a Singleton

public class SingletonSample

{

    //shared members

    private static SingletonSample _instance = new SingletonSample();

 

    public static SingletonSample Instance()

    {

        return _instance;

    }

 

    //instance members

    private SingletonSample()

    {

        //public instantiation disallowed

    }

 

    //other instance members

    //...

}

 

With the Singleton Design Pattern in place, the developer can easily access that single object instance without needing to worry about inadvertently creating multiple instances.

VB - Dim mySingleton As SingletonSample = SingletonSample.Instance

C# - SingletonSample mySingleton = SingletonSample.Instance();

 

Revision number 7, Thursday, November 13, 2008 10:51:14 PM by raklos

Comments

Good Article. Now I got basic idea about singleton pattern. Thanks

cool!!!, thanks for the info, really helps me to understand SINGLETON.

Is this thread safe?

since the singleton is created via a static initializer, its creation is thread-safe.

sorry, have you looked at this, before writing this article? http://msdn.microsoft.com/en-us/library/ms954629.aspx sealed class Singleton { private Singleton() {} public static readonly Singleton Instance = new Singleton(); } i think it's better way to implement singleton in C#

Nice one. Described basic idea very clearly. But developer need to aware about Thread Safe so need to use Locking. Here is a complete example with a description of Thread safe, Synchronous call and Locking. http://ashrafur.wordpress.com/2008/08/13/singleton-instantiate-just-one-object/

Locking is not required as the singleton object in the example was instantiated via a static initializer. The .NET framework guarantees that static initializers are run exactly once. On the other hand, the synclock double-check pattern might appear to be a good pattern but can actually still intermittently create multiple objects due to processor optimizations.

HOw to implement multi lock version of singeltoin

Nice article for understanding basic implementation of Singleton! Congratz!!!

good article

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (14)
  2. codehard (3)
  3. Babunareshnarra (2)
  4. Dungimon (1)
  5. cabhilash (1)
Microsoft Communities