annotate Lib/IMPL/Object/Factory.pm @ 67:9f5795a10939

Documentation, minor fixes
author wizard
date Fri, 19 Mar 2010 20:06:12 +0300
parents 259cd3df6e53
children 2f31ecabe9ea
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
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
4 use base qw(IMPL::Object IMPL::Object::Serializable);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
5
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
6 use IMPL::Class::Property;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
7
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
8 BEGIN {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
9 public property factory => prop_get | owner_set;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
10 public property parameters => prop_get | owner_set;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
11 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
12
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
13 # custom factory, overrides default
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
14 sub new {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
15 my $self = shift;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
16
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
17 return ref $self ? $self->CreateObject(@_) : $self->SUPER::new(@_);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
18 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
19
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
20 sub CTOR {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
21 my ($this,$factory,$parameters) = @_;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
22
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
23 $this->factory($factory) or die new IMPL::InvalidArgumentException("The argument 'factory' is mandatory");
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
24 $this->parameters($parameters) if $parameters;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
25 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
26
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
27 # override default restore method
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
28 sub restore {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
29 my ($class,$data,$surrogate) = @_;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
30
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
31 my %args = @$data;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
32
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
33 if ($surrogate) {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
34 $surrogate->callCTOR($args{factory},$args{parameters});
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
35 return $surrogate;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
36 } else {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
37 return $class->new($args{factory},$args{parameters});
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
38 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
39 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
40
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
41 sub CreateObject {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
42 my $this = shift;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
43
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
44 return $this->factory->new($this->parameters ? (_as_list($this->parameters),@_) : @_);
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 _as_list {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
48 ref $_[0] ?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
49 (ref $_[0] eq 'HASH' ?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
50 %{$_[0]}
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
51 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
52 (ref $_[0] eq 'ARRAY'?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
53 @{$_[0]}
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
54 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
55 $_[0]
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
56 )
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
57 )
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
58 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
59 ($_[0]);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
60 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
61
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
62
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
63 1;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
65 __END__
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
66
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
67 =pod
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
68
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
69 =head1 SYNOPSIS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
70
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
71 =begin code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
72
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
73 my $factory = new IMPL::Object::Factory(
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
74 'MyApp::User',
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
75 {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
76 isAdmin => 1
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
77 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
78 );
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
79
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
80 my $class = 'MyApp::User';
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
81
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
82 my $user;
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
83
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
84 $user = $class->new(name => 'nobody'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
85 # and pass parameters (name=>'nobody')
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
86
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
87 $user = $factory->new(name => 'root'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
88 # and pass paremeters (isAdmin => 1, name => 'root')
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
89
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
90 =end code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
91
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
92 Или сериализованная форма в XML.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
93
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
94 =begin code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
95
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
96 <factory type="IMPL::Object::Factory">
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
97 <factory>MyApp::User</factory>,
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
98 <parameters type="HASH">
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
99 <isAdmin>1</isAdmin>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
100 </parameters>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
101 </factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
102
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
103 =end code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
104
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
105 =head1 DESCRIPTION
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
106
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
107 C<[Serializable]>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
108
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
109 Класс, реализующий фабрику классов.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
110
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
111 Фабрика классов это любой объект, который имеет метод C< new > вызов которого приводит к созданию нового
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
112 объекта. Например каждый класс сам явялется фабрикой, поскольку, если у него вызвать метод
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
113 C< new >, то будет создан объект. Полученные объекты, в силу механизмов языка Perl, также
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
114 являются фабриками, притом такимиже, что и класс.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
115
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
116 Данный класс меняет поведение метода C< new > в зависимости от контекста вызова: статического
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
117 метода или метода объекта. При вызове метода C< new > у класса происходит создание объекта
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
118 фабрики с определенными параметрами. Далее объект-фабрика может быть использована для создания
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
119 объектов уже на основе параметров фабрики.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
120
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
121 =head1 MEMBERS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
122
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
123 =over
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
124
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
125 =item C< CTOR($factory,$parameters) >
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
126
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
127 Создает новый экземпляр
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
128
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
129 =over
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
130
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
131 =item C<$factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
132
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
133 Либо имя класса, либо другая фабрика.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
134
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
135 =item C<$parameters>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
136
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
137 Ссылка на параметры для создания объектов, может быть ссылкой на хеш, массив и т.д.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
138
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
139 Если является ссылкой на хеш, то при создании объектов данной фабрикой этот хеш
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
140 будет развернут в список и передан параметрами методу C<new>.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
141
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
142 Если является ссылкой на массив, то при создании объектов данной фабрикой этот массив
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
143 будет передан в списк и передан параметрами методу C<new>.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
144
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
145 Если является любым другим объектом или скаляром, то будет передан параметром методу
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
146 C<new> как есть.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
147
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
148 =back
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
149
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
150 =item C< [get] factory >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
151
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
152 Свойство, содержащее фабрику для создание новых объектов текущей фабрикой. Чаще всего оно содержит
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
153 имя класса.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
154
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
155 =item C< [get] parameters >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
156
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
157 Свойство, содержит ссылку на параметры для создания объектов, при создании объекта эти параметры будут
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
158 развернуты в список и переданы оператору C< new > фабрике из свойства C< factory >, за ними будут
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
159 следовать параметры непосредственно текущей фабрики.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
160
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
161 =item C<new(@params)>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
162
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
163 Создает новый объект, используя свйство C<factory> как фабрику и передавая туда параметры
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
164 из свойства C<parameters> и списка C<@params>. Ниже приведен упрощенный пример, как это происходит.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
165
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
166 =begin code
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
167
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
168 sub new {
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
169 my ($this,@params) = @_;
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
170
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
171 return $this->factory->new(_as_list($this->parameters), @params);
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
172 }
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
173
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
174 =end code
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
175
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
176 =back
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
177
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
178 =cut