<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Introduction to the Singleton Pattern in VB.NET</title>
	<link>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/</link>
	<description>Articles on VB.NET and Software Development Team Leadership</description>
	<pubDate>Sat, 22 Nov 2008 07:31:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: Antonio Cavallaro</title>
		<link>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-1139</link>
		<author>Antonio Cavallaro</author>
		<pubDate>Thu, 19 Jun 2008 05:52:24 +0000</pubDate>
		<guid>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-1139</guid>
		<description>Pardon, my personal thread-safe singleton generic object:

&lt;code&gt;
''' 
''' Singleton generic thread safe
''' 
''' 
''' Codebase by A.Cavallaro
Public Class Singleton(Of T As {Class, New})
    Private Shared ReadOnly l_ThreadSafeIstance As New T

    ''' 
    ''' Ritorna l'istanza thread-safe del singleton
    ''' 
    ''' 
    ''' 
    Public Shared Function GetInstance() As T
        Return l_ThreadSafeIstance
    End Function

    ''' 
    ''' Nessun costruttore per singleton
    ''' 
    ''' 
    Private Sub New()
    End Sub
End Class
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Pardon, my personal thread-safe singleton generic object:</p>
<p><code><br />
'''<br />
''' Singleton generic thread safe<br />
'''<br />
'''<br />
''' Codebase by A.Cavallaro<br />
Public Class Singleton(Of T As {Class, New})<br />
    Private Shared ReadOnly l_ThreadSafeIstance As New T</p>
<p>    '''<br />
    ''' Ritorna l'istanza thread-safe del singleton<br />
    '''<br />
    '''<br />
    '''<br />
    Public Shared Function GetInstance() As T<br />
        Return l_ThreadSafeIstance<br />
    End Function</p>
<p>    '''<br />
    ''' Nessun costruttore per singleton<br />
    '''<br />
    '''<br />
    Private Sub New()<br />
    End Sub<br />
End Class<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Antonio Cavallaro</title>
		<link>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-1138</link>
		<author>Antonio Cavallaro</author>
		<pubDate>Thu, 19 Jun 2008 05:51:11 +0000</pubDate>
		<guid>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-1138</guid>
		<description>This is my code for full thread-safety:

''' 
''' Singleton generic thread safe
''' 
''' 
''' Codebase by A.Cavallaro
Public Class Singleton(Of T As {Class, New})
    Private Shared ReadOnly l_ThreadSafeIstance As New T

    ''' 
    ''' Ritorna l'istanza thread-safe del singleton
    ''' 
    ''' 
    ''' 
    Public Shared Function GetInstance() As T
        Return l_ThreadSafeIstance
    End Function

    ''' 
    ''' Nessun costruttore per singleton
    ''' 
    ''' 
    Private Sub New()
    End Sub
