3 # BioPerl module for Bio::Factory::ObjectFactory
5 # Cared for by Hilmar Lapp <hlapp at gmx.net>
7 # Copyright Hilmar Lapp
9 # You may distribute this module under the same terms as perl itself
12 # (c) Hilmar Lapp, hlapp at gmx.net, 2003.
13 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2003.
15 # You may distribute this module under the same terms as perl itself.
16 # Refer to the Perl Artistic License (see the license accompanying this
17 # software package, or see http://www.perl.com/language/misc/Artistic.html)
18 # for the terms under which you may use, modify, and redistribute this module.
20 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
21 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 # POD documentation - main docs before the code
29 Bio::Factory::ObjectFactory - Instantiates a new Bio::Root::RootI (or derived class) through a factory
33 use Bio::Factory::ObjectFactory;
35 my $factory = Bio::Factory::ObjectFactory->new(-type => 'Bio::Ontology::GOterm');
36 my $term = $factory->create_object(-name => 'peroxisome',
37 -ontology => 'Gene Factory',
38 -identifier => 'GO:0005777');
43 This object will build L<Bio::Root::RootI> objects generically.
49 User feedback is an integral part of the evolution of this and other
50 Bioperl modules. Send your comments and suggestions preferably to
51 the Bioperl mailing list. Your participation is much appreciated.
53 bioperl-l@bioperl.org - General discussion
54 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
58 Report bugs to the Bioperl bug tracking system to help us keep track
59 of the bugs and their resolution. Bug reports can be submitted via the
62 http://bugzilla.open-bio.org/
64 =head1 AUTHOR - Hilmar Lapp
66 Email hlapp at gmx.net
71 This is mostly copy-and-paste with subsequent adaptation from
72 Bio::Seq::SeqFactory by Jason Stajich. Most credits should in fact go
77 The rest of the documentation details each of the object methods.
78 Internal methods are usually preceded with a _
83 # Let the code begin...
86 package Bio
::Factory
::ObjectFactory
;
90 use base
qw(Bio::Root::Root Bio::Factory::ObjectFactoryI);
95 Usage : my $obj = Bio::Factory::ObjectFactory->new();
96 Function: Builds a new Bio::Factory::ObjectFactory object
97 Returns : Bio::Factory::ObjectFactory
98 Args : -type => string, name of a L<Bio::Root::RootI> derived class.
100 -interface => string, name of the interface or class any type
101 specified needs to at least implement.
102 The default is Bio::Root::RootI.
107 my($class,@args) = @_;
109 my $self = $class->SUPER::new
(@args);
111 my ($type,$interface) = $self->_rearrange([qw(TYPE INTERFACE)], @args);
113 $self->{'_loaded_types'} = {};
114 $self->interface($interface || "Bio::Root::RootI");
115 $self->type($type) if $type;
123 Title : create_object
124 Usage : my $seq = $factory->create_object(<named parameters>);
125 Function: Instantiates a new object of the previously set type.
127 This object allows us to genericize the instantiation of
130 You must have provided -type at instantiation, or have
131 called type($mytype) before you can call this method.
133 Returns : an object of the type returned by type()
135 The return type is configurable using new(-type =>"..."),
136 or by calling $self->type("My::Fancy::Class").
137 Args : Initialization parameters specific to the type of
138 object we want. Check the POD of the class you set as type.
143 my ($self,@args) = @_;
145 my $type = $self->type(); # type has already been loaded upon set
146 return $type->new(-verbose
=> $self->verbose, @args);
152 Usage : $obj->type($newval)
153 Function: Get/set the type of object to be created.
155 This may be changed at any time during the lifetime of this
158 Returns : value of type (a string)
159 Args : newvalue (optional, a string)
169 if($type && (! $self->{'_loaded_types'}->{$type})) {
171 $self->_load_module($type);
174 $self->throw("module for '$type' failed to load: ".
177 my $o = bless {},$type;
178 if(!$self->_validate_type($o)) { # this may throw an exception
179 $self->throw("'$type' is not valid for factory ".ref($self));
181 $self->{'_loaded_types'}->{$type} = 1;
183 return $self->{'type'} = $type;
185 return $self->{'type'};
191 Usage : $obj->interface($newval)
192 Function: Get/set the interface or base class that supplied types
193 must at least implement (inherit from).
195 Returns : value of interface (a scalar)
196 Args : on set, new value (a scalar or undef, optional)
203 my $interface = shift;
206 return $self->{'interface'} = $interface;
208 return $self->{'interface'};
211 =head2 _validate_type
213 Title : _validate_type
214 Usage : $factory->_validate_type($object)
215 Function: Called to let derived factories validate the type set
218 The default implementation here checks whether the supplied
219 object skeleton implements the interface set via -interface
220 upon factory instantiation.
223 Returns : TRUE if the type is to be considered valid, and FALSE otherwise.
224 Instead of returning FALSE this method may also just throw
225 an informative exception.
227 The default implementation here will throw an exception
228 if the supplied object does not inherit from the interface
229 provided by the interface() method.
231 Args : A hash reference blessed into the specified type, allowing
238 my ($self,$obj) = @_;
240 if(! $obj->isa($self->interface())) {
241 $self->throw("invalid type: '".ref($obj).
242 "' does not implement '".$self->interface()."'");
247 #####################################################################
248 # aliases for naming consistency or other reasons #
249 #####################################################################
251 *create
= \
&create_object
;