473,581 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8286
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.publi c.dotnet.langua ges.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 SingletonMyObje ct 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.publi c.dotnet.langua ges.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******@discu ssions.microsof t.com> wrote in message
news:F8******** *************** ***********@mic rosoft.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 interrelationsh ip 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******@discu ssions.microsof t.com> wrote in message
news:F8******** *************** ***********@mic rosoft.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.cshar p.

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

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

Similar topics

3
2473
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 what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL...
13
3049
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 code looks like this: public sealed class Reference { private static readonly Reference instance = new Reference(); // Make the default...
4
1405
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 example for using the singleton object. Regards, Srini
13
739
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 created within the getInstance() method. The classes which are derived from the Singleton class, have to implement a static constructor, where they...
9
14132
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 project (thats why it is in a lib). For the database A I created a singleton class AProxy and for the database B the same as BProxy. Initializing and...
2
6316
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 kind of inheritance: singletonObj = new function() { this.prop = true; } Here are two ways to create a singleton with inheritance:
2
2410
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 class. I did have a look at the Microsoft "Patterns and Practices" article "Implementing Singleton in C#"...
5
2730
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 in it, all my classes have to re-compile. But I am planning to add more options often during development. I tried to solve it through a forward...
29
1711
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
7869
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8151
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8306
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8176
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5677
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5365
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3830
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1139
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.