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

*TTView: fixed memory leak
author sergey
date Wed, 20 Feb 2013 17:24:57 +0400
parents 5c82eec23bb6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
1 package IMPL::Object::Factory;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
2 use strict;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
3
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
4 use IMPL::Const qw(:prop);
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
5
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
6 use IMPL::declare {
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
7 base => [
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
8 'IMPL::Object' => undef,
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
9 'IMPL::Object::Serializable' => undef
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
10 ],
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
11 props => [
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
12 factory => PROP_RO,
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
13 parameters => PROP_RO,
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
14 method => PROP_RO
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
15 ]
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
16 };
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
17
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
18 # custom factory, overrides default
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
19 sub new {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
20 my $self = shift;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
21
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
22 return ref $self ? $self->CreateObject(@_) : $self->IMPL::Object::new(@_);
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
23 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
24
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
25 sub CTOR {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
26 my ($this,$factory,$parameters,$method) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
27
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
28 $this->factory($factory) or die new IMPL::InvalidArgumentException("The argument 'factory' is mandatory");
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
29 $this->parameters($parameters) if $parameters;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
30 $this->method($method) if $method;
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
31 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
32
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
33 # override default restore method
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
34 sub restore {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
35 my ($class,$data,$surrogate) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
36
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
37 my %args = @$data;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
38
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
39 if ($surrogate) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
40 $surrogate->self::CTOR($args{factory},$args{parameters},$args{method});
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
41 return $surrogate;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
42 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
43 return $class->new($args{factory},$args{parameters},$args{method});
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
44 }
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
45 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
46
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
47 sub CreateObject {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
48 my $this = shift;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
49
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
50 if (my $method = $this->method) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
51 $this->factory->$method($this->MergeParameters(@_));
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
52 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
53 $this->factory->new($this->MergeParameters(@_));
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
54 }
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
55 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
56
181
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
57 sub MergeParameters {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
58 my $this = shift;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
59
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
60 $this->parameters ? (_as_list($this->parameters),@_) : @_;
181
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
61 }
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
62
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
63
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
64 sub _as_list {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
65 ref $_[0] ?
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
66 (ref $_[0] eq 'HASH' ?
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
67 %{$_[0]}
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
68 :
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
69 (ref $_[0] eq 'ARRAY'?
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
70 @{$_[0]}
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
71 :
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
72 $_[0]
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
73 )
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
74 )
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
75 :
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
76 ($_[0]);
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
77 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
78
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
79
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
80 1;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
81
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
82 __END__
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
83
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
84 =pod
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
85
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
86 =head1 SYNOPSIS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
87
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
88 =begin code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
89
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
90 my $factory = new IMPL::Object::Factory(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
91 'MyApp::User',
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
92 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
93 isAdmin => 1
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
94 }
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
95 );
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
96
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
97 my $class = 'MyApp::User';
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
98
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
99 my $user;
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
100
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
101 $user = $class->new(name => 'nobody'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
102 # and pass parameters (name=>'nobody')
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
103
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
104 $user = $factory->new(name => 'root'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
105 # and pass paremeters (isAdmin => 1, name => 'root')
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
106
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
107 =end code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
108
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
109 Или сериализованная форма в XML.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
110
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
111 =begin code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
112
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
113 <factory type="IMPL::Object::Factory">
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
114 <factory>MyApp::User</factory>,
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
115 <parameters type="HASH">
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
116 <isAdmin>1</isAdmin>
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
117 </parameters>
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
118 </factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
119
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
120 =end code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
121
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
122 =head1 DESCRIPTION
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
123
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
124 C<[Serializable]>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
125
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
126 Класс, реализующий фабрику классов.
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
127
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
128 Фабрика классов это любой объект, который имеет метод C< new > вызов которого приводит к созданию нового
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
129 объекта. Например каждый класс сам явялется фабрикой, поскольку, если у него вызвать метод
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
130 C< new >, то будет создан объект. Полученные объекты, в силу механизмов языка Perl, также
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
131 являются фабриками, притом такимиже, что и класс.
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
132
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
133 Данный класс меняет поведение метода C< new > в зависимости от контекста вызова: статического
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
134 метода или метода объекта. При вызове метода C< new > у класса происходит создание объекта
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
135 фабрики с определенными параметрами. Далее объект-фабрика может быть использована для создания
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
136 объектов уже на основе параметров фабрики.
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
137
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
138 =head1 MEMBERS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
139
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
140 =over
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
141
73
wizard
parents: 67
diff changeset
142 =item C< CTOR($factory,$parameters,$method) >
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
143
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
144 Создает новый экземпляр фабрики.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
145
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
146 =over
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
147
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
148 =item C<$factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
149
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
150 Либо имя класса, либо другая фабрика.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
151
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
152 =item C<$parameters>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
153
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
154 Ссылка на параметры для создания объектов, может быть ссылкой на хеш, массив и т.д.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
155
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
156 Если является ссылкой на хеш, то при создании объектов данной фабрикой этот хеш
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
157 будет развернут в список и передан параметрами методу C<new>.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
158
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
159 Если является ссылкой на массив, то при создании объектов данной фабрикой этот массив
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
160 будет передан в списк и передан параметрами методу C<new>.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
161
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
162 Если является любым другим объектом или скаляром, то будет передан параметром методу
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
163 C<new> как есть.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
164
73
wizard
parents: 67
diff changeset
165 =item C<$method>
wizard
parents: 67
diff changeset
166
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
167 Имя метода (или ссылка на процедуру), который будет вызван у C<$factory> при создании
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
168 текущей фабрикой нового объекта.
73
wizard
parents: 67
diff changeset
169
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
170 =back
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
171
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
172 =item C< [get] factory >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
173
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
174 Свойство, содержащее фабрику для создание новых объектов текущей фабрикой. Чаще всего оно содержит
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
175 имя класса.
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
176
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
177 =item C< [get] parameters >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
178
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
179 Свойство, содержит ссылку на параметры для создания объектов, при создании объекта эти параметры будут
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
180 развернуты в список и переданы оператору C< new > фабрике из свойства C< factory >, за ними будут
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
181 следовать параметры непосредственно текущей фабрики.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
182
181
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
183 =item C<MergeParameters(@params)>
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
184
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
185 Метод смешивающий фиксированные параметры с параметрами переданными методу C<new(@params)>. По умолчанию
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
186 добавляет пареметры фабрики в конец к фиксированным параметрам. Для изменения этого поведения требуется
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
187 переопределить данный метод. Также этот метод можно переопределить для передачи параметров, значения
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
188 которых вычисляются.
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
189
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
190 =item C<new(@params)>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
191
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
192 Создает новый объект, используя свйство C<factory> как фабрику и передавая туда параметры
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
193 из свойства C<parameters> и списка C<@params>. Ниже приведен упрощенный пример, как это происходит.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
194
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
195 =begin code
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
196
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
197 sub new {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
198 my ($this,@params) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
199
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
200 my $method = $this->method || 'new';
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
201
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
202 return $this->factory->$method(_as_list($this->parameters), @params);
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
203 }
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
204
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
205 =end code
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
206
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
207 =back
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
208
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
209 =cut