Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Singleton vs Static

by BUU (Prior)
on Jul 08, 2003 at 01:54 UTC ( [id://272176]=perlquestion: print w/replies, xml ) Need Help??

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

I'm familiar with the Singleton pattern, where you define a class that only creates one object and returns it for all requests. My question is, does this pattern offer any advantages over just using class methods?

Replies are listed 'Best First'.
Re: Singleton vs Static
by dws (Chancellor) on Jul 08, 2003 at 02:50 UTC
    My question is, does [the Singleton] pattern offer any advantages over just using class methods?

    One advantage is that you can pass the singleton as an argument to functions/methods that expect an object, and they can use the Singleton as an object (e.g., they can call its methods). This isn't as big an advantage in Perl as it is in other languages, because

    "NameOfClass"->method(); -or- $object->method();
    where $object holds a string, do the right thing.

    When you're using class methods, you can't access instance data, and must access globals. Again, maybe not a huge disadvantage.

    But with class methods, you can reach into them to do

    $object->{'attribute'}
    But you probably shouldn't be doing that anyway.

Re: Singleton vs Static
by Dog and Pony (Priest) on Jul 08, 2003 at 04:36 UTC
    It makes sense when you have one instance of some kind of data that makes sense to manipulate as an object. There is hardly anything you can't do with class variables and methods, or for that matter with global variables and normal subs - but the singleton pattern can make for more logical, easier to write and maintain code.

    You might want to initialize some data the first time it is accessed if it is accessed, that is done automatically whenever you try to get the singleton the first time. You might want to access the data in different ways, including getting it via calcualting accessors, that has no direct mapping to the data, etc. Many such cases makes more sense to wrap up in an object, although you could do this with any kind of global data instead.

    It is a design pattern, and thus mainly a way to design your code in a hopefully better way for certain situations. It doesn't make something otherwise impossible possible. :)


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Singleton vs Static
by diotalevi (Canon) on Jul 08, 2003 at 09:53 UTC

    Singleton allows me to have a sort of global object with a class/method interface instead of a plain global variable. Also, Class::WeakSingleton allows me to have a singleton that can expire when no more references to the object remain. It just makes for a handy way to pass data around sometimes. I use it for a place to stash some data that is used in a database wrapper and then re-used later deep in the database hook methods. It would have been difficult to put the data into the database and awkward to just have a global - so a comprimise.

Re: Singleton vs Static
by mvc (Scribe) on Jul 08, 2003 at 11:17 UTC
    1. It is easier to refactor from Singleton to multi-object, than from static
    2. If you are doing OO, then using Singleton on a class, will make it more consistent with the rest of the system

    The Singleton pattern adds a little EEK to the system. Which is why we have Aspect::Singleton.

Re: Singleton vs Static
by dragonchild (Archbishop) on Jul 08, 2003 at 12:46 UTC
    Furthermore, you might have more than one copy of a Singleton in a system. This usually comes up in a mod_perl environment. Unless you have a separate process that you communicate with for something like your DB or a bunch of constants, you end up having to have one instance of a singleton for each child process. In this situation, you want the singleton to be OO.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://272176]
Approved by hossman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 21:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found