473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Singleton with parameters?

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
Nov 20 '05 #1
4 3038
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 Hazelton wrote:
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

Nov 20 '05 #2
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
"Simon Hazelton" <no****@spam.com> wrote in message
news:3f***********************@lovejoy.zen.co.uk.. .
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

Nov 20 '05 #3
Hmmm, that was really helpful to me. thanks OHM!

-CJ
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@sparta.btinternet.com...
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 Hazelton wrote:
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


Nov 20 '05 #4
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

------------------------

CJ Taylor wrote:
Hmmm, that was really helpful to me. thanks OHM!

-CJ
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bq**********@sparta.btinternet.com...
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 Hazelton wrote:
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

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Uwe Mayer | last post by:
Hi, I've been looking into ways of creating singleton objects. With Python2.3 I usually used a module-level variable and a factory function to implement singleton objects. With Python2.4 I...
2
by: davidw | last post by:
As I asked in last post, I want to put some logic in a object and all my webcontrol instance will access that object, the object is responsed to retrieve data from database if the data has not been...
9
by: VidalSasoon | last post by:
I am trying to set up a singleton since it seems like the best solution to my problem. I have a an object that I want to initialize when the program starts then I just want to call it like a...
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
1
by: Diffident | last post by:
Guys, I have been cracking my head over this concept in .NET framework. I have read many posts on this topic but not clear about this and hence I am posting it again. If you have designed...
14
by: Paul Bromley | last post by:
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...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
2
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.