comparison Lib/IMPL/Object/Singleton.pm @ 143:d9dd3500ead3

Singleton behavior changed
author wizard
date Thu, 08 Jul 2010 23:46:49 +0400
parents 0d2337e203c0
children b56ebc31bf18
comparison
equal deleted inserted replaced
142:4c9849f0f879 143:d9dd3500ead3
2 use strict; 2 use strict;
3 use warnings; 3 use warnings;
4 4
5 my %instances; 5 my %instances;
6 6
7 sub CTOR {
8 die new IMPL::InvalidOperationException("Only one instance of the singleton can be created",ref $_[0], $instances{ref $_[0]}) if $instances{ref $_[0]};
9 $instances{ref $_[0]} = $_[0];
10 }
11
7 sub instance { 12 sub instance {
8 my $self = shift; 13 my $self = shift;
9 14
10 $instances{$self} || ($instances{$self} = $self->new(@_)); 15 $instances{$_[0]};
11 } 16 }
12 17
13 1; 18 1;
14 19
15 __END__ 20 __END__