Parameters in a Singleton depend entirely on the purpose of the
parameters. It may mean a singleton that needs initialization or you
could be talking about a different pattern--Flyweight.
If the parameters are intended to initialize the singleton and should
only be provided once, or should match if provided a second time, and
the singleton will still be a true singleton in that there will always
only be a single object whose reference won't be changed, then two
Instance properties should be implemented. One has no params and
returns the existing object only ifit has already been initialized
(throw error otherwise). The second accepts the params and if it's
the first call it initializes and returns the singleton. If it's a
subsequent call it should verify that the parameters match. See
ParameterizedSingleton below.
If the parameters are intended to represent a unique object where
there can be multiple instances of the object based on parameters but
all instances for the same parameters should be identical then the
object is not a Singleton but a Flyweight. In this case the shared
property is a collection of objects and not a single instance and is
indexed by one or both of the parameters. See Flyweight below.
HTH,
Sam
Public NotInheritable Class ParameterizedSingleton
Private _username As String
Private _password As String
Private Shared _instance As ParameterizedSingleton
Private Sub New(ByVal username As String, ByVal password As String)
_username = username
_password = password
End Sub
' Singleton getters. Use Instance(username,password) first
' time to initialize the singleton and use Instance() on
' subsequent calls to use the existing singleton.
Public Shared ReadOnly Property Instance() As
ParameterizedSingleton
Get
If _instance Is Nothing Then
Throw New Exception("ParemeterizedSingleton has not been
properly initialized.")
End If
Return _instance
End Get
End Property
Public Shared ReadOnly Property Instance(ByVal username As String,
ByVal password As String) As ParameterizedSingleton
Get
If _instance Is Nothing Then
_instance = New ParameterizedSingleton(username, password)
Else
If Not _instance.UserName = username OrElse _
Not _instance.Password = password Then
Throw New Exception("ParemeterizedSingleton has already
been initialized.")
End If
End If
Return _instance
End Get
End Property
Public ReadOnly Property UserName() As String
Get
Return _username
End Get
End Property
Public ReadOnly Property Password() As String
Get
Return _password
End Get
End Property
End Class
Public NotInheritable Class Flyweight
Private _username As String
Private _password As String
Private Shared _instances As New Hashtable
Private Sub New(ByVal username As String, ByVal password As String)
_username = username
_password = password
End Sub
Public Shared ReadOnly Property Instance(ByVal username As String,
ByVal password As String) As Flyweight
Get
Dim obj As Flyweight
If _instances.Contains(username) Then
obj = DirectCast(_instances(username), Flyweight)
If obj.Password <> password Then
Throw New Exception("User '" + username + "' is already
initialized.")
End If
Else
obj = New Flyweight(username, password)
_instances(username) = obj
End If
Return obj
End Get
End Property
Public Shared ReadOnly Property Instance(ByVal username As String)
As Flyweight
Get
If Not _instances.Contains(username) Then
Throw New Exception("User '" + username + "' not
initialized.")
End If
Return DirectCast(_instances(username), Flyweight)
End Get
End Property
Public ReadOnly Property UserName() As String
Get
Return _username
End Get
End Property
Public ReadOnly Property Password() As String
Get
Return _password
End Get
End Property
End Class
On Thu, 24 Feb 2005 23:07:35 -0000, "Paul Bromley"
<fl*******@dsl.pipex.com> wrote:
Forgive my ignorance on this one as I am trying to use a Singleton class. I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class have
a number of paramterised constructors enabling me to pass in parameters or
not?
I am trying to use the following to send in a parmeter to a constructor, but
getting an error with it. I have a feeling that I am not able to do this. Is
there any other way that I can do this.
Dim sPatName As String = "Green"
Dim EMP As CEmPacc = CEmPacc.Create(sPatName)
Class myApp.CEmPacc cannot be indexed because it has no default property
Many thanks
Paul Bromley
B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.