C#: Singleton Pattern with inheritance?

G

Guest

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
 
C

Cor Ligthert [MVP]

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
 
J

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
 
G

Guest

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
 
C

Cor Ligthert [MVP]

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
 
F

Frans Bouma [C# MVP]

Cor said:
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

--
 
F

Frans Bouma [C# MVP]

Stampede said:
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



--
 
N

Nick Malik [Microsoft]

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.
 
C

Cor Ligthert [MVP]

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
 
F

Frans Bouma [C# MVP]

Cor said:
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


--
 
W

W.G. Ryan MVP

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 ;-)
 
C

Cor Ligthert [MVP]

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
 
W

W.G. Ryan MVP

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 ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top