in reply to Re^2: Singletons and Inheritance
in thread Singletons and Inheritance
And if any code calls $class->new() directly, they'll get another instance which getInstance knows nothing about, so it doesn't interfere.my %instances; sub getInstance { my $class = shift; $instances{$class} ||= $class->new(@_); return $instances{$class}; }
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).
|
|---|