473,382 Members | 1,639 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,382 software developers and data experts.

C#: Singleton Pattern with inheritance?

I woundered if the following would be possible:

I want to create an abstract Singleton class, which implements the singleton
behaviour with the limitation, that the unique object will not be created
within the getInstance() method.
The classes which are derived from the Singleton class, have to implement a
static constructor, where they load an instance into the static unique
variable.

Now I have to problems with that:

1. Is it possible to create an instance of a class inside its own static
constructor?
2. The static unique variable is inside the base class, so it would be the
same for all the derived classes and the whole idea is just stupid. Or has
every derived class its own static unique?

thanks a lot...

Stampede
Jul 26 '05 #1
13 8244
Stampede,

Not that you are not welcome here and not that you cannot get an answer
here.

FYI: one of the most active developers newsgroups on InterNet is
Microsoft.public.dotnet.languages.csharp

In that newsgroup are much more people who can and will answer your C#
questions and review the answers from others.

I hope this helps,

Cor
Jul 26 '05 #2
Jan
It's about 5 lines to make somthing into a Singleton so I think you
should just write them out.

But more importantly, if you wanted to make a Form a Singleton you need
to make SingletonForm. If you wanted to make MyClass into a singleton
you'd need SingletonMyObject and so forth so you'd just get a
proliferation of specializations.

Just my 2 cents,
Jan

Jul 26 '05 #3
I'm sorry for interrupting your newsgroup with my stupid, wrong-placed
question. I will move to the newsgroup you told me, which I searched for, but
coudn't find, and hope that the people there will be a little more friendly,
because I always try to stay friendly, even if someone does something stupid,
what mostly happens with new people, as I am one.

Anyway, thanks for telling me the right newsgroup and sorry for my bad
English, which may have disturbed you too.

greetings

Stampede

"Cor Ligthert [MVP]" wrote:
Stampede,

Not that you are not welcome here and not that you cannot get an answer
here.

FYI: one of the most active developers newsgroups on InterNet is
Microsoft.public.dotnet.languages.csharp

In that newsgroup are much more people who can and will answer your C#
questions and review the answers from others.

I hope this helps,

Cor

Jul 26 '05 #4
Stampede,

Can you explain to me what was not friendly, your message was it absolute
not, while I only intended to give you the best help you could get.

Cor
Jul 26 '05 #5
Hi Stampede:

Jon Skeet has an excellent discussion on the pattern here
http://www.yoda.arachsys.com/csharp/singleton.html . I'm not positive
about the GetInstance but I believe that will cover it.
"Stampede" <St******@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
I woundered if the following would be possible:

I want to create an abstract Singleton class, which implements the
singleton
behaviour with the limitation, that the unique object will not be created
within the getInstance() method.
The classes which are derived from the Singleton class, have to implement
a
static constructor, where they load an instance into the static unique
variable.

Now I have to problems with that:

1. Is it possible to create an instance of a class inside its own static
constructor?
2. The static unique variable is inside the base class, so it would be the
same for all the derived classes and the whole idea is just stupid. Or has
every derived class its own static unique?

thanks a lot...

Stampede

Jul 26 '05 #6
Cor Ligthert [MVP] wrote:
Stampede,

Can you explain to me what was not friendly, your message was it
absolute not, while I only intended to give you the best help you
could get.

Cor


Instead of whining about which newsgroup is best, you also could have
helped the guy out, which probably would have taken even less time. I
think thats what bothered the person.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jul 26 '05 #7
Stampede wrote:
I woundered if the following would be possible:

I want to create an abstract Singleton class, which implements the
singleton behaviour with the limitation, that the unique object will
not be created within the getInstance() method.
The classes which are derived from the Singleton class, have to
implement a static constructor, where they load an instance into the
static unique variable.

Now I have to problems with that:

1. Is it possible to create an instance of a class inside its own
static constructor?
sure. This is done in classes which are threadsafe and have to share
their functionality in both static and non-static environments.
2. The static unique variable is inside the base class, so it would
be the same for all the derived classes and the whole idea is just
stupid. Or has every derived class its own static unique?


a static method can't reach 'base', as 'base' isn't allowed in a
static method, as there's no instance.

so you have a class A which contains a static variable which holds the
unique instance and then you have a class B which you instantiate (and
which derives from your base class) and which reference you store in
the static variable. You do all this in the static constructor of A,
and expose either static methods from A which hide B's instance, OR
offer a getinstance method or similar which return the instance of B,
located in the static member variable.

Frans

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jul 26 '05 #8
Hello Stampede,

(Cor didn't mean any harm. Please forgive).

As to your problem:
I'm not sure why you'd want the object to be created in the constructor.
Many would suggest that, in the RARE case where a Singleton is justified,
that creating the object later is better. While I can see cases for the
other, I'd hate to encapsulate that in a base class.

I haven't seen a base class for Singletons that worked all that well,
because of the interrelationship between the static and the non-static bits
of the pattern. I would add "thankfully."

Singleton is one of those patterns that can be done well in rare situations
and can be badly abused in frequent situations. I'd rather we didn't give
to the world a way to make Singletons even easier to write. They are bad
enough as they are.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Stampede" <St******@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
I woundered if the following would be possible:

I want to create an abstract Singleton class, which implements the
singleton
behaviour with the limitation, that the unique object will not be created
within the getInstance() method.
The classes which are derived from the Singleton class, have to implement
a
static constructor, where they load an instance into the static unique
variable.

Now I have to problems with that:

1. Is it possible to create an instance of a class inside its own static
constructor?
2. The static unique variable is inside the base class, so it would be the
same for all the derived classes and the whole idea is just stupid. Or has
every derived class its own static unique?

thanks a lot...

Stampede

Jul 26 '05 #9
Frans,

I used this

FYI .................................................. ................
For your information. That was the only purpose of my answer.

I did not say that he had to go there.

There are enough C# specialist active in this newsgroup however not as much
as in the newsgroup languages.csharp.

What I wrote more or less as well

What is wrong with giving this information, I think that it is one of the
purposes of a newsgroup to give somebody a good lead to an answer on his
problem.

I could have searched this newsgroup of course as well for a good C# answer,
I know that I than can reference to tons. However I would than have used the
CSharp language newsgroup because in that is this question even more done.
However I was sure that somebody would answer this question after my
message. Sometimes you learn in a newsgroup.

:-)

