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, and provides a global point of access to it.
VB - Dim mySingleton As SingletonSample = SingletonSample.Instance
C# - SingletonSample mySingleton = SingletonSample.Instance();
Revision number 12, Sunday, June 06, 2010 9:20:30 AM by ahsanm.m
You must Login to comment.
|
Fri, Mar 28, 2008 8:25 AM
by kulanthaivelu
|
Good Article. Now I got basic idea about singleton pattern. Thanks
|
|
Fri, Apr 11, 2008 1:40 AM
by shariff
|
cool!!!, thanks for the info, really helps me to understand SINGLETON.
|
|
Fri, Jun 13, 2008 4:34 PM
by dragthor
|
Is this thread safe?
|
|
Sun, Jun 15, 2008 7:46 PM
by mbanavige
|
since the singleton is created via a static initializer, its creation is thread-safe.
|
|
Fri, Aug 8, 2008 4:39 AM
by ojosdegris
|
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#
|
|
Thu, Dec 11, 2008 2:14 AM
by ashrafur
|
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/
|
|
Thu, Dec 11, 2008 9:58 PM
by mbanavige
|
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.
|
|
Thu, Dec 18, 2008 3:17 AM
by sirdneo
|
HOw to implement multi lock version of singeltoin
|
|
Mon, Apr 13, 2009 6:34 AM
by zimmybansal
|
Nice article for understanding basic implementation of Singleton! Congratz!!!
|
|
Fri, Apr 17, 2009 3:00 PM
by joseluis2411
|
good article
|
|
Thu, Nov 26, 2009 5:09 AM
by kalit.sikka@hotmail.com
|
Nice one, But it could be more impressive if it in C#
|
|
Wed, Feb 3, 2010 4:31 AM
by dpant
|
discussing thread safety issues for Singleton means you do not understand what Singleton is and how it works. In other words, Singleton raises no thread safety issues, by nature.
|
|
Thu, Feb 4, 2010 2:09 PM
by beharavenkata@gmail.com
|
good article
|
|
Sat, Feb 6, 2010 6:06 PM
by Daniel Popovic
|
Good article. See this http://msdn.microsoft.com/en-us/library/ms998558.aspx for thread-safe.
|
|
Mon, May 31, 2010 5:17 AM
by idreesbari
|
niece idea about Singleton in short description
|