annotate Lib/IMPL/Object/Singleton.pm @ 290:7b0dad6117d5

*TTView: fixed memory leak
author sergey
date Wed, 20 Feb 2013 17:24:57 +0400
parents 0f59b2de72af
children 75a78cbf7dcf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
1 package IMPL::Object::Singleton;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
2 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
3 use warnings;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
4
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
5 require IMPL::Exception;
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
6 use parent qw(
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
7 IMPL::Class::Meta
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
8 );
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
9
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
10 __PACKAGE__->static_accessor_own(_instance => undef);
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
11
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
12 my %instances;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
13
143
d9dd3500ead3 Singleton behavior changed
wizard
parents: 88
diff changeset
14 sub CTOR {
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
15 die IMPL::InvalidOperationException->new("Only one instance of the singleton can be created",ref $_[0])
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
16 if $_[0]->_instance;
143
d9dd3500ead3 Singleton behavior changed
wizard
parents: 88
diff changeset
17 }
d9dd3500ead3 Singleton behavior changed
wizard
parents: 88
diff changeset
18
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
19 sub instance {
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
20 my $this = shift;
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
21 return $this->_instance || $this->_instance($this->Activate());
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
22 }
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
23
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
24 sub Activate {
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
25 die IMPL::NotImplementedException->new("Activation isn't implemented", shift);
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
26 }
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
27
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
28 sub Release {
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
29 shift->_instance(undef);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
30 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
31
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
32 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
33
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
34 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
35
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
36 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
37
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
38 =head1 SYNOPSIS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
39
88
0d2337e203c0 minor changes
wizard
parents: 49
diff changeset
40 =begin code
0d2337e203c0 minor changes
wizard
parents: 49
diff changeset
41
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
42 package Foo;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
43
166
4267a2ac3d46 Added Class::Template,
wizard
parents: 144
diff changeset
44 use parent qw(IMPL::Object IMPL::Object::Singleton);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
45
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
46 #....
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
47
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
48 Foo->isnatnce->some_work();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
50 Foo->isnatnce->get_result();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
51
88
0d2337e203c0 minor changes
wizard
parents: 49
diff changeset
52 =end code
0d2337e203c0 minor changes
wizard
parents: 49
diff changeset
53
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
54 =head1 DESCRIPTION
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
55
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
56 Реализует шаблон Singleton. Наследники данного класса могут иметь только один
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
57 экземпляр. Создать этот экземпляр можно явно, используюя конструктор, либо
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
58 автоматически при обращении к свойству C<instance>, для этого нужно
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
59 переопределить метод C<Activate()>
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
60
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
61 =head1 MEMBERS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
62
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
63 =head2 C<CTOR()>
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
64
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
65 Проверяет на единственность экземпляра класса, запоминает созданный экземпляр.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
66
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
67 =head2 C<[static,get]instance>
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
68
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
69 Текущий экземпляр класса, если он еще не создан, то вызывает метод C<Activate>.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
70
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
71 =head2 C<[static,abstract]Activate()>
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
72
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
73 Вызывается автоматически при обращении к свойству C<instance>, если экземпляр
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
74 объекта еще не был создан.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
75
263
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
76 =head2 C<[static]Release()>
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
77
0f59b2de72af *fixed IMPL::DOM::Schema circular module references
sergey
parents: 194
diff changeset
78 Освобождает текущий экземпляр.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
79
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
80 =cut