473,507 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Derivable singleton class

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#"
(http://msdn.microsoft.com/library/de...onInCsharp.asp)
and I found it useful but I still have a problem: what I need is a
thread-safe approach, because I expect to use this piece of code in
multi-threaded applications, but I'd like my class to be not "sealed" because
I would put some generic methods in it and I would add some specific
implementations in derived classes.

In the article above, instead, the class provided to obtain thread-safety is
designed "sealed", so that it doesn't completely suits my needs.

Bottom line: how can I implement a singleton class which is derivable and
thread-safe?
Or am I just thinking the whole thing wrong?

Thanks in advance.

Cheers,
baba.

Mar 29 '06 #1
2 2402
baba,

Is far as thread safety goes I don't think this is an issue if the singleto
design pattern. It is rather problem of the class itself. You need to make
its public members thread safe. The singleto design pattern just guarantees
that only one instance of the class exisits.

Regarding the inheritance of the singleton class it has been discussed in
the ng before. Even though the singleton pattern doesn't state that the
inheritance is not allowed, it is very unlikely. Enabling inheritance opens
backdoors for easily breaking the singleton effect of the pattern. It can be
implemented but the use of it is very limited.

I'd suggest reading Jon Skeet's blog (I hope Jon don't mind me posting this
link ) for an idea how to implemented soemething like this.
http://msmvps.com/blogs/jon.skeet/ar...heritance.aspx

If you look at the code you'll see what I mean by 'limited'
The type of the singleton object is determined upon type loading (which
happens once in the process (AppDomain) life time) You can't changed it
afterwards neither you can create singletons of different derived types in
the same application. If you could it would break the singleton effect.
--
HTH
Stoitcho Goutsev (100)

"baba" <baba@passport_NOSPAM_.com> wrote in message
news:E4**********************************@microsof t.com...
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#"
(http://msdn.microsoft.com/library/de...onInCsharp.asp)
and I found it useful but I still have a problem: what I need is a
thread-safe approach, because I expect to use this piece of code in
multi-threaded applications, but I'd like my class to be not "sealed"
because
I would put some generic methods in it and I would add some specific
implementations in derived classes.

In the article above, instead, the class provided to obtain thread-safety
is
designed "sealed", so that it doesn't completely suits my needs.

Bottom line: how can I implement a singleton class which is derivable and
thread-safe?
Or am I just thinking the whole thing wrong?

Thanks in advance.

Cheers,
baba.

Mar 29 '06 #2
baba <baba@passport_NOSPAM_.com> wrote:

<snip>
Bottom line: how can I implement a singleton class which is derivable and
thread-safe?


You can't, in general. As soon as you can derive from it, it's no
longer a singleton. The one exception is that you could use nested
classes to limit the inheritance.

See
http://msmvps.com/blogs/jon.skeet/ar...leton_inherita
nce.aspx
for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #3

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

Similar topics

7
12457
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
2623
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
11
1920
by: mem | last post by:
Concrete classes are ones that can be instantiated directly, as long as it doesn't have pure virtual function. However, people suggest that "Don't derive from concrete classes." Does this mean...
3
2466
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...
5
5293
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
2
1838
by: Amit | last post by:
please see the following lines of codes : class ABase; // i want to make it non-derivable class Super { private: Super () {}; friend class ABase; };
12
8920
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
5
1877
by: Amal P | last post by:
Dears, I have a question. class Data { }; class DisableDerive:virtual protected Data // A base class { private:
3
18224
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
7223
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,...
0
7319
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,...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7485
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...
0
4702
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...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
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...

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.