473,325 Members | 2,671 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,325 software developers and data experts.

Singleton Pattern with non-default constructor

Tom
In a web-application I need to read some configuration from a database.
I like to write a configuration class which will be implemented as
thread-safe singleton.

I connect through ODBC to the database. So to be able to do that I need
to pass the Datasource Name.

How can I create a singleton that has a constructor with a parameter?
Standard singleton pattern always use the default constructor. To
provide an init(string datasource_name) method is no real fun, since the
state of the singleton instance needed to be checked whenever used.

Is there any better solution to this?

Tom
Jun 29 '08 #1
2 3900
Tom wrote:
In a web-application I need to read some configuration from a database.
I like to write a configuration class which will be implemented as
thread-safe singleton.

I connect through ODBC to the database. So to be able to do that I need
to pass the Datasource Name.

How can I create a singleton that has a constructor with a parameter?
Standard singleton pattern always use the default constructor. To
provide an init(string datasource_name) method is no real fun, since the
state of the singleton instance needed to be checked whenever used.

Is there any better solution to this?
From the philosophical point of view you could arhue that if the
parameter is not fixed, then it is not really a singleton.

From the practical point of view you would probably just have the
constructor read the name from a config file.

The alternative would be a multipleton, where the GetInstance
method has a string argument and the static instance field
is a Dictionary<string,Xand the GetInstance lookup and create
if necessary.

Arne
Jun 29 '08 #2
If the object returned is state-dependent on the constructor parameter then
this is not a good target for the singleton pattern.

Effectively, the singleton objects state will change for all objects that
have a reference to it.

In this case, and if you really want to use a singleton, a more correct
method would be to maintain the pattern as described by the pattern and
provide a method to set the state.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Tom" <de*@bednarz.chwrote in message
news:ul**************@TK2MSFTNGP04.phx.gbl...
In a web-application I need to read some configuration from a database. I
like to write a configuration class which will be implemented as
thread-safe singleton.

I connect through ODBC to the database. So to be able to do that I need to
pass the Datasource Name.

How can I create a singleton that has a constructor with a parameter?
Standard singleton pattern always use the default constructor. To provide
an init(string datasource_name) method is no real fun, since the state of
the singleton instance needed to be checked whenever used.

Is there any better solution to this?

Tom
Jun 30 '08 #3

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

Similar topics

4
by: Neil Zanella | last post by:
Hello, I would be very interested in knowing how the following C++ multi-instance singleton (AKA Borg) design pattern based code snippet can be neatly coded in Python. While there may be...
10
by: ferdinand.stefanus | last post by:
Hi Could someone tell me what's the difference between these two singleton implementations: First implementation: class Singleton { public:
11
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be...
7
by: Stephen Brown | last post by:
I have some strange behavior on my web server that seems to point to garbage collection. I have a singleton that tracks web activity on my web site. The singleton works great, except that it...
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...
18
by: David | last post by:
Hello, I'm writing a C# class library that may be called by non-.Net applications. The main class of this class libray is a ServicedComponent so that non-.Net application can call this class as a...
15
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
13
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
8
by: Gaensh | last post by:
HI, I have a singleton pattern to acess my database the following is the sample code use to implement singleton pattern public class DataAccessHelper { private static DataAccessHelper instance;...
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.