Sync with main trunk
[bioperl-live.git] / Bio / Factory / ObjectFactory.pm
blobd08880f27053645643e0433f42b99c936e97fb5e
1 # $Id$
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
27 =head1 NAME
29 Bio::Factory::ObjectFactory - Instantiates a new Bio::Root::RootI (or derived class) through a factory
31 =head1 SYNOPSIS
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');
41 =head1 DESCRIPTION
43 This object will build L<Bio::Root::RootI> objects generically.
45 =head1 FEEDBACK
47 =head2 Mailing Lists
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
56 =head2 Reporting Bugs
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
60 web:
62 http://bugzilla.open-bio.org/
64 =head1 AUTHOR - Hilmar Lapp
66 Email hlapp at gmx.net
69 =head1 CONTRIBUTORS
71 This is mostly copy-and-paste with subsequent adaptation from
72 Bio::Seq::SeqFactory by Jason Stajich. Most credits should in fact go
73 to him.
75 =head1 APPENDIX
77 The rest of the documentation details each of the object methods.
78 Internal methods are usually preceded with a _
80 =cut
83 # Let the code begin...
86 package Bio::Factory::ObjectFactory;
87 use strict;
90 use base qw(Bio::Root::Root Bio::Factory::ObjectFactoryI);
92 =head2 new
94 Title : new
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.
99 There is no default.
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.
104 =cut
106 sub new {
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;
117 return $self;
121 =head2 create_object
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
128 objects.
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.
140 =cut
142 sub create_object {
143 my ($self,@args) = @_;
145 my $type = $self->type(); # type has already been loaded upon set
146 return $type->new(-verbose => $self->verbose, @args);
149 =head2 type
151 Title : type
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
156 factory.
158 Returns : value of type (a string)
159 Args : newvalue (optional, a string)
162 =cut
164 sub type{
165 my $self = shift;
167 if(@_) {
168 my $type = shift;
169 if($type && (! $self->{'_loaded_types'}->{$type})) {
170 eval {
171 $self->_load_module($type);
173 if( $@ ) {
174 $self->throw("module for '$type' failed to load: ".
175 $@);
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'};
188 =head2 interface
190 Title : interface
191 Usage : $obj->interface($newval)
192 Function: Get/set the interface or base class that supplied types
193 must at least implement (inherit from).
194 Example :
195 Returns : value of interface (a scalar)
196 Args : on set, new value (a scalar or undef, optional)
199 =cut
201 sub interface{
202 my $self = shift;
203 my $interface = shift;
205 if($interface) {
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
216 via type().
218 The default implementation here checks whether the supplied
219 object skeleton implements the interface set via -interface
220 upon factory instantiation.
222 Example :
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
232 queries like isa().
235 =cut
237 sub _validate_type{
238 my ($self,$obj) = @_;
240 if(! $obj->isa($self->interface())) {
241 $self->throw("invalid type: '".ref($obj).
242 "' does not implement '".$self->interface()."'");
244 return 1;
247 #####################################################################
248 # aliases for naming consistency or other reasons #
249 #####################################################################
251 *create = \&create_object;