Singleton with parameters?

  • Thread starter Thread starter Simon Hazelton
  • Start date Start date
S

Simon Hazelton

Is it possible in VB.net to have a single or singleton instance of a class
per specific database record.

I'm writing an auction site and have a problem with resolving proxy bids,
effectively I am creating an unlimited amount of "Auctioneers" for the site
as a whole (which is definitely wrong). I could create a singleton
"Auctioneer" Class for the site with a singleton but feel this would be a
performance botleneck.

Ideally I would like to ensure that only one instance of an "Auctioneer"
would be created per "Auction" thus ensuring that all proxy bids are
resolved in an ordered way.

Any ideas on how to resolve this issue or if I can achieve the desired
result using singletons will be greatly appreciated.

Simon Hazelton
 
Found this on the net, hope it helps ?
Regards OHM

The Singleton pattern is implemented in two steps. The first step is to
ensure that every constructor in the class implementing the Singleton
pattern is non-public. All constructors must be protected or private. The
second step is to implement a public factory method-a shared method-that
creates just one instance of the class.
How do a private constructor and a shared factory method ensure that there
is only one instance of the class? The answer is that consumers cannot
invoke non-public constructors; hence they cannot create instances of your
class except in the manner you prescribe. However, member methods can invoke
protected or private methods and can call your non-public constructors. The
result is that you control the number of instances of your class by
controlling access to the constructor.

Public Class Singleton

Private Shared FInstance As Singleton = Nothing

Private Sub New()

End Sub

Public Shared ReadOnly Property Instance()
Get
If (FInstance Is Nothing) Then
FInstance = New Singleton()
End If

Return FInstance
End Get
End Property

End Class

No matter how hard a consumer tries he will not be able to create more than
one instance of the Singleton class. The private shared field FInstance is
used to store the reference to the instance of the Singleton. The
constructor-Sub New-is private; thus Singleton objects cannot be created
directly. Finally, the shared property Instance can be invoked and this will
return an instance of the Singleton class but only create one instance of
the class.

Implementing advanced idioms like the Singleton pattern was difficult, if
not impossible, to do in Visual Basic 6. Fortunately VB.NET has been
promoted to a first class language, and there is very little that we will be
unable to do with the new VB.
 
Simon,

Rather then resort to a singleton class for every autioneer why not use a
combination of a factory pattern and an Identity map. For instance:

Create a factory class that would hand out the appropriate Auctioneer object
depending on the request. The Auctioneer class can only be instantiated by
the factory. The factory could maintain a list of active auctioneers that
would be checked before instantiating a new auctioneer.

You can handle the list of Auctioneers a couple of ways but one way could be
to make the Factory class a Singleton with a Shared collection of
auctioneers. In a multi user environment you will have to ensure that the
method that creates auctioneers is syncronized.

Dan
 
Do I detect some very thinly disguised 'HATE' sentiments here ?, If you've
got something to say CJ, I would rather that you just came out and said it.

OHM

------------------------
 
Back
Top