End Class</description>
		<content:encoded><![CDATA[<p>This is my code for full thread-safety:</p>
<p>&#8221;&#8217;<br />
&#8221;&#8217; Singleton generic thread safe<br />
&#8221;&#8217;<br />
&#8221;&#8217;<br />
&#8221;&#8217; Codebase by A.Cavallaro<br />
Public Class Singleton(Of T As {Class, New})<br />
    Private Shared ReadOnly l_ThreadSafeIstance As New T</p>
<p>    &#8221;&#8217;<br />
    &#8221;&#8217; Ritorna l&#8217;istanza thread-safe del singleton<br />
    &#8221;&#8217;<br />
    &#8221;&#8217;<br />
    &#8221;&#8217;<br />
    Public Shared Function GetInstance() As T<br />
        Return l_ThreadSafeIstance<br />
    End Function</p>
<p>    &#8221;&#8217;<br />
    &#8221;&#8217; Nessun costruttore per singleton<br />
    &#8221;&#8217;<br />
    &#8221;&#8217;<br />
    Private Sub New()<br />
    End Sub<br />
End Class</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jfrankcarr</title>
		<link>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-323</link>
		<author>jfrankcarr</author>
		<pubDate>Fri, 14 Sep 2007 18:56:42 +0000</pubDate>
		<guid>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-323</guid>
		<description>Thanks James,

Threading certainly does throw a whole new wrinkle on things. The article you linked to is very informative and helpful.

To implement the second version, simple thread-safety, mentioned in the linked article, my code example should look like this, if I followed it correctly:

&lt;pre&gt;Public Class MySingleton

    Private Shared _thisInstance As MySingleton
    Private Shared PadLock As New Object

    Protected Sub New()
        'initialization code goes here
    End Sub

    Public Shared Function GetSingleton() As MySingleton
        '
        ' Prevents multiple threads from creating 
        ' separate instances
        '
        SyncLock PadLock
            '
            ' initialize object if it hasn't already been done
            '
            If _thisInstance Is Nothing Then
                _thisInstance = New MySingleton
            End If
            '
            ' return the initialized instance
            '
            Return _thisInstance
        End SyncLock
    End Function

End Class&lt;/pre&gt;

James' code would be the fifth version, fully lazy instantiation, is that correct?

Thanks again for everyone's input.</description>
		<content:encoded><![CDATA[<p>Thanks James,</p>
<p>Threading certainly does throw a whole new wrinkle on things. The article you linked to is very informative and helpful.</p>
<p>To implement the second version, simple thread-safety, mentioned in the linked article, my code example should look like this, if I followed it correctly:</p>
<pre>Public Class MySingleton

    Private Shared _thisInstance As MySingleton
    Private Shared PadLock As New Object

    Protected Sub New()
        'initialization code goes here
    End Sub

    Public Shared Function GetSingleton() As MySingleton
        '
        ' Prevents multiple threads from creating
        ' separate instances
        '
        SyncLock PadLock
            '
            ' initialize object if it hasn't already been done
            '
            If _thisInstance Is Nothing Then
                _thisInstance = New MySingleton
            End If
            '
            ' return the initialized instance
            '
            Return _thisInstance
        End SyncLock
    End Function

End Class</pre>
<p>James&#8217; code would be the fifth version, fully lazy instantiation, is that correct?</p>
<p>Thanks again for everyone&#8217;s input.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jamesewelch</title>
		<link>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-322</link>
		<author>jamesewelch</author>
		<pubDate>Fri, 14 Sep 2007 18:23:00 +0000</pubDate>
		<guid>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-322</guid>
		<description>Oh.. Also, you might want to talk about using a lock to make sure there isn't two different instantiations of the singleton that happen at the same time. 

example in c#
http://www.yoda.arachsys.com/csharp/singleton.html</description>
		<content:encoded><![CDATA[<p>Oh.. Also, you might want to talk about using a lock to make sure there isn&#8217;t two different instantiations of the singleton that happen at the same time. </p>
<p>example in c#<br />
<a href="http://www.yoda.arachsys.com/csharp/singleton.html" rel="nofollow">http://www.yoda.arachsys.com/csharp/singleton.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jamesewelch</title>
		<link>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-321</link>
		<author>jamesewelch</author>
		<pubDate>Fri, 14 Sep 2007 18:19:41 +0000</pubDate>
		<guid>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-321</guid>
		<description>Also, the VB.NET Module is implemented as a Singleton, it just doesn't use lazy instantiation such as this example does. So that makes it just a class with all properties/methods declared as static/shared.

&lt;code&gt;
Public Module MySingleton

End Module
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Also, the VB.NET Module is implemented as a Singleton, it just doesn&#8217;t use lazy instantiation such as this example does. So that makes it just a class with all properties/methods declared as static/shared.</p>
<p><code><br />
Public Module MySingleton</p>
<p>End Module<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jfrankcarr</title>
		<link>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-318</link>
		<author>jfrankcarr</author>
		<pubDate>Fri, 14 Sep 2007 16:40:42 +0000</pubDate>
		<guid>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-318</guid>
		<description>Thanks Joe. I reformatted your code for readability.

Yes. That would work for creating a generic Singleton object. The only limitation is that you can only use objects that have a Public New that takes no parameters. String would be an example of an object that would not work although you could wrapper it in another class easily enough if you wanted.

The interesting thing about this code example is that you can use this Singleton provider to create a Singleton version of a class without having to have the code in the class itself.

Thanks again for a great example.</description>
		<content:encoded><![CDATA[<p>Thanks Joe. I reformatted your code for readability.</p>
<p>Yes. That would work for creating a generic Singleton object. The only limitation is that you can only use objects that have a Public New that takes no parameters. String would be an example of an object that would not work although you could wrapper it in another class easily enough if you wanted.</p>
<p>The interesting thing about this code example is that you can use this Singleton provider to create a Singleton version of a class without having to have the code in the class itself.</p>
<p>Thanks again for a great example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-316</link>
		<author>Joe</author>
		<pubDate>Fri, 14 Sep 2007 15:19:21 +0000</pubDate>
		<guid>http://vbnotebookfor.net/2007/09/13/introduction-to-the-singleton-pattern-in-vbnet/#comment-316</guid>
		<description>Would something like this do it?
&lt;pre&gt;
Public Class Singleton(Of T As {Class, New})
	Public Shared ReadOnly Property Instance() As T
		Get
			Return SingletonCreator._instance
		End Get
	End Property

	Class SingletonCreator
		Shared Sub New()
		End Sub

		Friend Shared ReadOnly _instance As New T()
	End Class
End Class
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Would something like this do it?</p>
<pre>
Public Class Singleton(Of T As {Class, New})
	Public Shared ReadOnly Property Instance() As T
		Get
			Return SingletonCreator._instance
		End Get
	End Property

	Class SingletonCreator
		Shared Sub New()
		End Sub

		Friend Shared ReadOnly _instance As New T()
	End Class
End Class
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
