Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Singletons and Inheritance

by Joost (Canon)
on Jul 20, 2004 at 11:28 UTC ( [id://375873]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Singletons and Inheritance
in thread Singletons and Inheritance

It's just my personal preference - I've run into too many situations where in the end you do NOT want a forced singleton. So I'd do:

my %instances; sub getInstance { my $class = shift; $instances{$class} ||= $class->new(@_); return $instances{$class}; }
And if any code calls $class->new() directly, they'll get another instance which getInstance knows nothing about, so it doesn't interfere.

Also, I think splitting up the code like this makes it a lot clearer.

OTOH, if you have a good reason to always disallow more than one instance (I can only think of interfaces to hardware), you could rename it so that getInstance is named new() and new() is named _new() or something (and don't document _new() in the public API).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found