49
|
1 package IMPL::Object::Singleton;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 my %instances;
|
|
6
|
143
|
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
|
49
|
12 sub instance {
|
|
13 my $self = shift;
|
|
14
|
143
|
15 $instances{$_[0]};
|
49
|
16 }
|
|
17
|
|
18 1;
|
|
19
|
|
20 __END__
|
|
21
|
|
22 =pod
|
|
23
|
|
24 =head1 SYNOPSIS
|
|
25
|
88
|
26 =begin code
|
|
27
|
49
|
28 package Foo;
|
|
29
|
|
30 use base qw(IMPL::Object IMPL::Object::Singleton);
|
|
31
|
|
32 #....
|
|
33
|
|
34 Foo->isnatnce->some_work();
|
|
35
|
|
36 Foo->isnatnce->get_result();
|
|
37
|
88
|
38 =end code
|
|
39
|
49
|
40 =head1 DESCRIPTION
|
|
41
|
|
42 Реализует шаблон Singleton
|
|
43
|
|
44 =head1 MEMBERS
|
|
45
|
|
46 =head2 OPERATORS
|
|
47
|
|
48 =list
|
|
49
|
|
50 =item C<instance CLASS(@params)>
|
|
51
|
|
52 Создает или возвращает экземпляр класса, если экземляр не существует, то он создается с параметрами C<@params>.
|
|
53
|
|
54 =over
|
|
55
|
|
56 =cut
|