M
Michel van den Berg
Hello,
I tried to implement the GoF Singleton Pattern. What do you think?
Public MustInherit Class Singleton(Of T As New)
Public Sub New()
If SingletonCreator.Creating = False Then Throw New
SingletonException(Of T)
End Sub
Public Shared ReadOnly Property Instance() As T
Get
Return SingletonCreator.Instance
End Get
End Property
Private Class SingletonCreator
Private Shared instanceValue As T
Public Shared ReadOnly Property Instance() As T
Get
If instanceValue Is Nothing Then
Creating = True
instanceValue = New T
Creating = False
End If
Return instanceValue
End Get
End Property
Private Shared creatingValue As Boolean = False
Public Shared Property Creating() As Boolean
Get
Return creatingValue
End Get
Set(ByVal value As Boolean)
creatingValue = value
End Set
End Property
End Class
End Class
The client works like:
Public Class SingletonClient
Inherits Singleton(Of SingletonClient)
End Class
That's all! Greetings,
mvb
I tried to implement the GoF Singleton Pattern. What do you think?
Public MustInherit Class Singleton(Of T As New)
Public Sub New()
If SingletonCreator.Creating = False Then Throw New
SingletonException(Of T)
End Sub
Public Shared ReadOnly Property Instance() As T
Get
Return SingletonCreator.Instance
End Get
End Property
Private Class SingletonCreator
Private Shared instanceValue As T
Public Shared ReadOnly Property Instance() As T
Get
If instanceValue Is Nothing Then
Creating = True
instanceValue = New T
Creating = False
End If
Return instanceValue
End Get
End Property
Private Shared creatingValue As Boolean = False
Public Shared Property Creating() As Boolean
Get
Return creatingValue
End Get
Set(ByVal value As Boolean)
creatingValue = value
End Set
End Property
End Class
End Class
The client works like:
Public Class SingletonClient
Inherits Singleton(Of SingletonClient)
End Class
That's all! Greetings,
mvb