469,323 Members | 1,558 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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 7994
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alicia Roberts | last post: by
13 posts views Thread by Robert W. | last post: by
4 posts views Thread by Srini | last post: by
13 posts views Thread by Stampede | last post: by
9 posts views Thread by Marcel Hug | last post: by
2 posts views Thread by Kevin Newman | last post: by
2 posts views Thread by baba | last post: by
5 posts views Thread by Markus Dehmann | last post: by
29 posts views Thread by Ugo | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.