Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Use Singletonprovider instead of the Singleton pattern

From Wiki

Jump to: navigation, search

We all know the singleton pattern for VB.NET. The singleton pattern is probably the most ised and overused pattern there is, but I won't go into that.

For those of you who forgot. This is a threadsafe singleton implementation.

  1. Public Class Class1
  2.  
  3. #Region " Singleton Pattern Support "
  4.     ''' <summary>
  5.     ''' Object to make it threadsafe
  6.     ''' </summary>
  7.     ''' <remarks></remarks>
  8.     Private Shared _SyncRoot As New Object
  9.     ''' <summary>
  10.     ''' Object that holds an instance of this class
  11.     ''' </summary>
  12.     ''' <remarks></remarks>
  13.     Private Shared _Instance As Class1
  14.  
  15.     ''' <summary>
  16.     ''' Make the constructor private so the have to use then Instance property to get an instance of this class
  17.     ''' </summary>
  18.     ''' <remarks></remarks>
  19.     Private Sub New()
  20.  
  21.     End Sub
  22.  
  23.     ''' <summary>
  24.     ''' Returns an Instance of this class
  25.     ''' </summary>
  26.     ''' <value></value>
  27.     ''' <returns>An instance of class1</returns>
  28.     ''' <remarks></remarks>
  29.     Public Shared ReadOnly Property Instance() As Class1
  30.         Get
  31.             SyncLock _SyncRoot
  32.                 If _Instance Is Nothing Then
  33.                     _Instance = New Class1
  34.                 End If
  35.                 Return _Instance
  36.             End SyncLock
  37.         End Get
  38.     End Property
  39. #End Region
  40.  
  41. End Class

But this adds extra code to your class. And this is something to I would avoid. And Do we always need the same instance of this class? Perhaps it could change in the future. So I like the use of singleton provider better.

This is what it looks like

  1. Public Class SingletonProvider(Of itemtype As {New})
  2.  
  3.     Public Sub New()
  4.     End Sub
  5.  
  6.  
  7.     Public Shared ReadOnly Property Instance() As itemtype
  8.  
  9.         Get
  10.             Return SingletonCreator.instance
  11.         End Get
  12.  
  13.     End Property
  14.  
  15.     Class SingletonCreator
  16.         Shared Sub New()
  17.         End Sub
  18.  
  19.         Public Shared ReadOnly instance As itemtype = New itemtype()
  20.     End Class
  21. End Class

This piece of code comes from [1].

I won't even try to explain it. But if you want to use it you have to make sure your singleton class has a default constructor

This is how you use it

  1. Singletonprovider(Of Class1).Instance.UseMethod

I hope that was usefull?

223 Rating: 2.3/5 (4 votes cast)