Cor
Jul 26 '05 #10
Cor Ligthert [MVP] wrote:
Frans,

I used this

FYI .................................................. ................
For your information. That was the only purpose of my answer.

I did not say that he had to go there.

There are enough C# specialist active in this newsgroup however not
as much as in the newsgroup languages.csharp.

What I wrote more or less as well

What is wrong with giving this information, I think that it is one of
the purposes of a newsgroup to give somebody a good lead to an answer
on his problem.

I could have searched this newsgroup of course as well for a good C#
answer, I know that I than can reference to tons. However I would
than have used the CSharp language newsgroup because in that is this
question even more done. However I was sure that somebody would
answer this question after my message. Sometimes you learn in a
newsgroup.


I know your intentions are good and just meant to point out an even
more on-topic newsgroup for the person you're replying to, but please
consider that by doing so, your text can and often will be interpreted
as 'rude' and 'let me teach you a lesson, sonny, you're babbling
offtopic here, take a hike'.

It's therefore better to invest the time spend on either answering the
question at hand or leave it up to another person who does know more on
the matter as it's off topic (in your eyes). :)

FB
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jul 27 '05 #11
Cor - I'm not trying to criticize you either but to be honest, there have
been many times that people have taken comments like that the wrong way. I
know as well as anybody that your intentions are noble but there have been
many occassions where people have taken such statements the wrong way. And
I don't mean just from you. There was a guy in the VB group a few weeks ago
that went nuts, blasting all MVPS, because someone pointed out to him the
rules of the newsgroup. A lot of people (and I know more than a few
personally) take such comments as rude and derisive irrespective of thier
intent. If I'm not mistaken, that's how the whole debacle between H.W.
and F.C. started. If nothing else, I'd probably change the way I phrased it
just to eliminate the possibility of confusion to something like "Hopefully
someone in this newsgroup will be able to answer your question, but if not,
the ________ newsgroup has a few more people in it that specialize in that
area, so if you can't get it answered here you may find an answer there as
well." Please know that I'm not criticising you or your intentions - I've
known you long enough to understand where you're coming from - I simply
chimed in b/c more than a few people have taken it the wrong way and I was
just hoping to eliminate some confusion ;-)

-----

BTW, (ask Frans or Roland) - considering how terrible my Dutch is, if I
answered 1/2 the questions in a Dutch newsgroup as you do in the English
ones, I'd probably have the entire Netherlands mad at me - but slowly I'm
learning ;-)
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:eu**************@TK2MSFTNGP10.phx.gbl...
Frans,

I used this

FYI .................................................. ................
For your information. That was the only purpose of my answer.

I did not say that he had to go there.

There are enough C# specialist active in this newsgroup however not as
much as in the newsgroup languages.csharp.

What I wrote more or less as well

What is wrong with giving this information, I think that it is one of the
purposes of a newsgroup to give somebody a good lead to an answer on his
problem.

I could have searched this newsgroup of course as well for a good C#
answer, I know that I than can reference to tons. However I would than
have used the CSharp language newsgroup because in that is this question
even more done. However I was sure that somebody would answer this
question after my message. Sometimes you learn in a newsgroup.

:-)

Cor

Jul 27 '05 #12
Bill,

I did it always in the way you wrote however, than I saw a message from
Armin who used that FYI.

I found it nice so took it over. I go back to my old behaviour again.

:-)

Cor
Jul 27 '05 #13
In no way am I disrespecting A.Z., H.W. or anyone else - but going back to
your old way is probably best - at least in this regard ;-)
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:uP**************@TK2MSFTNGP09.phx.gbl...
Bill,

I did it always in the way you wrote however, than I saw a message from
Armin who used that FYI.

I found it nice so took it over. I go back to my old behaviour again.

:-)

Cor

Jul 27 '05 #14

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

Similar topics

3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
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...
4
by: Srini | last post by:
Hi , Can anyone tell me when I can use singleton pattern. Will it be good for implementing the dataaccess Layer. Will it be usefull for the buisness object layer . Could you give me a practicle...
13
by: Stampede | last post by:
I woundered if the following would be possible: I want to create an abstract Singleton class, which implements the singleton behaviour with the limitation, that the unique object will not be...
9
by: Marcel Hug | last post by:
Hallo NG ! I Have a little question about inheritance of a singleton class. In my application i have a Database-Connection Lib, in which I would šlike to connect different databases of the same...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
2
by: baba | last post by:
Hi all, I'm quite new to C#. I am trying to implement some basics reusable classes using this language and the .NET Framework technology. What I'm trying to do now is to implement a singleton...
5
by: Markus Dehmann | last post by:
I need a Singleton for general program options so that all classes can access it. I use the code below (adapted from the Wikipedia singleton example). But the problem is if I change one variable...
29
by: Ugo | last post by:
Hi guys, how do you make a singleton access class? Do you know a better way of this one: var singletonClass = (function( ) { // Private variable var instance = null;
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.