Exforsys

Online Training

Singleton inheritance

This is a discussion on Singleton inheritance within the Software Patterns forums, part of the Testing category; I have looked at quite a number of singleton implementations in C++ trying to find out if I am trying ...


Go Back   Exforsys > Testing > Software Patterns

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-09-2004, 10:46 AM
Mike
Guest
 
Posts: n/a
Singleton inheritance

I have looked at quite a number of singleton implementations in C++
trying to find out if I am trying to do something stupid. I cannot find
anyone trying to do what I am trying, this leads me to think I am
trying to do something stupid!

I have 3 mapping classes that map values to strings and visa versa. I
had one base class that held a STL map, boost serialization and the two
access methods. I made the constructors of the 3 subclasses fill in the
maps with the different data.
I then realized that as these 3 maps were referenced by may other
classes that there should be only one instance of each derived map.

My problem is than I cannot get these derived classes to compile when I
try to apply a singleton approach to their creation.
Does it make sense to try and make singletons out of derived classes,
bearing in mind that the base class cannot be the singleton as there
has to be 3 flavors of it ?


Many thanks Mike

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-15-2004, 12:00 PM
elrond_III
Guest
 
Posts: n/a
Re: Singleton inheritance

Mike wrote:
[snip]
> My problem is than I cannot get these derived classes to compile when I
> try to apply a singleton approach to their creation.
> Does it make sense to try and make singletons out of derived classes,
> bearing in mind that the base class cannot be the singleton as there
> has to be 3 flavors of it ?


I am not a that experienced C++ programmer, however:

I made a ingelton for a sample programm vor the university in C++, there
I had a supercall too, which was not a singelton (it was a superclass
for the Obervable Pattern).

It should not be a problem to make a singelton from a class which is a
subclass from another since a singelton just says that this one and only
class instance is uniqu, and not that there is only one instance of
every class in the inheritance three.
(If it would be that way, a singelton in Java for example would be
impossible, since every class is derives from the class object)

I guess you problem lies somwhere else, not in the fact that your
superclass is not an singelton (oviusely, as you said if your superclass
would be a singelton, you wouldn't have the possibility to have three
sublasses instanced at the same time...)

hope I could help anyway, and excues me for my english...
cu
Joerg

--
--------------------------------------------------------------
Elrond_III -==: Hack the World :==-
g:R->R; [a,b] in D(g) and g[a,b] in [a,b] and for all x1,x2 in [a,b]
|g(x1)-g(x2)|<=L*|x1-x2| with L <= 1 then one s in [a,b] exist with
s=g(s)
-"Banachscher Fixpunktsatz"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-18-2004, 08:13 PM
Andrew McDonagh
Guest
 
Posts: n/a
Re: Singleton inheritance

Hi Mike,

IMHO, it is indeed unusual to have singletons that derived classes,
simply because this implies that the are other objects who are very
similar. In your case we have tree objects of the same base type, so
trying to make them a singleton (to me) smells.

You need to ask yourself why do these classes have to be a singleton.
Is it absolutely vital that the application only ever has a single
instance of these classes, or is it just that at the moment you only
need a single instance, or is it because of the easy access to them
provided by the static getInstance() method?

If there is a need, then a design pattern which would help your
situation is 'toolbox'.
http://www-106.ibm.com/developerwork...co-single.html

Essentially we have a normal singleton (i.e. the Toolbox), which
provides the 'one-and-only-one' instance of your tree derived classes
for the application.




Mike wrote:
> I have looked at quite a number of singleton implementations in C++
> trying to find out if I am trying to do something stupid. I cannot find
> anyone trying to do what I am trying, this leads me to think I am
> trying to do something stupid!
>
> I have 3 mapping classes that map values to strings and visa versa. I
> had one base class that held a STL map, boost serialization and the two
> access methods. I made the constructors of the 3 subclasses fill in the
> maps with the different data.
> I then realized that as these 3 maps were referenced by may other
> classes that there should be only one instance of each derived map.
>
> My problem is than I cannot get these derived classes to compile when I
> try to apply a singleton approach to their creation.
> Does it make sense to try and make singletons out of derived classes,
> bearing in mind that the base class cannot be the singleton as there
> has to be 3 flavors of it ?
>
>
> Many thanks Mike
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-18-2004, 09:06 PM
Rich MacDonald
Guest
 
Posts: n/a
Re: Singleton inheritance

"Mike" <mike310z@hotmail.com> wrote in news:1102603564.530268.307870
@f14g2000cwb.googlegroups.com:

> Does it make sense to try and make singletons out of derived classes,
> bearing in mind that the base class cannot be the singleton as there
> has to be 3 flavors of it ?


Seems reasonable to me. Just pretend that base class doesn't exist, i.e.,
its just providing some common methods. As far as code ugliness to get over
some C++ hassles, you're on your own...

P.S. This question is better asked over at comp.object.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-21-2004, 06:54 PM
James Rafter
Guest
 
Posts: n/a
Re: Singleton inheritance

Post your code (if that's appropriate in this group). Or post
it at comp.lang.c++ or comp.lang.c++.moderated.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:23 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.