Multiple Singletons?

Multiple Singletons?

Post by Albe » Wed, 28 Jul 1999 04:00:00



Right now, I have a system in which I have one basic ADT
and around 50+ classes that derive from it. The thing is,
there should be only one instance of each class ever
existing at once, which leads me to believe that making
them all Singletons would be a good idea. But...50+
Singletons? Surely there is a better way. Right now I'm
just instantiating them into a list class, and making
all the objects as static, but this seems messy to me.
Any better suggestions?

--

Albert

"LOGIC, n. The art of thinking and reasoning
 in strict accordance with the limitations and
 incapacities of the human misunderstanding."
 - Ambrose Bierce, "The Devil's Dictionary"

 
 
 

Multiple Singletons?

Post by Daniel » Wed, 28 Jul 1999 04:00:00




>Right now, I have a system in which I have one basic ADT
>and around 50+ classes that derive from it. The thing is,
>there should be only one instance of each class ever
>existing at once, which leads me to believe that making
>them all Singletons would be a good idea. But...50+
>Singletons? Surely there is a better way. Right now I'm
>just instantiating them into a list class, and making
>all the objects as static, but this seems messy to me.
>Any better suggestions?

I have a feeling you should have 50+ objects instatiated from the one
basic ADT instead of 50+ classes each with only one instance...

 
 
 

Multiple Singletons?

Post by David Anderso » Wed, 28 Jul 1999 04:00:00


Sounds like a classic case for a Factory to me! Make the Factory a Singleton and let it be
responsible for how many instances of each of the other 50 classes, it wants to make.

David
http://www.uidesign.net


> Right now, I have a system in which I have one basic ADT
> and around 50+ classes that derive from it. The thing is,
> there should be only one instance of each class ever
> existing at once, which leads me to believe that making
> them all Singletons would be a good idea. But...50+
> Singletons? Surely there is a better way. Right now I'm
> just instantiating them into a list class, and making
> all the objects as static, but this seems messy to me.
> Any better suggestions?

> --

> Albert

> "LOGIC, n. The art of thinking and reasoning
>  in strict accordance with the limitations and
>  incapacities of the human misunderstanding."
>  - Ambrose Bierce, "The Devil's Dictionary"

 
 
 

Multiple Singletons?

Post by Simone BORDE » Wed, 28 Jul 1999 04:00:00


Hi,

nice question !

Do you _really_ need to be sure that exists only 1 instance of these classes
? Maybe you can mask to the client the fact that can be more than 1 instance
using a kind of proxy.
Are you working multithread ? Be careful with static objects
Are you using C++ ?

What you can do is use only 1 singleton that "registers" all the instances
and use something like
Singleton& Singleton::instance(const std::string& name) to retrieve them by
name
or if you want to use them without casting the return value use something
like
template <typename T>
T& getSingletonInstance(const std::string& name)

You can also avoid to have all of them static, using plain objects (that
will open you the door of using polimorphism if needed) making their
constructor private or protected and use a single friend class
("SingletonManager") to instantiate all of them (friends not always break
encapsulation, sometimes strenghten it) ensuring this way that only
singleton manager can instantiate them

Hope helped,
Enjoy

Simon


Quote:> Right now, I have a system in which I have one basic ADT
> and around 50+ classes that derive from it. The thing is,
> there should be only one instance of each class ever
> existing at once, which leads me to believe that making
> them all Singletons would be a good idea. But...50+
> Singletons? Surely there is a better way. Right now I'm
> just instantiating them into a list class, and making
> all the objects as static, but this seems messy to me.
> Any better suggestions?

> --

> Albert

> "LOGIC, n. The art of thinking and reasoning
>  in strict accordance with the limitations and
>  incapacities of the human misunderstanding."
>  - Ambrose Bierce, "The Devil's Dictionary"

 
 
 

Multiple Singletons?

Post by Robert O'Dow » Thu, 29 Jul 1999 04:00:00



> Right now, I have a system in which I have one basic ADT
> and around 50+ classes that derive from it. The thing is,
> there should be only one instance of each class ever
> existing at once, which leads me to believe that making
> them all Singletons would be a good idea. But...50+
> Singletons? Surely there is a better way. Right now I'm
> just instantiating them into a list class, and making
> all the objects as static, but this seems messy to me.
> Any better suggestions?

Try defining a helper class with the following behavior.

1)  It is a Singleton
2)  It contains a single instance of every class you need.
3)  As appropriate to your application, delegate functionality
      to the appropriate object.

It is up to you whether or not this singleton is derived
from your ADT or not.

-<Automagically included trailer>
Robert O'Dowd                       Ph    +61 (8) 8259 6546
MOD/DSTO                            Fax    +61 (8) 8259 5139
P.O. Box 1500                       Email:

Salisbury, South Australia, 5108

Disclaimer: Opinions above are mine and may be worth what you paid for
them

 
 
 

1. Multiple server processes for singleton object?

Hi all,

I have a free-threaded singleton object which resides inside an
ATL-generated EXE.  This object is instantiated by both an NT-service and a
VB client (which also receives events from the singleton via a connection
point).

On each alarm (this code is used in an SW-based monitoring system):
- service instantiates the singleton
- service makes a method call, passing alarm data
- singleton processes the alarm data
- singleton fires an event, passing the alarm data
- service releases the singleton

The clients remain connected for the lifetime of the system (I'm not totally
sure why the service constantly instantiates and releases the singleton -
that decision was made long before anybody here was working for this
company!).

Using DCOMCNFG, the singleton is set to run as a specific user (the local
administrator account in 99% of cases), no authentication, running on the
local computer, with the default impersonation level set to "Identify".

The system works perfectly 99.9% of the time (but isn't that always the
case? :>).  Once in a blue moon (for reasons which escape me), two server
processes (EXE's) will appear in the task list.  The service will then be
instantiating the singleton from process 1, while the client will still be
connected to the singleton in process 2.  Obviously, the events then never
reach the client.

From my understanding, this should never occur.  Has anybody else ever seen
this?

Thanks!

John

2. Stream in of pin names.

3. Multiple instances of singleton object in application and dll

4. Print Pictures by using TeX ?

5. Singleton not a singleton?

6. Sorting table of contents

7. SBS v4.0 upgrade to v4.5 problem

8. Singleton class factory doesn't return HRESULT from FinalConstruct of singleton instance...

9. multiple processes & multiple threads program has advantage over one process & multiple threads program ?

10. Multiple GraphBuilders, Multiple Threads?

11. Hangup if multiple threads access multiple Exchange mailboxes

12. Multiple sockets multiple streams