473,569 Members | 3,015 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
"Implementi ng 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 2409
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******** *************** ***********@mic rosoft.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
"Implementi ng 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.co m>
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
12466
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) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
10
2626
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
1924
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 that either make it abstract base class for it to be derivable or keep them as "concrete" not to be derived at all? Then what's the purpose of...
3
2469
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...
5
5297
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++ Design" or similar books, but I feel there are full of clever guys here who could help me out.
2
1840
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
8938
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
5
1883
by: Amal P | last post by:
Dears, I have a question. class Data { }; class DisableDerive:virtual protected Data // A base class { private:
3
18232
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. ...
0
7930
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
8138
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
7983
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...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
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
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
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.