leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I've had some confusion about singleton objects and "scope"- of that object "instance".

I've seen stated that you can have only one instance of a singleton object- system wide. This is complete bs- right? A gross blanket statement.

It seems a singleton object is only good for the - how would you say this.. process? application instance? whatever first set your program in motion via the perl interpreter? The opening line here seems much more sensible.

If a singleton object were good system wide, it would mean that simply by calling use X, every process started by every user/non-user on that system shares the same access to the same data, (perhaps in memory in this case? or swap.. who knows). As I experience it- a singleton is not a one line daemon-ish hack.

I imagine the way to do this really- would be store the object instance in ram- even caching wouldn't do here- the technicalities are way beyond me.

So- could we get some clarification as to what a singleton really does as far as scope- where with an object, each is it's own thing- a singleton is the same thing as far as the current instance of your initial process/app instance is concerned, is this correct?

Replies are listed 'Best First'.
Re: what is the real scope of a singleton
by jeffa (Bishop) on Dec 23, 2008 at 15:25 UTC

    Yes. A Singleton lives the life of your application. If you want it to live longer you would need to serialize it -- as well as provide an interface that serializes the object when apps that use it are closed as well as an interface that de-serializes and re-instantiates (?) it into RAM whenever it is requested. What a horrible mess that will be. :) I can't think of a reason why I would a Singleton that lives past the life on an app, but if it can be done then surely it solves some problem*.

    *I think this is a stretch -- but in some ways the session attached to a user in a web app might qualify as such a singleton ... sort of.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Yea, I guess.. serialize it.. maybe seek memory somehow- for a live instance of this- how to go about it is way beyond me.

      I think simply using cache in this case- there is no way you could keep various instances in sync without molesting the system resources. We're not talking about storing some value of some expensive operation to a timeframe resolution- but .. an instant kind of .. storage.
      I guess this is more an ipc thing.

      Dunno, daemonizing or using some witchy pipe or some other posix conjuration.

      But yea- as the original post- I don't think this kind of behaviour is something you would want to have happen without being fully aware of what's up- every process on the system accessing the same chunk of memory space- very insteresting though.

      Thanks for clarifying, you rock.

Re: what is the real scope of a singleton
by jdporter (Paladin) on Dec 23, 2008 at 19:15 UTC

    I would say that "system wide" is correct; what is open to interpretation is "system". It could be, and often is, a heavy-weight process, but it could be a light-weight process (aka thread), or it could be a system of networked processes, or whatever makes sense in your situation. I'm almost tempted to say that a system is, by definition, the scope of a singleton, rather than the other way around.

    Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.
Re: what is the real scope of a singleton
by chromatic (Archbishop) on Dec 23, 2008 at 19:12 UTC

    Design patterns are descriptions of common elements of design. You oughtn't worry whether a particular piece of code conforms to a very precise definition of a design pattern. They're not prescriptive; they're descriptive.