473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Inheritance of a singleton class

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 connetcing to the Database is the same
in both classes (except of the database-path, which is a parameter), just
the Functions are different, because the tables are different.
Now I thought about creating a general DBConnectifity-class (or interface)
to generalize the Initializing and db-connection stuff in this class and
define the specific function in the suc-classes.
I'm new in using patterns and .NET-Programming (OO-Programming) and I'm not
sure, if that work proper. In a little test application I allways get an
compile error, because it is not possible to convert a class2 (subclass of
class1) in a class1 (general singleton class) object by calling
getInstance().

It is possible to inheritance of a singleton class ?
What did I wrong ?

Thanks
Regards
Marcel Hug
Dec 5 '05 #1
9 14098
Marcel Hug wrote:

<snip>
It is possible to inheritance of a singleton class ?


Not generally, no. Singletons rely on the fact that only the singleton
class itself can create an instance. As soon as you can have *one*
derived class creating an instance of itself, you can have *multiple*
derived classes, thus breaking the singleton nature.

You can do various things to ensure at runtime that only one instance
is created (like throwing an exception if a second instance is ever
created), but that's not the same as the normal singleton pattern.

Jon

Dec 5 '05 #2
Jon,

With the risk of getting involved into an endless discussion with you I'd
like to say that I believe singleton design pattern doesn't disallow
inheritance. Singleton guarantees single instance of a type and since
derived class *is* its base type then inheritance is allowed as long as
there is only one instance of the base (singleton) type. In other words only
one instance of the singleton class or any of its derived classes can exist.

How one can implement that? Well this is implementation detail. Probably
some combination between singleton and factory design patterns.


--

Stoitcho Goutsev (100) [C# MVP]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Marcel Hug wrote:

<snip>
It is possible to inheritance of a singleton class ?


Not generally, no. Singletons rely on the fact that only the singleton
class itself can create an instance. As soon as you can have *one*
derived class creating an instance of itself, you can have *multiple*
derived classes, thus breaking the singleton nature.

You can do various things to ensure at runtime that only one instance
is created (like throwing an exception if a second instance is ever
created), but that's not the same as the normal singleton pattern.

Jon

Dec 6 '05 #3
Stoitcho Goutsev (100) [C# MVP] <10*@100.com> wrote:
With the risk of getting involved into an endless discussion with you I'd
like to say that I believe singleton design pattern doesn't disallow
inheritance. Singleton guarantees single instance of a type and since
derived class *is* its base type then inheritance is allowed as long as
there is only one instance of the base (singleton) type. In other words only
one instance of the singleton class or any of its derived classes can exist.

How one can implement that? Well this is implementation detail. Probably
some combination between singleton and factory design patterns.


You can implement that so that at runtime an exception is thrown or
whatever, but I see that as fundamentally different from being able to
see clearly that the *only* thing which can construct an instance is
the class itself (due to a private constructor being the only one
available), and that class *only* creating an instance when none has
been created before.

With the classic singleton pattern, there's nothing I can do to even
*try* to create another instance (reflection aside) unless I change the
singleton's code.

If you allow inheritance, then you can break the pattern by adding
another derived class - suddenly the singleton class itself isn't in
control over what calls its constructor, so it has to add extra run-
time checks.

--
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
Dec 6 '05 #4
Marcel... A singleton pattern can return an instance that implements an
interface. If all concrete types can be abstracted to a generic
interface, then you
can return a "subtype" at runtime.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '05 #5
There's nothing fundamentally wrong with subclassing a class that is
intended to be a singleton.
There are many reasons you might want to do it.
And there are many ways to accomplish it.

Dec 7 '05 #6
> There's nothing fundamentally wrong with subclassing a class that is
intended to be a singleton.
There are many reasons you might want to do it.
And there are many ways to accomplish it.


Not if I'm a good boy and declare my singleton as "public sealed class Singleton".
;)
Dec 7 '05 #7
> For the database A I created a singleton class AProxy and for the database B
the same as BProxy. Initializing and connetcing to the Database is the same
in both classes (except of the database-path, which is a parameter), just
the Functions are different, because the tables are different.
Now I thought about creating a general DBConnectifity-class (or interface)
to generalize the Initializing and db-connection stuff in this class and
define the specific function in the sub-classes.


Before this discussion spirals out of control :-), notice that the OP
isn't asking that the DBConnectivity class be a singleton, only that
its derived classes be singletons. There is nothing in the singleton
pattern that says that the singleton can't inherit from a base class,
or that the various derived classes from a base class can't be
singletons.

That said, the only thing you _can't_ do within the letter of the law
is put the singleton code in the base class, and have inheriting
classes inherit their "singleton-ness" from the base. Neither can you
guarantee that each derived class is a singleton. You have to write the
singleton pattern code in each derived class.

Dec 7 '05 #8
GoogleNewsReaderMan <lo***********@hotmail.com> wrote:
There's nothing fundamentally wrong with subclassing a class that is
intended to be a singleton.
There are many reasons you might want to do it.
And there are many ways to accomplish it.


And that's fine, so long as you then accept that the class is no longer
a singleton.

--
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
Dec 7 '05 #9
Bruce Wood <br*******@canada.com> wrote:
For the database A I created a singleton class AProxy and for the database B
the same as BProxy. Initializing and connetcing to the Database is the same
in both classes (except of the database-path, which is a parameter), just
the Functions are different, because the tables are different.
Now I thought about creating a general DBConnectifity-class (or interface)
to generalize the Initializing and db-connection stuff in this class and
define the specific function in the sub-classes.
Before this discussion spirals out of control :-), notice that the OP
isn't asking that the DBConnectivity class be a singleton, only that
its derived classes be singletons. There is nothing in the singleton
pattern that says that the singleton can't inherit from a base class,
or that the various derived classes from a base class can't be
singletons.


Nope, that's fine.
That said, the only thing you _can't_ do within the letter of the law
is put the singleton code in the base class, and have inheriting
classes inherit their "singleton-ness" from the base. Neither can you
guarantee that each derived class is a singleton. You have to write the
singleton pattern code in each derived class.


Exactly. However, that's what I thouhght was wanted, given this part of
the first post:

<quote>
In a little test application I allways get an
compile error, because it is not possible to convert a class2 (subclass
of class1) in a class1 (general singleton class) object by calling
getInstance().
</quote>

Note that class1 here is meant to be a singleton class, and class2 is
derived from it - hence breaking its singleton nature.

--
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
Dec 7 '05 #10

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

Similar topics

3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
3
by: Michael | last post by:
Ok, I've got a Singleton template: template<class T> class Singleton { public: static T& GetInstance() { static T* pInst= NULL;
7
by: Mahesh | last post by:
Hello, I am having a query about inheritance. I have a virtual base class that is derived by to other classes that are hirarchially at the same level. But I still need to ensure that only a...
13
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...
2
by: Andrew Ducker | last post by:
I'm implementing a singleton using the example at: http://www.yoda.arachsys.com/csharp/singleton.html as a basis (second example). However - I have about 20 classes I wish to make singletons -...
6
by: Andrew Ducker | last post by:
Let's say I have a root class called RootBusinessService and I then want to have 25 business service classes based off of it. And each class has a property that's shared between all instances of...
33
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able...
2
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...
1
by: Mr. Croup | last post by:
Hi, maybe someone can help with this one... I have a templated singleton class from which some manager derives template<class S> class Singleton { private: static S * SingletonInstance;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.