annotate Lib/IMPL/Object/Factory.pm @ 64:259cd3df6e53

Doc generation Minor fixes
author wizard
date Mon, 15 Mar 2010 17:45:13 +0300
parents
children 9f5795a10939
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 sub ProcessItems {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
74 my ($factory,$items);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
75
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
76 return map $factory->new($_), @$items;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
77 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
78
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
79 my @users = ProcessItems('MyApp::User',$db->selectUsers);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
80
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
81 my $factory = new IMPL::Object::Factory(
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
82 'MyApp::User',
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
83 {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
84 isAdmin => 1
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
85 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
86 );
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
87
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
88 =end code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
89
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
90 my @admins = ProcessItems($factory,$db->selectAdmins);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
91
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
92
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
93 =head1 DESCRIPTION
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
94
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
95 Класс, реализующий фабрику классов.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
96
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
97 Фабрика классов это любой объект, который имеет метод C< new > вызов которого приводит к созданию нового
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
98 объекта. Например каждый класс сам явялется фабрикой, поскольку, если у него вызвать метод
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
99 C< new >, то будет создан объект. Полученные объекты, в силу механизмов языка Perl, также
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
100 являются фабриками, притом такимиже, что и класс.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
101
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
102 Данный класс меняет поведение метода C< new > в зависимости от контекста вызова: статического
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
103 метода или метода объекта. При вызове метода C< new > у класса происходит создание объекта
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
104 фабрики с определенными параметрами. Далее объект-фабрика может быть использована для создания
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
105 объектов уже на основе параметров фабрики.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
106
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
107 =head1 MEMBERS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
108
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
109 =over
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
110
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
111 =item C< factory >
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
112
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
113 Свойство, содержащее фабрику для создание новых объектов текущей фабрикой. Чаще всего оно содержит
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
114 имя класса.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
115
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
116 =item C< parameters >
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
117
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
118 Свойство, содержит ссылку на параметры для создания объектов, при создании объекта эти параметры будут
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
119 развернуты в список и переданы оператору C< new > фабрике из свойства C< factory >, за ними будут
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
120 следовать параметры непосредственно текущей фабрики.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
121
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
122 =back
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
123
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
124 =cut