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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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" |
|
|||
|
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 > |
|
|||
|
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. |
![]() |
| Thread Tools | |
|
|