NAME

Class::LazyFactory - Base class factory for lazy-loaded concrete classes

SYNOPSIS

# factory class 
package MyHello::Factory;
use strict;
use base qw/Class::LazyFactory/;

__PACKAGE__->initialize_factory( 
    namespace   => 'MyHello::Impl',
    constructor => 'new',
);


# the base class
package MyHello::Impl::Abstract;
use strict;
use Carp;

sub new { my $class = shift; return bless({ },$class); }
sub get_greeting { croak "Unimplemented" }


# concrete class #1
package MyHello::Impl::English;
use strict;
use base qw/MyHello::Impl::Abstract/;

sub get_greeting { "hello world" }


# main.pl
my $greeting = MyHello::Factory->new( "English" );
print $greeting->get_greeting();

DESCRIPTION

Class::LazyFactory is a base class for factory classes. Concrete classes are lazy loaded, i.e. dynamically require()d, and instances of the concrete classes are constructed.

By using a factory method design pattern, one can provide a consistent interface for constructing a family of classes, without knowing the actual concrete classes in advance.

USAGE

Class::LazyFactory should be used as the base class for your factory objects. For example:

package MyHello::Factory;
use strict;
use base qw/Class::LazyFactory/;

__PACKAGE__->initialize_factory( 
    namespace   => 'MyHello::Impl',
    constructor => 'new',
);

MyHello::Factory becomes your factory for constructing objects under the namespace of MyHello::Impl. Concrete classes need not be registered in advance in order to be instantiated. Concrete classes will only be loaded upon first construction.

METHODS

FACTORY_CLASS->initialize_factory( %params )

This should be called in the factory class inheriting from Class::LazyFactory.

Parameters are as follows:

namespace       => optional, use of a namespace is strongly recommended.
                   This is the prefix used when resolving the 
                   fully-qualified namespace of the concrete class.
constructor     => optional, defaults to 'new'.
                   This is the class method that is called when 
                   instantiating a concrete class.
FACTORY_CLASS->new( $concrete_class_name, @params )

Returns an instance of a concrete class. The fully-qualified class name of the concrete class is resolved by concatenating the namespace prefix (see initialize_factory) and $concrete_class_name.

@params is passed to the concrete class constructor.

SEE ALSO

Factory Design Patterns - Abstract Factory, Factory Method

DBI, DBI::DBD - This module was heavily inspired by DBI; DBI reflects the factory design pattern and that drivers are dynamically loaded from the given DSN string.

Class::Factory

AUTHOR

Dexter Tad-y, <dtady@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2009 by Dexter Tad-y

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.